Skip to content

Commit

Permalink
Merge pull request #935 from labrys/patch-1
Browse files Browse the repository at this point in the history
Fix error message for gettz when passed bytes
  • Loading branch information
pganssle committed Nov 2, 2019
2 parents f09f066 + 13301ac commit c31392f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
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 @@ -1101,6 +1101,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

0 comments on commit c31392f

Please sign in to comment.