[pypy-svn] r77592 - pypy/branch/fast-forward/lib-python/modified-2.7.0

afa at codespeak.net afa at codespeak.net
Tue Oct 5 08:58:01 CEST 2010


Author: afa
Date: Tue Oct  5 08:57:59 2010
New Revision: 77592

Modified:
   pypy/branch/fast-forward/lib-python/modified-2.7.0/pickle.py
Log:
PyPy can save references to module dictionaries.
Same code as in modified-2.5.2, except for a hack
needed by previous versions of the py library.

This fixes a test in pypy.interpreter.test.test_zzpickle_and_slow


Modified: pypy/branch/fast-forward/lib-python/modified-2.7.0/pickle.py
==============================================================================
--- pypy/branch/fast-forward/lib-python/modified-2.7.0/pickle.py	(original)
+++ pypy/branch/fast-forward/lib-python/modified-2.7.0/pickle.py	Tue Oct  5 08:57:59 2010
@@ -638,6 +638,10 @@
             # else tmp is empty, and we're done
 
     def save_dict(self, obj):
+        modict_saver = self._pickle_moduledict(obj)
+        if modict_saver is not None:
+            return self.save_reduce(*modict_saver)
+
         write = self.write
 
         if self.bin:
@@ -687,6 +691,29 @@
                 write(SETITEM)
             # else tmp is empty, and we're done
 
+    def _pickle_moduledict(self, obj):
+        # save module dictionary as "getattr(module, '__dict__')"
+
+        # 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__')
+
+
     def save_inst(self, obj):
         cls = obj.__class__
 



More information about the Pypy-commit mailing list