diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2017-01-11 20:13:03 +0200 |
---|---|---|
committer | Chris Withers <chris@withers.org> | 2019-04-30 08:39:55 +0100 |
commit | 5a0e779b61ef3a87d6bc48bc3355a73f0deb523a (patch) | |
tree | 463ace25c0d86e463f00521200ab0d2034f48b0e /mock | |
parent | f9efc3e82452b7a7c936d85ff08eb9368a2f03eb (diff) | |
download | mock-5a0e779b61ef3a87d6bc48bc3355a73f0deb523a.tar.gz |
Issue #20804: The unittest.mock.sentinel attributes now preserve their
identity when they are copied or pickled.
Backports: d9c956fb23f1c38c8050e9531ff5a77559f7f7af
Signed-off-by: Chris Withers <chris@simplistix.co.uk>
Diffstat (limited to 'mock')
-rw-r--r-- | mock/mock.py | 6 | ||||
-rw-r--r-- | mock/tests/testsentinel.py | 14 |
2 files changed, 19 insertions, 1 deletions
diff --git a/mock/mock.py b/mock/mock.py index 864366d..7377cd2 100644 --- a/mock/mock.py +++ b/mock/mock.py @@ -356,6 +356,9 @@ class _SentinelObject(object): def __repr__(self): return 'sentinel.%s' % self.name + def __reduce__(self): + return 'sentinel.%s' % self.name + class _Sentinel(object): """Access attributes to return a named object, usable as a sentinel.""" @@ -368,6 +371,9 @@ class _Sentinel(object): raise AttributeError return self._sentinels.setdefault(name, _SentinelObject(name)) + def __reduce__(self): + return 'sentinel' + sentinel = _Sentinel() diff --git a/mock/tests/testsentinel.py b/mock/tests/testsentinel.py index 3253fa3..afaa194 100644 --- a/mock/tests/testsentinel.py +++ b/mock/tests/testsentinel.py @@ -3,7 +3,8 @@ # http://www.voidspace.org.uk/python/mock/ import unittest - +import copy +import pickle from mock import sentinel, DEFAULT @@ -28,6 +29,17 @@ class SentinelTest(unittest.TestCase): # If this doesn't raise an AttributeError then help(mock) is broken self.assertRaises(AttributeError, lambda: sentinel.__bases__) + def testPickle(self): + for proto in range(pickle.HIGHEST_PROTOCOL+1): + with self.subTest(protocol=proto): + pickled = pickle.dumps(sentinel.whatever, proto) + unpickled = pickle.loads(pickled) + self.assertIs(unpickled, sentinel.whatever) + + def testCopy(self): + self.assertIs(copy.copy(sentinel.whatever), sentinel.whatever) + self.assertIs(copy.deepcopy(sentinel.whatever), sentinel.whatever) + if __name__ == '__main__': unittest.main() |