diff options
author | Paul Ganssle <paul@ganssle.io> | 2018-07-23 12:17:37 -0400 |
---|---|---|
committer | Paul Ganssle <paul@ganssle.io> | 2018-07-23 12:26:43 -0400 |
commit | 61606bcefdf0fca7e88e0a810a42711830e623ab (patch) | |
tree | bd04ee9ad091e876f4c0673730760f775fb98d1c /dateutil | |
parent | 1ad962a2a40b35076404b75ca74645479776007b (diff) | |
download | dateutil-61606bcefdf0fca7e88e0a810a42711830e623ab.tar.gz |
Use Python3.7's nullcontext if available
The _ContextWrapper class was added before nullcontext was added
to the standard library. When available, we should prefer to use
nullcontext.
Diffstat (limited to 'dateutil')
-rw-r--r-- | dateutil/tz/tz.py | 30 |
1 files changed, 17 insertions, 13 deletions
diff --git a/dateutil/tz/tz.py b/dateutil/tz/tz.py index a9ed663..ba0d8b7 100644 --- a/dateutil/tz/tz.py +++ b/dateutil/tz/tz.py @@ -465,7 +465,7 @@ class tzfile(_tzinfo): if fileobj is not None: if not file_opened_here: - fileobj = _ContextWrapper(fileobj) + fileobj = _nullcontext(fileobj) with fileobj as file_stream: tzobj = self._read_tzfile(file_stream) @@ -1257,7 +1257,7 @@ class tzical(object): fileobj = open(fileobj, 'r') else: self._s = getattr(fileobj, 'name', repr(fileobj)) - fileobj = _ContextWrapper(fileobj) + fileobj = _nullcontext(fileobj) self._vtz = {} @@ -1784,18 +1784,22 @@ else: return calculated_offset -class _ContextWrapper(object): - """ - Class for wrapping contexts so that they are passed through in a - with statement. - """ - def __init__(self, context): - self.context = context +try: + # Python 3.7 feature + from contextmanager import nullcontext as _nullcontext +except ImportError: + class _nullcontext(object): + """ + Class for wrapping contexts so that they are passed through in a + with statement. + """ + def __init__(self, context): + self.context = context - def __enter__(self): - return self.context + def __enter__(self): + return self.context - def __exit__(*args, **kwargs): - pass + def __exit__(*args, **kwargs): + pass # vim:ts=4:sw=4:et |