summaryrefslogtreecommitdiff
path: root/dateutil
diff options
context:
space:
mode:
authorPaul Ganssle <paul@ganssle.io>2019-11-02 11:46:18 -0400
committerGitHub <noreply@github.com>2019-11-02 11:46:18 -0400
commitc31392f853819b8e5db0d032f6d79aadb4100a2b (patch)
tree2d80c648061c2b20fe07ce3689c792b8a7a1748d /dateutil
parentf09f06619998609e6f2f8925294c6553742349f9 (diff)
parent13301ac563d3854142ea8fafe2e2713692d09ade (diff)
downloaddateutil-git-c31392f853819b8e5db0d032f6d79aadb4100a2b.tar.gz
Merge pull request #935 from labrys/patch-1
Fix error message for gettz when passed bytes
Diffstat (limited to 'dateutil')
-rw-r--r--dateutil/test/test_tz.py30
-rw-r--r--dateutil/tz/tz.py11
2 files changed, 39 insertions, 2 deletions
diff --git a/dateutil/test/test_tz.py b/dateutil/test/test_tz.py
index 314e014..4180199 100644
--- a/dateutil/test/test_tz.py
+++ b/dateutil/test/test_tz.py
@@ -1102,6 +1102,36 @@ def test_gettz_badzone_unicode():
@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():
NYC1 = tz.gettz('America/New_York')
diff --git a/dateutil/tz/tz.py b/dateutil/tz/tz.py
index 2460b5e..af81e88 100644
--- a/dateutil/tz/tz.py
+++ b/dateutil/tz/tz.py
@@ -1615,8 +1615,15 @@ def __get_gettz():
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)