[pypy-svn] r16309 - in pypy/dist/pypy/interpreter: . pyparser/test

ludal at codespeak.net ludal at codespeak.net
Tue Aug 23 18:26:53 CEST 2005


Author: ludal
Date: Tue Aug 23 18:26:52 2005
New Revision: 16309

Modified:
   pypy/dist/pypy/interpreter/pycode.py
   pypy/dist/pypy/interpreter/pycompiler.py
   pypy/dist/pypy/interpreter/pyparser/test/test_astcompiler.py
Log:
 added a method to create new code objects from parameters


Modified: pypy/dist/pypy/interpreter/pycode.py
==============================================================================
--- pypy/dist/pypy/interpreter/pycode.py	(original)
+++ pypy/dist/pypy/interpreter/pycode.py	Tue Aug 23 18:26:52 2005
@@ -101,55 +101,79 @@
         self.co_lnotab = ""          # string: encoding addr<->lineno mapping
         self.hidden_applevel = False
 
-    def _from_code(self, 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.
-        """
-        self.hidden_applevel = hidden_applevel
-        import types
-        assert isinstance(code, types.CodeType)
+
+    def _code_new( self, argcount, nlocals, stacksize, flags,
+                   code, consts, names, varnames, filename,
+                   name, firstlineno, lnotab, freevars, cellvars,
+                   hidden_applevel=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!
-        x = code.co_argcount; assert isinstance(x, int)
+        import types
+        x = argcount; assert isinstance(x, int)
         self.co_argcount = x
-        x = code.co_nlocals; assert isinstance(x, int)
+        x = nlocals; assert isinstance(x, int)
         self.co_nlocals = x
-        x = code.co_stacksize; assert isinstance(x, int)
+        x = stacksize; assert isinstance(x, int)
         self.co_stacksize = x
-        x = code.co_flags; assert isinstance(x, int)
+        x = flags; assert isinstance(x, int)
         self.co_flags = x
-        x = code.co_code; assert isinstance(x, str)
+        x = code; assert isinstance(x, str)
         self.co_code = x
         #self.co_consts = <see below>
-        x = code.co_names; assert isinstance(x, tuple)
+        x = names; assert isinstance(x, tuple)
         self.co_names = [ str(n) for n in x ]
-        x = code.co_varnames; assert isinstance(x, tuple)
+        x = varnames; assert isinstance(x, tuple)
         self.co_varnames = [ str(n) for n in x ]
-        x = code.co_freevars; assert isinstance(x, tuple)
+        x = freevars; assert isinstance(x, tuple)
         self.co_freevars = [ str(n) for n in x ]
-        x = code.co_cellvars; assert isinstance(x, tuple)
+        x = cellvars; assert isinstance(x, tuple)
         self.co_cellvars = [ str(n) for n in x ]
-        x = code.co_filename; assert isinstance(x, str)
+        x = filename; assert isinstance(x, str)
         self.co_filename = x
-        x = code.co_name; assert isinstance(x, str)
+        x = name; assert isinstance(x, str)
         self.co_name = x
-        x = code.co_firstlineno; assert isinstance(x, int)
+        x = firstlineno; assert isinstance(x, int)
         self.co_firstlineno = x
-        x = code.co_lnotab; assert isinstance(x, str)
+        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 code.co_consts:
+        for const in consts:
             if isinstance(const, types.CodeType):
                 const = PyCode(space)._from_code(const, hidden_applevel=hidden_applevel)
             newconsts_w.append(space.wrap(const))
         self.co_consts_w = newconsts_w
         return self
 
+    def _from_code(self, 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.
+        """
+        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 )
+        return self
+
     def create_frame(self, space, w_globals, closure=None):
         "Create an empty PyFrame suitable for this code object."
         # select the appropriate kind of frame
@@ -235,6 +259,8 @@
                           W_Root, str, str, int, 
                           str, W_Root, 
                           W_Root]
+
+
     def descr_code__new__(space, w_subtype,
                           argcount, nlocals, stacksize, flags,
                           codestring, w_constants, w_names,

Modified: pypy/dist/pypy/interpreter/pycompiler.py
==============================================================================
--- pypy/dist/pypy/interpreter/pycompiler.py	(original)
+++ pypy/dist/pypy/interpreter/pycompiler.py	Tue Aug 23 18:26:52 2005
@@ -345,7 +345,23 @@
             raise OperationError(space.w_TypeError,space.wrap(str(e)))
         # __________ end of XXX above
         from pypy.interpreter.pycode import PyCode
-        return PyCode(space)._from_code(c)
+        code = PyCode(space)        
+        code._code_new( c[0],
+                        c[1],
+                        c[2],
+                        c[3],
+                        c[4],
+                        c[5],
+                        c[6],
+                        c[7],
+                        c[8],
+                        c[9],
+                        c[10],
+                        c[11],
+                        c[12],
+                        c[13],
+                        )
+        return code
     #compile_parse_result._annspecialcase_ = 'override:cpy_stablecompiler'
 
 

Modified: pypy/dist/pypy/interpreter/pyparser/test/test_astcompiler.py
==============================================================================
--- pypy/dist/pypy/interpreter/pyparser/test/test_astcompiler.py	(original)
+++ pypy/dist/pypy/interpreter/pyparser/test/test_astcompiler.py	Tue Aug 23 18:26:52 2005
@@ -38,11 +38,12 @@
     assert code1.co_code == code2.co_code
 
 def check_compile( expr ):
+    import new
     ast_tree = ast_parse_expr( expr )
     misc.set_filename("<?>", ast_tree)
     print ast_tree
     codegenerator = pycodegen.InteractiveCodeGenerator(ast_tree)
-    code1 = codegenerator.getCode()
+    code1 = new.code(*codegenerator.getCode())
     code2 = ast_compile( expr )
     compare_code(code1,code2)
 



More information about the Pypy-commit mailing list