]> granicus.if.org Git - python/commitdiff
Add the same _keep_alive patch (by Michael Scharff) that was added to
authorGuido van Rossum <guido@python.org>
Wed, 3 Sep 1997 00:23:54 +0000 (00:23 +0000)
committerGuido van Rossum <guido@python.org>
Wed, 3 Sep 1997 00:23:54 +0000 (00:23 +0000)
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.

Lib/pickle.py

index bc01c905a4cc2315d580323c7db7741eacf5658a..97eb4e45bc8def715cc7756f7cc22b78c04bd873 100644 (file)
@@ -473,6 +473,7 @@ class Pickler:
         if hasattr(object, '__getinitargs__'):
             args = object.__getinitargs__()
             len(args) # XXX Assert it's a sequence
+           _keep_alive(args, memo)
         else:
             args = ()
 
@@ -501,6 +502,7 @@ class Pickler:
             stuff = object.__dict__
         else:
             stuff = getstate()
+           _keep_alive(stuff, memo)
         save(stuff)
         write(BUILD)
     dispatch[InstanceType] = save_inst
@@ -523,6 +525,23 @@ class Pickler:
     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 = {}
 
 def whichmodule(cls, clsname):