From 16a64414653979927e50a9ebb86deebfce7b0f95 Mon Sep 17 00:00:00 2001 From: Juan-Pablo Scaletti Date: Tue, 27 Oct 2020 02:25:48 +0000 Subject: [PATCH] Add __deepcopy__ method to TomlTz class. Fix #308 (#309) Without this method, the dict that results from parsing a datetime with a timezone cannot be deep-copied using the stdlib `copy.deepcopy()` function. --- tests/test_api.py | 9 +++++++++ toml/tz.py | 3 +++ 2 files changed, 12 insertions(+) diff --git a/tests/test_api.py b/tests/test_api.py index 67bcd88..1acc26f 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -271,3 +271,12 @@ def test_comment_preserve_decoder_encoder(): encoder=toml.TomlPreserveCommentEncoder()) assert len(s) == len(test_str) and sorted(test_str) == sorted(s) + + +def test_deepcopy_timezone(): + import copy + + o = toml.loads("dob = 1979-05-24T07:32:00-08:00") + o2 = copy.deepcopy(o) + assert o2["dob"] == o["dob"] + assert o2["dob"] is not o["dob"] diff --git a/toml/tz.py b/toml/tz.py index 93c3c8a..bf20593 100644 --- a/toml/tz.py +++ b/toml/tz.py @@ -11,6 +11,9 @@ def __init__(self, toml_offset): self._hours = int(self._raw_offset[1:3]) self._minutes = int(self._raw_offset[4:6]) + def __deepcopy__(self, memo): + return self.__class__(self._raw_offset) + def tzname(self, dt): return "UTC" + self._raw_offset