[pypy-commit] pypy default: Simplify the custom logic to pickle module dictionary (as per Stackless

arigo noreply at buildbot.pypy.org
Wed Jun 6 11:27:00 CEST 2012


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r55426:2fc954d5e462
Date: 2012-06-06 11:26 +0200
http://bitbucket.org/pypy/pypy/changeset/2fc954d5e462/

Log:	Simplify the custom logic to pickle module dictionary (as per
	Stackless Python). Now it should only work with normal, imported,
	correctly-__name__ed modules, without needing to walk all of
	sys.modules.

diff --git a/lib-python/2.7/pickle.py b/lib-python/2.7/pickle.py
--- a/lib-python/2.7/pickle.py
+++ b/lib-python/2.7/pickle.py
@@ -638,7 +638,7 @@
             # else tmp is empty, and we're done
 
     def save_dict(self, obj):
-        modict_saver = self._pickle_moduledict(obj)
+        modict_saver = self._pickle_maybe_moduledict(obj)
         if modict_saver is not None:
             return self.save_reduce(*modict_saver)
 
@@ -691,26 +691,17 @@
                 write(SETITEM)
             # else tmp is empty, and we're done
 
-    def _pickle_moduledict(self, obj):
+    def _pickle_maybe_moduledict(self, obj):
         # save module dictionary as "getattr(module, '__dict__')"
+        try:
+            themodule = sys.modules[obj['__name__']]
+            if type(themodule) is not ModuleType:
+                return None
+            if themodule.__dict__ is not obj:
+                return None
+        except (AttributeError, KeyError, TypeError):
+            return None
 
-        # build index of module dictionaries
-        try:
-            modict = self.module_dict_ids
-        except AttributeError:
-            modict = {}
-            from sys import modules
-            for mod in modules.values():
-                if isinstance(mod, ModuleType):
-                    modict[id(mod.__dict__)] = mod
-            self.module_dict_ids = modict
-
-        thisid = id(obj)
-        try:
-            themodule = modict[thisid]
-        except KeyError:
-            return None
-        from __builtin__ import getattr
         return getattr, (themodule, '__dict__')
 
 


More information about the pypy-commit mailing list