Skip to content

Commit 89f15d5

Browse files
committed
constructor.timezone: __copy_ & __deepcopy__
close #387
1 parent d0d660d commit 89f15d5

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

lib/yaml/constructor.py

+6
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ def utcoffset(self, dt=None):
3838
def dst(self, dt=None):
3939
return datetime.timedelta(0)
4040

41+
def __copy__(self):
42+
return self.__deepcopy__()
43+
44+
def __deepcopy__(self, memodict={}):
45+
return self.__class__(self.utcoffset())
46+
4147
__repr__ = __str__ = tzname
4248

4349

tests/lib/test_constructor.py

+12
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,18 @@ def test_subclass_blacklist_types(data_filename, verbose=False):
305305

306306
test_subclass_blacklist_types.unittest = ['.subclass_blacklist']
307307

308+
def test_timezone_copy(verbose=False):
309+
import copy
310+
tzinfo = yaml.constructor.timezone(datetime.timedelta(0))
311+
312+
tz_copy = copy.copy(tzinfo)
313+
tz_deepcopy = copy.deepcopy(tzinfo)
314+
315+
if tzinfo.tzname() != tz_copy.tzname() != tz_deepcopy.tzname():
316+
raise AssertionError("Timezones should be equal")
317+
318+
test_timezone_copy.unittest = []
319+
308320
if __name__ == '__main__':
309321
import sys, test_constructor
310322
sys.modules['test_constructor'] = sys.modules['__main__']

0 commit comments

Comments
 (0)