summaryrefslogtreecommitdiffstats
path: root/Lib/copy.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1997-12-07 16:18:22 (GMT)
committerGuido van Rossum <guido@python.org>1997-12-07 16:18:22 (GMT)
commitc5d2d517000bf71c4afc1775c30d97c8d395e2b1 (patch)
treef28d7e9fa8bf5e5a809ac1a682373473c9b4cc23 /Lib/copy.py
parente907208b306a1b0882ae052176c72ff371475297 (diff)
downloadcpython-c5d2d517000bf71c4afc1775c30d97c8d395e2b1.zip
cpython-c5d2d517000bf71c4afc1775c30d97c8d395e2b1.tar.gz
cpython-c5d2d517000bf71c4afc1775c30d97c8d395e2b1.tar.bz2
Apply the same change to classes without an __getinitargs__() method
as in pickle: the new instance is created without calling __init__().
Diffstat (limited to 'Lib/copy.py')
-rw-r--r--Lib/copy.py16
1 files changed, 11 insertions, 5 deletions
diff --git a/Lib/copy.py b/Lib/copy.py
index 51c375d..1fbd481 100644
--- a/Lib/copy.py
+++ b/Lib/copy.py
@@ -46,7 +46,7 @@ any similar types.
Classes can use the same interfaces to control copying that they use
to control pickling: they can define methods called __getinitargs__(),
-__getstate__() and __setstate__(). See the __doc__ string of module
+__getstate__() and __setstate__(). See the documentation for module
"pickle" for information on these methods.
"""
@@ -107,9 +107,10 @@ def _copy_inst(x):
return x.__copy__()
if hasattr(x, '__getinitargs__'):
args = x.__getinitargs__()
+ y = apply(x.__class__, args)
else:
- args = ()
- y = apply(x.__class__, args)
+ y = _EmptyClass()
+ y.__class__ = x.__class__
if hasattr(x, '__getstate__'):
state = x.__getstate__()
else:
@@ -219,9 +220,10 @@ def _deepcopy_inst(x, memo):
args = x.__getinitargs__()
_keep_alive(args, memo)
args = deepcopy(args, memo)
+ y = apply(x.__class__, args)
else:
- args = ()
- y = apply(x.__class__, args)
+ y = _EmptyClass()
+ y.__class__ = x.__class__
memo[id(x)] = y
if hasattr(x, '__getstate__'):
state = x.__getstate__()
@@ -240,6 +242,10 @@ del d
del types
+# Helper for instance creation without calling __init__
+class _EmptyClass:
+ pass
+
def _test():
l = [None, 1, 2L, 3.14, 'xyzzy', (1, 2L), [3.14, 'abc'],
{'abc': 'ABC'}, (), [], {}]