diff options
author | Guido van Rossum <guido@python.org> | 2001-09-28 18:13:29 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2001-09-28 18:13:29 (GMT) |
commit | 6cef6d5d62f083ada715da3610ce12147b625ee2 (patch) | |
tree | de5a51234c4e4675698d20abc03bd09c1b1ee0fc /Lib/test/test_descr.py | |
parent | 19405a4a2a68570cae67501935adea3f375f84bf (diff) | |
download | cpython-6cef6d5d62f083ada715da3610ce12147b625ee2.zip cpython-6cef6d5d62f083ada715da3610ce12147b625ee2.tar.gz cpython-6cef6d5d62f083ada715da3610ce12147b625ee2.tar.bz2 |
Changes to copy() and deepcopy() in copy.py to support __reduce__ as a
fallback for objects that are neither supported by our dispatch table
nor have a __copy__ or __deepcopy__ method.
Changes to _reduce() in copy_reg.py to support reducing objects that
don't have a __dict__ -- copy.copy(complex()) now invokes _reduce().
Add tests for copy.copy() and copy.deepcopy() to test_regrtest.py.
Diffstat (limited to 'Lib/test/test_descr.py')
-rw-r--r-- | Lib/test/test_descr.py | 44 |
1 files changed, 43 insertions, 1 deletions
diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py index 0357a06..2c5e7a4 100644 --- a/Lib/test/test_descr.py +++ b/Lib/test/test_descr.py @@ -2033,7 +2033,8 @@ def setclass(): cant(list(), object) def pickles(): - if verbose: print "Testing pickling new-style classes and objects..." + if verbose: + print "Testing pickling and copying new-style classes and objects..." import pickle, cPickle def sorteditems(d): @@ -2092,6 +2093,46 @@ def pickles(): print "a = x =", a print "b = y =", b + # Testing copy.deepcopy() + import copy + for cls in C, C1, C2: + cls2 = copy.deepcopy(cls) + verify(cls2 is cls) + + a = C1(1, 2); a.append(42); a.append(24) + b = C2("hello", "world", 42) + x, y = copy.deepcopy((a, b)) + assert x.__class__ == a.__class__ + assert sorteditems(x.__dict__) == sorteditems(a.__dict__) + assert y.__class__ == b.__class__ + assert sorteditems(y.__dict__) == sorteditems(b.__dict__) + assert `x` == `a` + assert `y` == `b` + if verbose: + print "a = x =", a + print "b = y =", b + +def copies(): + if verbose: print "Testing copy.copy() and copy.deepcopy()..." + import copy + class C(object): + pass + + a = C() + a.foo = 12 + b = copy.copy(a) + verify(b.__dict__ == a.__dict__) + + a.bar = [1,2,3] + c = copy.copy(a) + verify(c.bar == a.bar) + verify(c.bar is a.bar) + + d = copy.deepcopy(a) + verify(d.__dict__ == a.__dict__) + a.bar.append(4) + verify(d.bar == [1,2,3]) + def test_main(): lists() @@ -2136,6 +2177,7 @@ def test_main(): descrdoc() setclass() pickles() + copies() if verbose: print "All OK" if __name__ == "__main__": |