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

Skip to content

Commit e6eef4b

Browse files
committed
Use __dict__.update(state) instead of for loop over state.items() and
call to setattr(). This changes semantics, following the change already implemented in pickle. Also reindented a few lines properly.
1 parent 040e565 commit e6eef4b

1 file changed

Lines changed: 9 additions & 11 deletions

File tree

Lib/copy.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,13 @@ def _copy_inst(x):
111111
args = ()
112112
y = apply(x.__class__, args)
113113
if hasattr(x, '__getstate__'):
114-
state = x.__getstate__()
114+
state = x.__getstate__()
115115
else:
116-
state = x.__dict__
116+
state = x.__dict__
117117
if hasattr(y, '__setstate__'):
118-
y.__setstate__(state)
118+
y.__setstate__(state)
119119
else:
120-
for key in state.keys():
121-
setattr(y, key, state[key])
120+
y.__dict__.update(state)
122121
return y
123122
d[types.InstanceType] = _copy_inst
124123

@@ -225,16 +224,15 @@ def _deepcopy_inst(x, memo):
225224
y = apply(x.__class__, args)
226225
memo[id(x)] = y
227226
if hasattr(x, '__getstate__'):
228-
state = x.__getstate__()
229-
_keep_alive(state, memo)
227+
state = x.__getstate__()
228+
_keep_alive(state, memo)
230229
else:
231-
state = x.__dict__
230+
state = x.__dict__
232231
state = deepcopy(state, memo)
233232
if hasattr(y, '__setstate__'):
234-
y.__setstate__(state)
233+
y.__setstate__(state)
235234
else:
236-
for key in state.keys():
237-
setattr(y, key, state[key])
235+
y.__dict__.update(state)
238236
return y
239237
d[types.InstanceType] = _deepcopy_inst
240238

0 commit comments

Comments
 (0)