From 89f15d5b05ad80ff00d26ab3375179a0e18be6ba Mon Sep 17 00:00:00 2001 From: Ovv Date: Mon, 6 Apr 2020 11:14:45 +0200 Subject: [PATCH] constructor.timezone: __copy_ & __deepcopy__ close #387 --- lib/yaml/constructor.py | 6 ++++++ tests/lib/test_constructor.py | 12 ++++++++++++ 2 files changed, 18 insertions(+) diff --git a/lib/yaml/constructor.py b/lib/yaml/constructor.py index 794681cb..60b07d33 100644 --- a/lib/yaml/constructor.py +++ b/lib/yaml/constructor.py @@ -38,6 +38,12 @@ def utcoffset(self, dt=None): def dst(self, dt=None): return datetime.timedelta(0) + def __copy__(self): + return self.__deepcopy__() + + def __deepcopy__(self, memodict={}): + return self.__class__(self.utcoffset()) + __repr__ = __str__ = tzname diff --git a/tests/lib/test_constructor.py b/tests/lib/test_constructor.py index 5a8cce21..c76df5ed 100644 --- a/tests/lib/test_constructor.py +++ b/tests/lib/test_constructor.py @@ -305,6 +305,18 @@ def test_subclass_blacklist_types(data_filename, verbose=False): test_subclass_blacklist_types.unittest = ['.subclass_blacklist'] +def test_timezone_copy(verbose=False): + import copy + tzinfo = yaml.constructor.timezone(datetime.timedelta(0)) + + tz_copy = copy.copy(tzinfo) + tz_deepcopy = copy.deepcopy(tzinfo) + + if tzinfo.tzname() != tz_copy.tzname() != tz_deepcopy.tzname(): + raise AssertionError("Timezones should be equal") + +test_timezone_copy.unittest = [] + if __name__ == '__main__': import sys, test_constructor sys.modules['test_constructor'] = sys.modules['__main__']