[pypy-svn] r5081 - in pypy/trunk/src/pypy/interpreter: . test

arigo at codespeak.net arigo at codespeak.net
Sat Jun 12 18:32:58 CEST 2004


Author: arigo
Date: Sat Jun 12 18:32:58 2004
New Revision: 5081

Modified:
   pypy/trunk/src/pypy/interpreter/pycode.py
   pypy/trunk/src/pypy/interpreter/test/test_code.py
   pypy/trunk/src/pypy/interpreter/typedef.py
Log:
new.code().


Modified: pypy/trunk/src/pypy/interpreter/pycode.py
==============================================================================
--- pypy/trunk/src/pypy/interpreter/pycode.py	(original)
+++ pypy/trunk/src/pypy/interpreter/pycode.py	Sat Jun 12 18:32:58 2004
@@ -98,6 +98,32 @@
         # first approximation
         return dis.findlabels(self.co_code)
 
+    def descr_code__new__(space, w_subtype,
+                          w_argcount, w_nlocals, w_stacksize, w_flags,
+                          w_codestring, w_constants, w_names,
+                          w_varnames, w_filename, w_name, w_firstlineno,
+                          w_lnotab, w_freevars=None, w_cellvars=None):
+        code = space.allocate_instance(PyCode, w_subtype)
+        code.__init__()
+        # XXX typechecking everywhere!
+        code.co_argcount   = space.unwrap(w_argcount)
+        code.co_nlocals    = space.unwrap(w_nlocals)
+        code.co_stacksize  = space.unwrap(w_stacksize)
+        code.co_flags      = space.unwrap(w_flags)
+        code.co_code       = space.unwrap(w_codestring)
+        code.co_consts     = space.unwrap(w_constants)
+        code.co_names      = space.unwrap(w_names)
+        code.co_varnames   = space.unwrap(w_varnames)
+        code.co_filename   = space.unwrap(w_filename)
+        code.co_name       = space.unwrap(w_name)
+        code.co_firstlineno= space.unwrap(w_firstlineno)
+        code.co_lnotab     = space.unwrap(w_lnotab)
+        if w_freevars is not None:
+            code.co_freevars = space.unwrap(w_freevars)
+        if w_cellvars is not None:
+            code.co_cellvars = space.unwrap(w_cellvars)
+        return space.wrap(code)
+
 
 def enhanceclass(baseclass, newclass, cache={}):
     # this is a bit too dynamic for RPython, but it looks nice

Modified: pypy/trunk/src/pypy/interpreter/test/test_code.py
==============================================================================
--- pypy/trunk/src/pypy/interpreter/test/test_code.py	(original)
+++ pypy/trunk/src/pypy/interpreter/test/test_code.py	Sat Jun 12 18:32:58 2004
@@ -15,6 +15,43 @@
         self.assertEquals(code.co_names,())
         self.assertEquals(code.co_varnames,())
         self.assertEquals(code.co_argcount,0)
+    def test_code(self):
+        import new
+        codestr = "global c\na = 1\nb = 2\nc = a + b\n"
+        ccode = compile(codestr, '<string>', 'exec')
+        co = new.code(ccode.co_argcount,
+                      ccode.co_nlocals,
+                      ccode.co_stacksize,
+                      ccode.co_flags,
+                      ccode.co_code,
+                      ccode.co_consts,
+                      ccode.co_names,
+                      ccode.co_varnames,
+                      ccode.co_filename,
+                      ccode.co_name,
+                      ccode.co_firstlineno,
+                      ccode.co_lnotab,
+                      ccode.co_freevars,
+                      ccode.co_cellvars)
+        d = {}
+        exec co in d
+        self.assertEquals(d['c'], 3)
+        # test backwards-compatibility version with no freevars or cellvars
+        co = new.code(ccode.co_argcount,
+                      ccode.co_nlocals,
+                      ccode.co_stacksize,
+                      ccode.co_flags,
+                      ccode.co_code,
+                      ccode.co_consts,
+                      ccode.co_names,
+                      ccode.co_varnames,
+                      ccode.co_filename,
+                      ccode.co_name,
+                      ccode.co_firstlineno,
+                      ccode.co_lnotab)
+        d = {}
+        exec co in d
+        self.assertEquals(d['c'], 3)
 
 if __name__ == '__main__':
     testit.main()

Modified: pypy/trunk/src/pypy/interpreter/typedef.py
==============================================================================
--- pypy/trunk/src/pypy/interpreter/typedef.py	(original)
+++ pypy/trunk/src/pypy/interpreter/typedef.py	Sat Jun 12 18:32:58 2004
@@ -162,6 +162,7 @@
     )
 
 PyCode.typedef = TypeDef('code',
+    __new__ = interp2app(PyCode.descr_code__new__.im_func),
     co_argcount = attrproperty('co_argcount'),
     co_nlocals = attrproperty('co_nlocals'),
     co_stacksize = attrproperty('co_stacksize'),



More information about the Pypy-commit mailing list