-
Notifications
You must be signed in to change notification settings - Fork 444
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid crashing on importing
babel.localtime
when TZ envvar is malfo…
…rmed (#1100) When `pytz` is _not_ installed, importing `babel.localtime` could fail (repeatedly) when the `TZ` environment variable is malformed enough to be caught by `_validate_tzfile_path`, which might throw a certain `ValueError`. (When `pytz` is installed, it would raise an `UnknownTimeZoneError` we already catch and ignore for the same sort of input.) Fixes #1092
- Loading branch information
Showing
2 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import sys | ||
|
||
import pytest | ||
|
||
from babel.localtime import _helpers, get_localzone | ||
|
||
|
||
@pytest.mark.skipif( | ||
sys.platform == "win32", | ||
reason="Issue 1092 is not applicable on Windows", | ||
) | ||
def test_issue_1092_without_pytz(monkeypatch): | ||
pytest.importorskip("zoneinfo", reason="zoneinfo is not available") | ||
monkeypatch.setenv("TZ", "/UTC") # Malformed timezone name. | ||
# In case pytz _is_ also installed, we want to pretend it's not, so patch it out... | ||
monkeypatch.setattr(_helpers, "pytz", None) | ||
with pytest.raises(LookupError): | ||
get_localzone() | ||
|
||
|
||
@pytest.mark.skipif( | ||
sys.platform == "win32", | ||
reason="Issue 1092 is not applicable on Windows", | ||
) | ||
def test_issue_1092_with_pytz(monkeypatch): | ||
pytest.importorskip("pytz", reason="pytz is not installed") | ||
monkeypatch.setenv("TZ", "/UTC") # Malformed timezone name. | ||
with pytest.raises(LookupError): | ||
get_localzone() |