Commit a1c3a3a8 authored by Guido van Rossum's avatar Guido van Rossum

Apply the same change to classes without an __getinitargs__() method

as in pickle: the new instance is created without calling __init__().
parent 56dfc1e6
...@@ -46,7 +46,7 @@ any similar types. ...@@ -46,7 +46,7 @@ any similar types.
Classes can use the same interfaces to control copying that they use Classes can use the same interfaces to control copying that they use
to control pickling: they can define methods called __getinitargs__(), 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. "pickle" for information on these methods.
""" """
...@@ -107,9 +107,10 @@ def _copy_inst(x): ...@@ -107,9 +107,10 @@ def _copy_inst(x):
return x.__copy__() return x.__copy__()
if hasattr(x, '__getinitargs__'): if hasattr(x, '__getinitargs__'):
args = x.__getinitargs__() args = x.__getinitargs__()
else:
args = ()
y = apply(x.__class__, args) y = apply(x.__class__, args)
else:
y = _EmptyClass()
y.__class__ = x.__class__
if hasattr(x, '__getstate__'): if hasattr(x, '__getstate__'):
state = x.__getstate__() state = x.__getstate__()
else: else:
...@@ -219,9 +220,10 @@ def _deepcopy_inst(x, memo): ...@@ -219,9 +220,10 @@ def _deepcopy_inst(x, memo):
args = x.__getinitargs__() args = x.__getinitargs__()
_keep_alive(args, memo) _keep_alive(args, memo)
args = deepcopy(args, memo) args = deepcopy(args, memo)
else:
args = ()
y = apply(x.__class__, args) y = apply(x.__class__, args)
else:
y = _EmptyClass()
y.__class__ = x.__class__
memo[id(x)] = y memo[id(x)] = y
if hasattr(x, '__getstate__'): if hasattr(x, '__getstate__'):
state = x.__getstate__() state = x.__getstate__()
...@@ -240,6 +242,10 @@ del d ...@@ -240,6 +242,10 @@ del d
del types del types
# Helper for instance creation without calling __init__
class _EmptyClass:
pass
def _test(): def _test():
l = [None, 1, 2L, 3.14, 'xyzzy', (1, 2L), [3.14, 'abc'], l = [None, 1, 2L, 3.14, 'xyzzy', (1, 2L), [3.14, 'abc'],
{'abc': 'ABC'}, (), [], {}] {'abc': 'ABC'}, (), [], {}]
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment