Anonymous committed 0e6f0a2

Backport 1.70 and 1.71 (which really go together):

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__

Fiddle comments and variable names in whichmodule().

Comments (0)

Files changed (1)

-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:
         name = '__main__'
-    classmap[cls] = name
+    classmap[func] = name
     return name
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.