Add support for copy_reg.dispatch_table.

Rewrote copy() and deepcopy() without avoidable try/except statements;
getattr(x, name, None) or dict.get() are much faster than try/except.
This commit is contained in:
Guido van Rossum 2003-02-07 17:30:18 +00:00
parent 2731c5cf46
commit c06e3acc73
2 changed files with 77 additions and 48 deletions

View file

@ -2,6 +2,7 @@
import sys
import copy
import copy_reg
import unittest
from test import test_support
@ -32,6 +33,19 @@ class TestCopy(unittest.TestCase):
self.assertEqual(y.__class__, x.__class__)
self.assertEqual(y.foo, x.foo)
def test_copy_registry(self):
class C(object):
def __new__(cls, foo):
obj = object.__new__(cls)
obj.foo = foo
return obj
def pickle_C(obj):
return (C, (obj.foo,))
x = C(42)
self.assertRaises(TypeError, copy.copy, x)
copy_reg.pickle(C, pickle_C, C)
y = copy.copy(x)
def test_copy_reduce(self):
class C(object):
def __reduce__(self):
@ -182,6 +196,19 @@ class TestCopy(unittest.TestCase):
self.assertEqual(y.__class__, x.__class__)
self.assertEqual(y.foo, x.foo)
def test_deepcopy_registry(self):
class C(object):
def __new__(cls, foo):
obj = object.__new__(cls)
obj.foo = foo
return obj
def pickle_C(obj):
return (C, (obj.foo,))
x = C(42)
self.assertRaises(TypeError, copy.deepcopy, x)
copy_reg.pickle(C, pickle_C, C)
y = copy.deepcopy(x)
def test_deepcopy_reduce(self):
class C(object):
def __reduce__(self):