mirror of
https://github.com/python/cpython.git
synced 2025-10-03 13:45:29 +00:00
Add the same _keep_alive patch (by Michael Scharff) that was added to
copy.deepcopy() a while ago. Can't reproduce this but it doesn't break anything and it looks like the code could have the same problem.
This commit is contained in:
parent
15a40394b0
commit
5ed5c4c364
1 changed files with 19 additions and 0 deletions
|
@ -473,6 +473,7 @@ class Pickler:
|
||||||
if hasattr(object, '__getinitargs__'):
|
if hasattr(object, '__getinitargs__'):
|
||||||
args = object.__getinitargs__()
|
args = object.__getinitargs__()
|
||||||
len(args) # XXX Assert it's a sequence
|
len(args) # XXX Assert it's a sequence
|
||||||
|
_keep_alive(args, memo)
|
||||||
else:
|
else:
|
||||||
args = ()
|
args = ()
|
||||||
|
|
||||||
|
@ -501,6 +502,7 @@ class Pickler:
|
||||||
stuff = object.__dict__
|
stuff = object.__dict__
|
||||||
else:
|
else:
|
||||||
stuff = getstate()
|
stuff = getstate()
|
||||||
|
_keep_alive(stuff, memo)
|
||||||
save(stuff)
|
save(stuff)
|
||||||
write(BUILD)
|
write(BUILD)
|
||||||
dispatch[InstanceType] = save_inst
|
dispatch[InstanceType] = save_inst
|
||||||
|
@ -523,6 +525,23 @@ class Pickler:
|
||||||
dispatch[BuiltinFunctionType] = save_global
|
dispatch[BuiltinFunctionType] = save_global
|
||||||
|
|
||||||
|
|
||||||
|
def _keep_alive(x, memo):
|
||||||
|
"""Keeps a reference to the object x in the memo.
|
||||||
|
|
||||||
|
Because we remember objects by their id, we have
|
||||||
|
to assure that possibly temporary objects are kept
|
||||||
|
alive by referencing them.
|
||||||
|
We store a reference at the id of the memo, which should
|
||||||
|
normally not be used unless someone tries to deepcopy
|
||||||
|
the memo itself...
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
memo[id(memo)].append(x)
|
||||||
|
except KeyError:
|
||||||
|
# aha, this is the first one :-)
|
||||||
|
memo[id(memo)]=[x]
|
||||||
|
|
||||||
|
|
||||||
classmap = {}
|
classmap = {}
|
||||||
|
|
||||||
def whichmodule(cls, clsname):
|
def whichmodule(cls, clsname):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue