]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Backport 1.70 and 1.71 (which really go together):
authorGuido van Rossum <guido@python.org>
Mon, 7 Oct 2002 13:09:25 +0000 (13:09 +0000)
committerGuido van Rossum <guido@python.org>
Mon, 7 Oct 2002 13:09:25 +0000 (13:09 +0000)
1.70:
whichmodule() should skip dummy package entries in sys.modules.

This fixes the charming, but unhelpful error message for
>>> pickle.dumps(type.__new__)
Can't pickle <built-in method __new__ of type object at 0x812a440>: it's not the same object as datetime.math.__new__

1.71:
Fiddle comments and variable names in whichmodule().

Lib/pickle.py

index 38bda79fc566a62c53ba448e74c1e37d48b9193b..7f26385412925db2a745cca22c64166d0132dd75 100644 (file)
@@ -552,28 +552,29 @@ def _keep_alive(x, memo):
         memo[id(memo)]=[x]
 
 
-classmap = {}
+classmap = {} # called classmap for backwards compatibility
 
-# This is no longer used to find classes, but still for functions
-def whichmodule(cls, clsname):
-    """Figure out the module in which a class occurs.
+def whichmodule(func, funcname):
+    """Figure out the module in which a function occurs.
 
     Search sys.modules for the module.
     Cache in classmap.
     Return a module name.
-    If the class cannot be found, return __main__.
+    If the function cannot be found, return __main__.
     """
-    if classmap.has_key(cls):
-        return classmap[cls]
+    if classmap.has_key(classmap):
+        return classmap[func]
 
     for name, module in sys.modules.items():
+        if module is None:
+            continue # skip dummy package entries
         if name != '__main__' and \
-            hasattr(module, clsname) and \
-            getattr(module, clsname) is cls:
+            hasattr(module, funcname) and \
+            getattr(module, funcname) is func:
             break
     else:
         name = '__main__'
-    classmap[cls] = name
+    classmap[func] = name
     return name