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

pedronis at codespeak.net pedronis at codespeak.net
Wed Apr 8 18:43:53 CEST 2009


Author: pedronis
Date: Wed Apr  8 18:43:53 2009
New Revision: 63866

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/interpreter/typedef.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:
my current bunch of changes, mostly started supporting pickling of builtin code objects



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	Wed Apr  8 18:43:53 2009
@@ -229,7 +229,7 @@
         self.space = space
         self.name = space.str_w(w_name)
         self.w_doc = w_doc
-        self.code = space.interp_w(PyCode, w_code)
+        self.code = space.interp_w(Code, w_code)
         self.w_func_globals = w_func_globals
         if w_closure is not space.w_None:
             from pypy.interpreter.nestedscope import Cell

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	Wed Apr  8 18:43:53 2009
@@ -405,6 +405,10 @@
         eval.Code.__init__(self, func.__name__)
         self.docstring = func.__doc__
 
+        # xxx not unique right now
+        self.identifier = "%s-%s-%s" % (func.__module__, func.__name__,
+                                        getattr(self_type, '__name__', '*'))
+
         # unwrap_spec can be passed to interp2app or
         # attached as an attribute to the function.
         # It is a list of types or singleton objects:
@@ -479,6 +483,27 @@
                 self.__class__ = globals()['BuiltinCode%d' % arity]
                 setattr(self, 'fastfunc_%d' % arity, fastfunc)
 
+    def descr__reduce__(self, space):
+        from pypy.interpreter.mixedmodule import MixedModule
+        w_mod    = space.getbuiltinmodule('_pickle_support')
+        mod      = space.interp_w(MixedModule, w_mod)
+        builtin_code = mod.get('builtin_code')
+        return space.newtuple([builtin_code,
+                               space.newtuple([space.wrap(self.identifier)])])
+
+    # delicate   
+    _all = {'': None}
+
+    def _freeze_(self):
+        # 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]
+    find = staticmethod(find)
+
     def signature(self):
         return self.sig
 
@@ -757,6 +782,8 @@
         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 gateway.as_classmethod:
             fn = ClassMethod(space.wrap(fn))

Modified: pypy/branch/wip-fix-stackless-O2-pickling/pypy/interpreter/typedef.py
==============================================================================
--- pypy/branch/wip-fix-stackless-O2-pickling/pypy/interpreter/typedef.py	(original)
+++ pypy/branch/wip-fix-stackless-O2-pickling/pypy/interpreter/typedef.py	Wed Apr  8 18:43:53 2009
@@ -3,7 +3,7 @@
 
 """
 import py
-from pypy.interpreter.gateway import interp2app
+from pypy.interpreter.gateway import interp2app, BuiltinCode
 from pypy.interpreter.argument import Arguments
 from pypy.interpreter.baseobjspace import Wrappable, W_Root, ObjSpace, \
     DescrMismatch
@@ -654,6 +654,18 @@
     )
 Code.typedef.acceptable_as_base_class = False
 
+BuiltinCode.typedef = TypeDef('builtin-code',
+    __reduce__   = interp2app(BuiltinCode.descr__reduce__,
+                              unwrap_spec=['self', ObjSpace]),
+    co_name = interp_attrproperty('co_name', cls=BuiltinCode),
+    co_varnames = GetSetProperty(fget_co_varnames, cls=BuiltinCode),
+    co_argcount = GetSetProperty(fget_co_argcount, cls=BuiltinCode),
+    co_flags = GetSetProperty(fget_co_flags, cls=BuiltinCode),
+    co_consts = GetSetProperty(fget_co_consts, cls=BuiltinCode),
+    )
+BuiltinCode.typedef.acceptable_as_base_class = False
+
+
 Frame.typedef = TypeDef('internal-frame',
     f_code = GetSetProperty(Frame.fget_code),
     f_locals = GetSetProperty(Frame.fget_getdictscope),

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	Wed Apr  8 18:43:53 2009
@@ -20,4 +20,5 @@
         'traceback_new' : 'maker.traceback_new',
         'generator_new' : 'maker.generator_new',
         'xrangeiter_new': 'maker.xrangeiter_new',
+        'builtin_code': 'maker.builtin_code'
     }

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	Wed Apr  8 18:43:53 2009
@@ -1,3 +1,4 @@
+from pypy.interpreter.error import OperationError 
 from pypy.interpreter.nestedscope import Cell
 from pypy.interpreter.pycode import PyCode
 from pypy.interpreter.function import Function, Method
@@ -78,6 +79,18 @@
     return space.wrap(new_iter)
 xrangeiter_new.unwrap_spec = [ObjSpace, int, int, int]
 
+def builtin_code(space, identifier):
+    from pypy.interpreter import gateway
+    try:
+        return gateway.BuiltinCode.find(identifier)
+    except KeyError:
+        raise OperationError(space.w_RuntimeError, 
+                             space.wrap("cannot unpickle builtin code: "+
+                                        identifier))
+builtin_code.unwrap_spec = [ObjSpace, str]
+        
+
+
 # ___________________________________________________________________
 # Helper functions for internal use
 

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	Wed Apr  8 18:43:53 2009
@@ -1,6 +1,23 @@
 from pypy.conftest import gettestobjspace
 from py.test import skip
 
+
+class AppTestPicklePrerequisites(object):
+    def setup_class(cls):
+        space = gettestobjspace(usemodules=('_stackless',))
+        cls.space = space
+
+    def test_pickle_switch_function(object):
+        import _stackless, pickle
+
+        sw = _stackless.coroutine.switch.im_func
+        dump = pickle.dumps(sw)
+        res = pickle.loads(dump)
+
+        # xxx identity preservation for the function would be better
+        assert res.func_code == sw.func_code
+        
+
 class FrameCheck(object):
 
     def __init__(self, name):
@@ -42,8 +59,11 @@
 
         opmap = space.unwrap(w_opmap)
         cls.CALL_FUNCTION = opmap['CALL_FUNCTION']
-        cls.CALL_FUNCTION_VAR = opmap['CALL_FUNCTION_VAR']        
-        
+        cls.CALL_FUNCTION_VAR = opmap['CALL_FUNCTION_VAR']
+        cls.CALL_METHOD = opmap['CALL_METHOD']
+
+        cls.callmethod = getattr(cls, cls.callmethod_label)
+
     def teardown_class(cls):
         from pypy.rlib import rstack
         rstack.resume_state_create = cls.old_resume_state_create
@@ -115,8 +135,8 @@
         e('dispatch', FrameCheck('g'), gcode, ec)
         e('handle_bytecode', FrameCheck('g'), gcode, ec)
         e('dispatch_call', FrameCheck('g'), gcode,
-          BytecodeCheck(gcode, self.CALL_FUNCTION, 0), ec)
-        e('CALL_FUNCTION', FrameCheck('g'), 0)
+          BytecodeCheck(gcode, self.callmethod, 0), ec)
+        e(self.callmethod_label, FrameCheck('g'), 0)
         e('w_switch', w_co.costate, space)
         e('coroutine_switch', w_co.costate)
         self.end()
@@ -174,8 +194,8 @@
         e('dispatch', FrameCheck('g'), gcode, ec)
         e('handle_bytecode', FrameCheck('g'), gcode, ec)
         e('dispatch_call', FrameCheck('g'), gcode,
-          BytecodeCheck(gcode, self.CALL_FUNCTION, 0), ec)
-        e('CALL_FUNCTION', FrameCheck('g'), 0)
+          BytecodeCheck(gcode, self.callmethod, 0), ec)
+        e(self.callmethod_label, FrameCheck('g'), 0)
         e('w_switch', w_co.costate, space)
         e('coroutine_switch', w_co.costate)
         self.end()        
@@ -239,26 +259,26 @@
         e('dispatch', FrameCheck('f'), fcode, ec)
         e('handle_bytecode', FrameCheck('f'), fcode, ec)
         e('dispatch_call', FrameCheck('f'), fcode,
-          BytecodeCheck(fcode, self.CALL_FUNCTION, 1), ec)
-        e('CALL_FUNCTION', FrameCheck('f'), 1)
+          BytecodeCheck(fcode, self.callmethod, 1), ec)
+        e(self.callmethod_label, FrameCheck('f'), 1)
         # g
         e('execute_frame', FrameCheck('g'), ec)
         e('dispatch', FrameCheck('g'), gcode, ec)
         e('handle_bytecode', FrameCheck('g'), gcode, ec)
         e('dispatch_call', FrameCheck('g'), gcode,
-          BytecodeCheck(gcode, self.CALL_FUNCTION, 0), ec)
-        e('CALL_FUNCTION', FrameCheck('g'), 0)
+          BytecodeCheck(gcode, self.callmethod, 0), ec)
+        e(self.callmethod_label, FrameCheck('g'), 0)
         e('w_switch', w_co.costate, space)
         e('coroutine_switch', w_co.costate)
         self.end()
 
 class TestReconstructFrameChain(BaseTestReconstructFrameChain):
-    pass
+    callmethod_label = 'CALL_FUNCTION'
 
 class TestReconstructFrameChain_CALL_METHOD(BaseTestReconstructFrameChain):
-    OPTIONS = {"objspace.opcodes.CALL_METHOD": True}
+    OPTIONS = {"objspace.opcodes.CALL_METHOD": True,
+               }
 
-    def setup_class(cls):
-        skip("this needs special casing in Function reduce for BuiltinCodes like in Method as first thing")
+    callmethod_label = 'CALL_METHOD'
 
                 



More information about the Pypy-commit mailing list