[pypy-svn] r22452 - in pypy/branch/arre-experiments/pypy: interpreter interpreter/astcompiler interpreter/test objspace/flow objspace/flow/test objspace/std tool/test

ac at codespeak.net ac at codespeak.net
Thu Jan 19 17:38:54 CET 2006


Author: ac
Date: Thu Jan 19 17:38:53 2006
New Revision: 22452

Modified:
   pypy/branch/arre-experiments/pypy/interpreter/astcompiler/pyassem.py
   pypy/branch/arre-experiments/pypy/interpreter/baseobjspace.py
   pypy/branch/arre-experiments/pypy/interpreter/gateway.py
   pypy/branch/arre-experiments/pypy/interpreter/pycode.py
   pypy/branch/arre-experiments/pypy/interpreter/pycompiler.py
   pypy/branch/arre-experiments/pypy/interpreter/test/test_eval.py
   pypy/branch/arre-experiments/pypy/interpreter/test/test_function.py
   pypy/branch/arre-experiments/pypy/objspace/flow/objspace.py
   pypy/branch/arre-experiments/pypy/objspace/flow/test/test_framestate.py
   pypy/branch/arre-experiments/pypy/objspace/std/marshal_impl.py
   pypy/branch/arre-experiments/pypy/tool/test/test_pytestsupport.py
Log:
Refactor construction of PyCode objects.

Modified: pypy/branch/arre-experiments/pypy/interpreter/astcompiler/pyassem.py
==============================================================================
--- pypy/branch/arre-experiments/pypy/interpreter/astcompiler/pyassem.py	(original)
+++ pypy/branch/arre-experiments/pypy/interpreter/astcompiler/pyassem.py	Thu Jan 19 17:38:53 2006
@@ -832,19 +832,18 @@
             argcount = argcount - 1
         # was return new.code, now we just return the parameters and let
         # the caller create the code object
-        # XXX _code_new_w itself is not really annotable
-        return PyCode(self.space)._code_new_w( argcount, nlocals,
-                                               self.stacksize, self.flags,
-                                               self.lnotab.getCode(),
-                                               self.getConsts(),
-                                               self.names,
-                                               self.varnames,
-                                               self.filename, self.name,
-                                               self.firstline,
-                                               self.lnotab.getTable(),
-                                               self.freevars,
-                                               self.cellvars
-                                               )
+        return PyCode( self.space, argcount, nlocals,
+                       self.stacksize, self.flags,
+                       self.lnotab.getCode(),
+                       self.getConsts(),
+                       self.names,
+                       self.varnames,
+                       self.filename, self.name,
+                       self.firstline,
+                       self.lnotab.getTable(),
+                       self.freevars,
+                       self.cellvars
+                       )
 
     def getConsts(self):
         """Return a tuple for the const slot of the code object

Modified: pypy/branch/arre-experiments/pypy/interpreter/baseobjspace.py
==============================================================================
--- pypy/branch/arre-experiments/pypy/interpreter/baseobjspace.py	(original)
+++ pypy/branch/arre-experiments/pypy/interpreter/baseobjspace.py	Thu Jan 19 17:38:53 2006
@@ -577,7 +577,7 @@
         if isinstance(expression, str):
             expression = compile(expression, '?', 'eval')
         if isinstance(expression, types.CodeType):
-            expression = PyCode(self)._from_code(expression)
+            expression = PyCode._from_code(self, expression)
         if not isinstance(expression, PyCode):
             raise TypeError, 'space.eval(): expected a string, code or PyCode object'
         return expression.exec_code(self, w_globals, w_locals)
@@ -589,7 +589,7 @@
         if isinstance(statement, str):
             statement = compile(statement, '?', 'exec')
         if isinstance(statement, types.CodeType):
-            statement = PyCode(self)._from_code(statement)
+            statement = PyCode._from_code(self, statement)
         if not isinstance(statement, PyCode):
             raise TypeError, 'space.exec_(): expected a string, code or PyCode object'
         w_key = self.wrap('__builtins__')

Modified: pypy/branch/arre-experiments/pypy/interpreter/gateway.py
==============================================================================
--- pypy/branch/arre-experiments/pypy/interpreter/gateway.py	(original)
+++ pypy/branch/arre-experiments/pypy/interpreter/gateway.py	Thu Jan 19 17:38:53 2006
@@ -694,7 +694,7 @@
 def build_applevel_dict(self, space):
     "NOT_RPYTHON"
     from pypy.interpreter.pycode import PyCode
-    pycode = PyCode(space)._from_code(self.code, hidden_applevel=self.hidden_applevel)
+    pycode = PyCode._from_code(space, self.code, hidden_applevel=self.hidden_applevel)
     w_glob = space.newdict([])
     space.setitem(w_glob, space.wrap('__name__'), space.wrap('__builtin__'))
     space.exec_(pycode, w_glob, w_glob)

Modified: pypy/branch/arre-experiments/pypy/interpreter/pycode.py
==============================================================================
--- pypy/branch/arre-experiments/pypy/interpreter/pycode.py	(original)
+++ pypy/branch/arre-experiments/pypy/interpreter/pycode.py	Thu Jan 19 17:38:53 2006
@@ -4,7 +4,8 @@
 The bytecode interpreter itself is implemented by the PyFrame class.
 """
 
-import dis
+import dis, imp, struct, types
+
 from pypy.interpreter import eval
 from pypy.interpreter.error import OperationError
 from pypy.interpreter.gateway import NoneNotWrapped 
@@ -45,6 +46,7 @@
         kwargname = None
     return argnames, varargname, kwargname
 
+cpython_magic, = struct.unpack("<i", imp.get_magic())
 
 NESTED    = 1
 GENERATOR = 2
@@ -58,7 +60,6 @@
     from pypy.interpreter.generator import GeneratorFrame
 
     def fresh_GeneratorFrame_methods():
-        import types
         from pypy.tool.sourcetools import func_with_new_name
         dic = GeneratorFrame.__dict__.copy()
         for n in dic:
@@ -80,131 +81,19 @@
 class PyCode(eval.Code):
     "CPython-style code objects."
 
-    magic = 62061 | 0x0a0d0000       # value for Python 2.4.1
-    
-    def __init__(self, space, co_name=''):
-        self.space = space
-        eval.Code.__init__(self, co_name)
-        self.co_argcount = 0         # #arguments, except *vararg and **kwarg
-        self.co_nlocals = 0          # #local variables
-        self.co_stacksize = 0        # #entries needed for evaluation stack
-        self.co_flags = 0            # CO_..., see above
-        self.co_code = None          # string: instruction opcodes
-        self.co_consts_w = []        # list of constants used (wrapped!)
-        self.co_names = []           # list of strings: names (for attrs..)
-        self.co_varnames = []        # list of strings: local variable names
-        self.co_freevars = []        # list of strings: free variable names
-        self.co_cellvars = []        # list of strings: cell variable names
-        # The rest doesn't count for hash/cmp
-        self.co_filename = ""        # string: where it was loaded from
-        #self.co_name (in base class)# string: name, for reference
-        self.co_firstlineno = 0      # first source line number
-        self.co_lnotab = ""          # string: encoding addr<->lineno mapping
-        self.hidden_applevel = False
-        self.do_fastcall = -1
-
-    def _code_new( self, argcount, nlocals, stacksize, flags,
-                   code, consts, names, varnames, filename,
-                   name, firstlineno, lnotab, freevars, cellvars,
-                   hidden_applevel=False, from_cpython=False):
-        """Initialize a new code objects from parameters from new.code"""
-        # simply try to suck in all attributes we know of
-        # with a lot of boring asserts to enforce type knowledge
-        # XXX get rid of that ASAP with a real compiler!
-        import types
-        x = argcount; assert isinstance(x, int)
-        self.co_argcount = x
-        x = nlocals; assert isinstance(x, int)
-        self.co_nlocals = x
-        x = stacksize; assert isinstance(x, int)
-        self.co_stacksize = x
-        x = flags; assert isinstance(x, int)
-        self.co_flags = x
-        x = code; assert isinstance(x, str)
-        self.co_code = x
-        #self.co_consts = <see below>
-        x = names; assert isinstance(x, tuple)
-        self.co_names = [ str(n) for n in x ]
-        x = varnames; assert isinstance(x, tuple)
-        self.co_varnames = [ str(n) for n in x ]
-        x = freevars; assert isinstance(x, tuple)
-        self.co_freevars = [ str(n) for n in x ]
-        x = cellvars; assert isinstance(x, tuple)
-        self.co_cellvars = [ str(n) for n in x ]
-        x = filename; assert isinstance(x, str)
-        self.co_filename = x
-        x = name; assert isinstance(x, str)
-        self.co_name = x
-        x = firstlineno; assert isinstance(x, int)
-        self.co_firstlineno = x
-        x = lnotab; assert isinstance(x, str)
-        self.co_lnotab = x
-        # recursively _from_code()-ify the code objects in code.co_consts
-        space = self.space
-        newconsts_w = []
-        for const in consts:
-            if isinstance(const, types.CodeType): # from stable compiler
-                const = PyCode(space)._from_code(const, hidden_applevel=hidden_applevel,
-                                                        from_cpython=from_cpython)
-            newconsts_w.append(space.wrap(const))
-        self.co_consts_w = newconsts_w
-        # stick the underlying CPython magic value, if the code object
-        # comes from there
-        if from_cpython:
-            import imp, struct
-            magic, = struct.unpack("<i", imp.get_magic())
-            if magic != self.magic:
-                self.magic = magic
-        self._compute_fastcall()
-        return self
-
-        
-    def _from_code(self, code, hidden_applevel=False, from_cpython=True):
-        """ Initialize the code object from a real (CPython) one.
-            This is just a hack, until we have our own compile.
-            At the moment, we just fake this.
-            This method is called by our compile builtin function.
-        """
-        self.hidden_applevel = hidden_applevel
-        import types
-        assert isinstance(code, types.CodeType)
-        self._code_new( code.co_argcount,
-                        code.co_nlocals,
-                        code.co_stacksize,
-                        code.co_flags,
-                        code.co_code,
-                        code.co_consts,
-                        code.co_names,
-                        code.co_varnames,
-                        code.co_filename,
-                        code.co_name,
-                        code.co_firstlineno,
-                        code.co_lnotab,
-                        code.co_freevars,
-                        code.co_cellvars,
-                        hidden_applevel,
-                        from_cpython )
-        return self
-
-
-
-    def _code_new_w( self, argcount, nlocals, stacksize, flags,
+    def __init__(self, space,  argcount, nlocals, stacksize, flags,
                      code, consts, names, varnames, filename,
                      name, firstlineno, lnotab, freevars, cellvars,
-                     hidden_applevel=False):
+                     hidden_applevel=False, magic = 62061 | 0x0a0d0000): # value for Python 2.4.1
         """Initialize a new code objects from parameters given by
         the pypy compiler"""
-        # simply try to suck in all attributes we know of
-        # with a lot of boring asserts to enforce type knowledge
-        # XXX get rid of that ASAP with a real compiler!
-        import types
+        self.space = space
+        eval.Code.__init__(self, name)
         self.co_argcount = argcount
         self.co_nlocals = nlocals
         self.co_stacksize = stacksize
         self.co_flags = flags
         self.co_code = code
-##         for w in consts:
-##             assert isinstance(w,W_Root)
         self.co_consts_w = consts
         self.co_names = names
         self.co_varnames = varnames
@@ -214,9 +103,59 @@
         self.co_name = name
         self.co_firstlineno = firstlineno
         self.co_lnotab = lnotab
+        self.hidden_applevel = hidden_applevel
+        self.magic = magic
         self._compute_fastcall()
-        return self
+        self._signature = cpython_code_signature(self)
+
+    def signature(self):
+        return self._signature
+    
+    def _from_code(space, code, hidden_applevel=False):
+        """ Initialize the code object from a real (CPython) one.
+            This is just a hack, until we have our own compile.
+            At the moment, we just fake this.
+            This method is called by our compile builtin function.
+        """
+        assert isinstance(code, types.CodeType)
+        newconsts_w = []
+        for const in code.co_consts:
+            if isinstance(const, types.CodeType): # from stable compiler
+                const = PyCode._from_code(space, const, hidden_applevel=hidden_applevel)
 
+            newconsts_w.append(space.wrap(const))
+        # stick the underlying CPython magic value, if the code object
+        # comes from there
+        return PyCode(space, code.co_argcount,
+                      code.co_nlocals,
+                      code.co_stacksize,
+                      code.co_flags,
+                      code.co_code,
+                      newconsts_w,
+                      list(code.co_names),
+                      list(code.co_varnames),
+                      code.co_filename,
+                      code.co_name,
+                      code.co_firstlineno,
+                      code.co_lnotab,
+                      list(code.co_freevars),
+                      list(code.co_cellvars),
+                      hidden_applevel, cpython_magic)
+
+    _from_code = staticmethod(_from_code)
+
+    def _code_new_w(space, argcount, nlocals, stacksize, flags,
+                    code, consts, names, varnames, filename,
+                    name, firstlineno, lnotab, freevars, cellvars,
+                    hidden_applevel=False):
+        """Initialize a new code objects from parameters given by
+        the pypy compiler"""
+        return PyCode(space, argcount, nlocals, stacksize, flags, code, consts,
+                      names, varnames, filename, name, firstlineno, lnotab,
+                      freevars, cellvars, hidden_applevel)
+
+    _code_new_w = staticmethod(_code_new_w)
+    
     def _compute_fastcall(self):
         # Speed hack!
         self.do_fastcall = -1
@@ -379,30 +318,26 @@
                           w_varnames, filename, name, firstlineno,
                           lnotab, w_freevars=NoneNotWrapped,
                           w_cellvars=NoneNotWrapped):
-        code = space.allocate_instance(PyCode, w_subtype)
-        PyCode.__init__(code, space)
         if argcount < 0:
             raise OperationError(space.w_ValueError,
                                  space.wrap("code: argcount must not be negative"))
-        code.co_argcount   = argcount
-        code.co_nlocals    = nlocals
         if nlocals < 0:
             raise OperationError(space.w_ValueError,
                                  space.wrap("code: nlocals must not be negative"))        
-        code.co_stacksize  = stacksize 
-        code.co_flags      = flags 
-        code.co_code       = codestring 
-        code.co_consts_w   = space.unpacktuple(w_constants)
-        code.co_names      = unpack_str_tuple(space, w_names)
-        code.co_varnames   = unpack_str_tuple(space, w_varnames)
-        code.co_filename   = filename 
-        code.co_name       = name 
-        code.co_firstlineno= firstlineno 
-        code.co_lnotab     = lnotab 
+        consts_w   = space.unpacktuple(w_constants)
+        names      = unpack_str_tuple(space, w_names)
+        varnames   = unpack_str_tuple(space, w_varnames)
         if w_freevars is not None:
-            code.co_freevars = unpack_str_tuple(space, w_freevars)
+            freevars = unpack_str_tuple(space, w_freevars)
+        else:
+            freevars = []
         if w_cellvars is not None:
-            code.co_cellvars = unpack_str_tuple(space, w_cellvars)
+            cellvars = unpack_str_tuple(space, w_cellvars)
+        else:
+            cellvars = []
+        code = space.allocate_instance(PyCode, w_subtype)
+        PyCode.__init__(code, space, argcount, nlocals, stacksize, flags, codestring, consts_w, names,
+                      varnames, filename, name, firstlineno, lnotab, freevars, cellvars)
         return space.wrap(code)
     descr_code__new__.unwrap_spec = unwrap_spec 
 

Modified: pypy/branch/arre-experiments/pypy/interpreter/pycompiler.py
==============================================================================
--- pypy/branch/arre-experiments/pypy/interpreter/pycompiler.py	(original)
+++ pypy/branch/arre-experiments/pypy/interpreter/pycompiler.py	Thu Jan 19 17:38:53 2006
@@ -142,7 +142,7 @@
         except TypeError, e:
             raise OperationError(space.w_TypeError, space.wrap(str(e)))
         from pypy.interpreter.pycode import PyCode
-        return PyCode(space)._from_code(c)
+        return PyCode._from_code(space, c)
     compile._annspecialcase_ = "override:cpy_compile"
 
     def _warn_explicit(self, message, category, filename, lineno,

Modified: pypy/branch/arre-experiments/pypy/interpreter/test/test_eval.py
==============================================================================
--- pypy/branch/arre-experiments/pypy/interpreter/test/test_eval.py	(original)
+++ pypy/branch/arre-experiments/pypy/interpreter/test/test_eval.py	Thu Jan 19 17:38:53 2006
@@ -8,7 +8,7 @@
     def setup_method(self, method):
         def c(x, y, *args):
             pass
-        code = PyCode(self.space)._from_code(c.func_code)
+        code = PyCode._from_code(self.space, c.func_code)
 
         class ConcreteFastscopeFrame(Frame):
             

Modified: pypy/branch/arre-experiments/pypy/interpreter/test/test_function.py
==============================================================================
--- pypy/branch/arre-experiments/pypy/interpreter/test/test_function.py	(original)
+++ pypy/branch/arre-experiments/pypy/interpreter/test/test_function.py	Thu Jan 19 17:38:53 2006
@@ -223,7 +223,7 @@
     def setup_method(self, method):
         def c(self, bar):
             return bar
-        code = PyCode(self.space)._from_code(c.func_code)
+        code = PyCode._from_code(self.space, c.func_code)
         self.fn = Function(self.space, code, self.space.newdict([]))
         
     def test_get(self):
@@ -250,7 +250,7 @@
         space = self.space
         # Create some function for this test only
         def m(self): return self
-        func = Function(space, PyCode(self.space)._from_code(m.func_code),
+        func = Function(space, PyCode._from_code(self.space, m.func_code),
                         space.newdict([]))
         # Some shorthands
         obj1 = space.wrap(23)

Modified: pypy/branch/arre-experiments/pypy/objspace/flow/objspace.py
==============================================================================
--- pypy/branch/arre-experiments/pypy/objspace/flow/objspace.py	(original)
+++ pypy/branch/arre-experiments/pypy/objspace/flow/objspace.py	Thu Jan 19 17:38:53 2006
@@ -225,7 +225,7 @@
         if func.func_doc and func.func_doc.lstrip().startswith('NOT_RPYTHON'):
             raise Exception, "%r is tagged as NOT_RPYTHON" % (func,)
         code = func.func_code
-        code = PyCode(self)._from_code(code)
+        code = PyCode._from_code(self, code)
         if func.func_closure is None:
             closure = None
         else:

Modified: pypy/branch/arre-experiments/pypy/objspace/flow/test/test_framestate.py
==============================================================================
--- pypy/branch/arre-experiments/pypy/objspace/flow/test/test_framestate.py	(original)
+++ pypy/branch/arre-experiments/pypy/objspace/flow/test/test_framestate.py	Thu Jan 19 17:38:53 2006
@@ -18,7 +18,7 @@
         except AttributeError:
             pass
         code = func.func_code
-        code = PyCode(self.space)._from_code(code)
+        code = PyCode._from_code(self.space, code)
         w_globals = Constant({}) # space.newdict([])
         frame = code.create_frame(space, w_globals)
 

Modified: pypy/branch/arre-experiments/pypy/objspace/std/marshal_impl.py
==============================================================================
--- pypy/branch/arre-experiments/pypy/objspace/std/marshal_impl.py	(original)
+++ pypy/branch/arre-experiments/pypy/objspace/std/marshal_impl.py	Thu Jan 19 17:38:53 2006
@@ -410,22 +410,24 @@
     return res
 
 def unmarshal_pycode(space, u, tc):
-    code = PyCode(space)
-    code.co_argcount    = u.get_int()
-    code.co_nlocals     = u.get_int()
-    code.co_stacksize   = u.get_int()
-    code.co_flags       = u.get_int()
-    code.co_code        = unmarshal_str(u)
+    argcount    = u.get_int()
+    nlocals     = u.get_int()
+    stacksize   = u.get_int()
+    flags       = u.get_int()
+    code        = unmarshal_str(u)
     u.start(TYPE_TUPLE)
-    code.co_consts_w    = u.get_list_w()
-    code.co_names       = unmarshal_strlist(u, TYPE_TUPLE)
-    code.co_varnames    = unmarshal_strlist(u, TYPE_TUPLE)
-    code.co_freevars    = unmarshal_strlist(u, TYPE_TUPLE)
-    code.co_cellvars    = unmarshal_strlist(u, TYPE_TUPLE)
-    code.co_filename    = unmarshal_str(u)
-    code.co_name        = unmarshal_str(u)
-    code.co_firstlineno = u.get_int()
-    code.co_lnotab      = unmarshal_str(u)
+    consts_w    = u.get_list_w()
+    names       = unmarshal_strlist(u, TYPE_TUPLE)
+    varnames    = unmarshal_strlist(u, TYPE_TUPLE)
+    freevars    = unmarshal_strlist(u, TYPE_TUPLE)
+    cellvars    = unmarshal_strlist(u, TYPE_TUPLE)
+    filename    = unmarshal_str(u)
+    name        = unmarshal_str(u)
+    firstlineno = u.get_int()
+    lnotab      = unmarshal_str(u)
+    code = PyCode._code_new_w(space, argcount, nlocals, stacksize, flags,
+                              code, consts_w, names, varnames, filename,
+                              name, firstlineno, lnotab, freevars, cellvars)
     return space.wrap(code)
 register(TYPE_CODE, unmarshal_pycode)
 

Modified: pypy/branch/arre-experiments/pypy/tool/test/test_pytestsupport.py
==============================================================================
--- pypy/branch/arre-experiments/pypy/tool/test/test_pytestsupport.py	(original)
+++ pypy/branch/arre-experiments/pypy/tool/test/test_pytestsupport.py	Thu Jan 19 17:38:53 2006
@@ -13,7 +13,7 @@
 
 def test_AppFrame(space):
     import sys
-    co = PyCode(space)._from_code(somefunc.func_code)
+    co = PyCode._from_code(space, somefunc.func_code)
     pyframe = PyFrame(space, co, space.newdict([]), None)
     runner = AppFrame(pyframe)
     exprinfo.run("f = lambda x: x+1", runner)



More information about the Pypy-commit mailing list