diff options
author | Guido van Rossum <guido@python.org> | 2003-02-18 22:41:24 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2003-02-18 22:41:24 (GMT) |
commit | 2a30b21f6484ca738ba0a5c116aa7bb7c134bc8d (patch) | |
tree | 0e73263456121ee37aa883c853eab397f47e4d65 /Lib/test | |
parent | d6cfccf4cffd9f403e16bbc95a87a477e02fb942 (diff) | |
download | cpython-2a30b21f6484ca738ba0a5c116aa7bb7c134bc8d.zip cpython-2a30b21f6484ca738ba0a5c116aa7bb7c134bc8d.tar.gz cpython-2a30b21f6484ca738ba0a5c116aa7bb7c134bc8d.tar.bz2 |
Three test cases for __reduce_ex__. This fails for cPickle, until Tim
checks in his changes to support this in cPickle.c.
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/pickletester.py | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/Lib/test/pickletester.py b/Lib/test/pickletester.py index d541194..0c269f2 100644 --- a/Lib/test/pickletester.py +++ b/Lib/test/pickletester.py @@ -739,6 +739,58 @@ class AbstractPickleTests(unittest.TestCase): self.assertEqual(x.foo, y.foo) self.assertEqual(x.bar, y.bar) + def test_reduce_overrides_default_reduce_ex(self): + for proto in 0, 1, 2: + x = REX_one() + self.assertEqual(x._reduce_called, 0) + s = self.dumps(x, proto) + self.assertEqual(x._reduce_called, 1) + y = self.loads(s) + self.assertEqual(y._reduce_called, 0) + + def test_reduce_ex_called(self): + for proto in 0, 1, 2: + x = REX_two() + self.assertEqual(x._proto, None) + s = self.dumps(x, proto) + self.assertEqual(x._proto, proto) + y = self.loads(s) + self.assertEqual(y._proto, None) + + def test_reduce_ex_overrides_reduce(self): + for proto in 0, 1, 2: + x = REX_three() + self.assertEqual(x._proto, None) + s = self.dumps(x, proto) + self.assertEqual(x._proto, proto) + y = self.loads(s) + self.assertEqual(y._proto, None) + +# Test classes for reduce_ex + +class REX_one(object): + _reduce_called = 0 + def __reduce__(self): + self._reduce_called = 1 + return REX_one, () + # No __reduce_ex__ here, but inheriting it from object + +class REX_two(object): + _proto = None + def __reduce_ex__(self, proto): + self._proto = proto + return REX_two, () + # No __reduce__ here, but inheriting it from object + +class REX_three(object): + _proto = None + def __reduce_ex__(self, proto): + self._proto = proto + return REX_two, () + def __reduce__(self): + raise TestFailed, "This __reduce__ shouldn't be called" + +# Test classes for newobj class MyInt(int): sample = 1 |