import types
import weakref
from copyreg import dispatch_table
+import builtins
class Error(Exception):
pass
if t is not None:
d[t] = _copy_immutable
for name in ("complex", "unicode"):
- t = globals()['__builtins__'].get(name)
+ t = getattr(builtins, name, None)
if t is not None:
d[t] = _copy_immutable
args = deepcopy(args, memo)
y = callable(*args)
memo[id(x)] = y
- if listiter is not None:
- for item in listiter:
- if deep:
- item = deepcopy(item, memo)
- y.append(item)
- if dictiter is not None:
- for key, value in dictiter:
- if deep:
- key = deepcopy(key, memo)
- value = deepcopy(value, memo)
- y[key] = value
+
if state:
if deep:
state = deepcopy(state, memo)
if slotstate is not None:
for key, value in slotstate.items():
setattr(y, key, value)
+
+ if listiter is not None:
+ for item in listiter:
+ if deep:
+ item = deepcopy(item, memo)
+ y.append(item)
+ if dictiter is not None:
+ for key, value in dictiter:
+ if deep:
+ key = deepcopy(key, memo)
+ value = deepcopy(value, memo)
+ y[key] = value
return y
del d
print(map(reprlib.repr, l1))
print(map(reprlib.repr, l2))
print(map(reprlib.repr, l3))
+ class odict(dict):
+ def __init__(self, d = {}):
+ self.a = 99
+ dict.__init__(self, d)
+ def __setitem__(self, k, i):
+ dict.__setitem__(self, k, i)
+ self.a
+ o = odict({"A" : "B"})
+ x = deepcopy(o)
+ print(o, x)
if __name__ == '__main__':
_test()
self.assertEqual(x.foo, y.foo)
self.assertTrue(x.foo is not y.foo)
+ def test_deepcopy_dict_subclass(self):
+ class C(dict):
+ def __init__(self, d=None):
+ if not d:
+ d = {}
+ self._keys = list(d.keys())
+ super().__init__(d)
+ def __setitem__(self, key, item):
+ super().__setitem__(key, item)
+ if key not in self._keys:
+ self._keys.append(key)
+ x = C(d={'foo':0})
+ y = copy.deepcopy(x)
+ self.assertEqual(x, y)
+ self.assertEqual(x._keys, y._keys)
+ self.assertTrue(x is not y)
+ x['bar'] = 1
+ self.assertNotEqual(x, y)
+ self.assertNotEqual(x._keys, y._keys)
+
def test_copy_list_subclass(self):
class C(list):
pass