Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error message for gettz when passed bytes #935

Merged
merged 4 commits into from Nov 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion AUTHORS.md
Expand Up @@ -115,7 +115,7 @@ switch, and thus all their contributions are dual-licensed.
- bjv <brandon.vanvaerenbergh@MASKED> (@bjamesvERT)
- gl <gl@MASKED>
- gfyoung <gfyoung17@gmail.com> **D**
- labrys <labrys@MASKED> (gh: @labrys) **R**
- Labrys <labrys.git@gmail.com> (gh: @labrys) **R**
- ms-boom <ms-boom@MASKED>
- ryanss <ryanssdev@MASKED> (gh: @ryanss) **R**

Expand Down
1 change: 1 addition & 0 deletions changelog.d/935.misc.rst
@@ -0,0 +1 @@
Raise a more helpful TypeError message when passing a bytes zonename to gettz in Python 3. Reported by @labrys (gh issue #927). Fixed by @labrys (gh pr #935)
30 changes: 30 additions & 0 deletions dateutil/test/test_tz.py
Expand Up @@ -1102,6 +1102,36 @@ def test_gettz_badzone_unicode():
assert tzi is None


@pytest.mark.gettz
@pytest.mark.parametrize(
"badzone,exc_reason",
[
pytest.param(
b"America/New_York",
".*should be str, not bytes.*",
id="bytes on Python 3",
marks=[
pytest.mark.skipif(
PY2, reason="bytes arguments accepted in Python 2"
)
],
),
pytest.param(
object(),
None,
id="no startswith()",
marks=[
pytest.mark.xfail(reason="AttributeError instead of TypeError",
raises=AttributeError),
],
),
],
)
def test_gettz_zone_wrong_type(badzone, exc_reason):
with pytest.raises(TypeError, match=exc_reason):
tz.gettz(badzone)


@pytest.mark.gettz
@pytest.mark.xfail(IS_WIN, reason='zoneinfo separately cached')
def test_gettz_cache_clear():
Expand Down
11 changes: 9 additions & 2 deletions dateutil/tz/tz.py
Expand Up @@ -1615,8 +1615,15 @@ def nocache(name=None):
else:
tz = tzlocal()
else:
if name.startswith(":"):
name = name[1:]
try:
if name.startswith(":"):
name = name[1:]
except TypeError as e:
if isinstance(name, bytes):
new_msg = "gettz argument should be str, not bytes"
six.raise_from(TypeError(new_msg), e)
else:
raise
if os.path.isabs(name):
if os.path.isfile(name):
tz = tzfile(name)
Expand Down