[pypy-svn] r64146 - in pypy/branch/wip-fix-stackless-O2-pickling/pypy: interpreter module/_pickle_support module/_stackless/test

cfbolz at codespeak.net cfbolz at codespeak.net
Thu Apr 16 12:11:43 CEST 2009


Author: cfbolz
Date: Thu Apr 16 12:11:43 2009
New Revision: 64146

Modified:
   pypy/branch/wip-fix-stackless-O2-pickling/pypy/interpreter/function.py
   pypy/branch/wip-fix-stackless-O2-pickling/pypy/interpreter/gateway.py
   pypy/branch/wip-fix-stackless-O2-pickling/pypy/module/_pickle_support/__init__.py
   pypy/branch/wip-fix-stackless-O2-pickling/pypy/module/_pickle_support/maker.py
   pypy/branch/wip-fix-stackless-O2-pickling/pypy/module/_stackless/test/test_pickle_infrastructure.py
Log:
(pedronis, cfbolz): fix pickling of functions that have builtin code objects
attached to them.


Modified: pypy/branch/wip-fix-stackless-O2-pickling/pypy/interpreter/function.py
==============================================================================
--- pypy/branch/wip-fix-stackless-O2-pickling/pypy/interpreter/function.py	(original)
+++ pypy/branch/wip-fix-stackless-O2-pickling/pypy/interpreter/function.py	Thu Apr 16 12:11:43 2009
@@ -195,10 +195,37 @@
     def descr_function_repr(self):
         return self.getrepr(self.space, 'function %s' % (self.name,))
 
+
+    # delicate   
+    _all = {'': None}
+
+    def _freeze_(self):
+        from pypy.interpreter.gateway import BuiltinCode
+        if isinstance(self.code, BuiltinCode):
+            identifier = self.code.identifier
+            if Function._all.get(identifier, self) is not self:
+                print "builtin code identifier %s used twice: %s and %s" % (
+                    identifier, self, Function._all[identifier])
+            # we have been seen by other means so rtyping should not choke
+            # on us
+            Function._all[identifier] = self
+        return False
+
+    def find(identifier):
+        return Function._all[identifier]
+    find = staticmethod(find)
+
     def descr_function__reduce__(self, space):
+        from pypy.interpreter.gateway import BuiltinCode
         from pypy.interpreter.mixedmodule import MixedModule
         w_mod    = space.getbuiltinmodule('_pickle_support')
         mod      = space.interp_w(MixedModule, w_mod)
+        code = self.code
+        if isinstance(code, BuiltinCode):
+            new_inst = mod.get('builtin_function')
+            return space.newtuple([new_inst,
+                                   space.newtuple([space.wrap(code.identifier)])])
+            
         new_inst = mod.get('func_new')
         w        = space.wrap
         if self.closure is None:

Modified: pypy/branch/wip-fix-stackless-O2-pickling/pypy/interpreter/gateway.py
==============================================================================
--- pypy/branch/wip-fix-stackless-O2-pickling/pypy/interpreter/gateway.py	(original)
+++ pypy/branch/wip-fix-stackless-O2-pickling/pypy/interpreter/gateway.py	Thu Apr 16 12:11:43 2009
@@ -490,20 +490,9 @@
         return space.newtuple([builtin_code,
                                space.newtuple([space.wrap(self.identifier)])])
 
-    # delicate   
-    _all = {'': None}
-
-    def _freeze_(self):
-        if BuiltinCode._all.get(self.identifier, self) is not self:
-            print "builtin code identifier %s used twice: %s and %s" % (
-                self.identifier, self, BuiltinCode._all[self.identifier])
-        # we have been seen by other means so rtyping should not choke
-        # on us
-        BuiltinCode._all[self.identifier] = self
-        return False
-
     def find(indentifier):
-        return BuiltinCode._all[indentifier]
+        from pypy.interpreter.function import Function
+        return Function._all[indentifier].code
     find = staticmethod(find)
 
     def signature(self):
@@ -799,9 +788,9 @@
         space = cache.space
         defs = gateway._getdefaults(space) # needs to be implemented by subclass
         code = gateway._code
-        if not space.config.translating: # for tests and py.py
-            code._freeze_()
         fn = Function(space, code, None, defs, forcename = gateway.name)
+        if not space.config.translating: # for tests and py.py
+            fn._freeze_()
         if gateway.as_classmethod:
             fn = ClassMethod(space.wrap(fn))
         return fn

Modified: pypy/branch/wip-fix-stackless-O2-pickling/pypy/module/_pickle_support/__init__.py
==============================================================================
--- pypy/branch/wip-fix-stackless-O2-pickling/pypy/module/_pickle_support/__init__.py	(original)
+++ pypy/branch/wip-fix-stackless-O2-pickling/pypy/module/_pickle_support/__init__.py	Thu Apr 16 12:11:43 2009
@@ -20,5 +20,6 @@
         'traceback_new' : 'maker.traceback_new',
         'generator_new' : 'maker.generator_new',
         'xrangeiter_new': 'maker.xrangeiter_new',
-        'builtin_code': 'maker.builtin_code'
+        'builtin_code': 'maker.builtin_code',
+        'builtin_function' : 'maker.builtin_function',
     }

Modified: pypy/branch/wip-fix-stackless-O2-pickling/pypy/module/_pickle_support/maker.py
==============================================================================
--- pypy/branch/wip-fix-stackless-O2-pickling/pypy/module/_pickle_support/maker.py	(original)
+++ pypy/branch/wip-fix-stackless-O2-pickling/pypy/module/_pickle_support/maker.py	Thu Apr 16 12:11:43 2009
@@ -89,6 +89,15 @@
                                         identifier))
 builtin_code.unwrap_spec = [ObjSpace, str]
         
+def builtin_function(space, identifier):
+    from pypy.interpreter import function
+    try:
+        return function.Function.find(identifier)
+    except KeyError:
+        raise OperationError(space.w_RuntimeError, 
+                             space.wrap("cannot unpickle builtin function: "+
+                                        identifier))
+builtin_function.unwrap_spec = [ObjSpace, str]
 
 
 # ___________________________________________________________________

Modified: pypy/branch/wip-fix-stackless-O2-pickling/pypy/module/_stackless/test/test_pickle_infrastructure.py
==============================================================================
--- pypy/branch/wip-fix-stackless-O2-pickling/pypy/module/_stackless/test/test_pickle_infrastructure.py	(original)
+++ pypy/branch/wip-fix-stackless-O2-pickling/pypy/module/_stackless/test/test_pickle_infrastructure.py	Thu Apr 16 12:11:43 2009
@@ -15,10 +15,19 @@
         dump = pickle.dumps(sw)
         res = pickle.loads(dump)
 
-        # xxx identity preservation for the function would be better
+        assert res is sw
         assert res.func_code is sw.func_code
         assert res.func_doc is sw.func_doc
         assert res.func_globals is sw.func_globals
+
+    def test_pickle_switch_function_code(object):
+        import _stackless, pickle
+
+        sw = _stackless.coroutine.switch.im_func.func_code
+        dump = pickle.dumps(sw)
+        res = pickle.loads(dump)
+
+        assert res is sw
         
 class AppTestPicklePrerequisites(BaseAppTestPicklePrerequisites):
     pass



More information about the Pypy-commit mailing list