aboutsummaryrefslogtreecommitdiff
path: root/dateutil
diff options
context:
space:
mode:
authorPaul Ganssle <paul@ganssle.io>2018-09-02 12:52:57 -0400
committerPaul Ganssle <paul@ganssle.io>2018-09-02 14:57:41 -0400
commit0540d7f0c9be3aa2f87ce816123b802f590931e6 (patch)
tree7fc03c45b659c9091ea489cc8a6c5698fe96f357 /dateutil
parenta6e928487c4acb74f2df42987d35ed6b0737f0bc (diff)
downloaddateutil-0540d7f0c9be3aa2f87ce816123b802f590931e6.tar.gz
Improve tzname_in_python2 decorator
This improves the ability to inspect wrapped tzname methods in Python 2 and removes an unnecessary function call (to the nested function) in Python 3. From my measurements, this is about twice as fast in Python 3 and approximately the same speed in Python 2.
Diffstat (limited to 'dateutil')
-rw-r--r--dateutil/tz/_common.py22
1 files changed, 13 insertions, 9 deletions
diff --git a/dateutil/tz/_common.py b/dateutil/tz/_common.py
index ccabb7d..594e082 100644
--- a/dateutil/tz/_common.py
+++ b/dateutil/tz/_common.py
@@ -1,4 +1,4 @@
-from six import PY3
+from six import PY2
from functools import wraps
@@ -16,14 +16,18 @@ def tzname_in_python2(namefunc):
tzname() API changed in Python 3. It used to return bytes, but was changed
to unicode strings
"""
- def adjust_encoding(*args, **kwargs):
- name = namefunc(*args, **kwargs)
- if name is not None and not PY3:
- name = name.encode()
-
- return name
-
- return adjust_encoding
+ if PY2:
+ @wraps(namefunc)
+ def adjust_encoding(*args, **kwargs):
+ name = namefunc(*args, **kwargs)
+ if name is not None:
+ name = name.encode()
+
+ return name
+
+ return adjust_encoding
+ else:
+ return namefunc
# The following is adapted from Alexander Belopolsky's tz library