Address SF #451547. The approach is a bit draconian: any object that
authorGuido van Rossum <guido@python.org>
Fri, 17 Aug 2001 18:49:52 +0000 (18:49 +0000)
committerGuido van Rossum <guido@python.org>
Fri, 17 Aug 2001 18:49:52 +0000 (18:49 +0000)
is pickled as a global must now exist by the name under which it is
pickled, otherwise the pickling fails.  Previously, such things would
fail on unpickling, or unpickle as the wrong global object.  I'm
hoping that this won't break existing code that is playing tricks with
this.

I need a volunteer to do this for cPickle too.

Lib/pickle.py

index c92dac276c6247e9a98dd7f3a89fec24018d5ed0..8be7a8d362120ba595ca84d78f62ee2a65358569 100644 (file)
@@ -497,6 +497,20 @@ class Pickler:
         except AttributeError:
             module = whichmodule(object, name)
 
+        try:
+            __import__(module)
+            mod = sys.modules[module]
+            klass = getattr(mod, name)
+        except (ImportError, KeyError, AttributeError):
+            raise PicklingError(
+                "Can't pickle %r: it's not found as %s.%s" %
+                (object, module, name))
+        else:
+            if klass is not object:
+                raise PicklingError(
+                    "Can't pickle %r: it's not the same object as %s.%s" %
+                    (object, module, name))
+
         memo_len = len(memo)
         write(GLOBAL + module + '\n' + name + '\n' +
             self.put(memo_len))