Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c5d2d51

Browse files
committed
Apply the same change to classes without an __getinitargs__() method
as in pickle: the new instance is created without calling __init__().
1 parent e907208 commit c5d2d51

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

Lib/copy.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class instances).
4646
4747
Classes can use the same interfaces to control copying that they use
4848
to control pickling: they can define methods called __getinitargs__(),
49-
__getstate__() and __setstate__(). See the __doc__ string of module
49+
__getstate__() and __setstate__(). See the documentation for module
5050
"pickle" for information on these methods.
5151
"""
5252

@@ -107,9 +107,10 @@ def _copy_inst(x):
107107
return x.__copy__()
108108
if hasattr(x, '__getinitargs__'):
109109
args = x.__getinitargs__()
110+
y = apply(x.__class__, args)
110111
else:
111-
args = ()
112-
y = apply(x.__class__, args)
112+
y = _EmptyClass()
113+
y.__class__ = x.__class__
113114
if hasattr(x, '__getstate__'):
114115
state = x.__getstate__()
115116
else:
@@ -219,9 +220,10 @@ def _deepcopy_inst(x, memo):
219220
args = x.__getinitargs__()
220221
_keep_alive(args, memo)
221222
args = deepcopy(args, memo)
223+
y = apply(x.__class__, args)
222224
else:
223-
args = ()
224-
y = apply(x.__class__, args)
225+
y = _EmptyClass()
226+
y.__class__ = x.__class__
225227
memo[id(x)] = y
226228
if hasattr(x, '__getstate__'):
227229
state = x.__getstate__()
@@ -240,6 +242,10 @@ def _deepcopy_inst(x, memo):
240242

241243
del types
242244

245+
# Helper for instance creation without calling __init__
246+
class _EmptyClass:
247+
pass
248+
243249
def _test():
244250
l = [None, 1, 2L, 3.14, 'xyzzy', (1, 2L), [3.14, 'abc'],
245251
{'abc': 'ABC'}, (), [], {}]

0 commit comments

Comments
 (0)