[pypy-svn] rev 1361 - in pypy/trunk/src/pypy: interpreter module module/test

hpk at codespeak.net hpk at codespeak.net
Thu Sep 18 19:57:51 CEST 2003


Author: hpk
Date: Thu Sep 18 19:57:48 2003
New Revision: 1361

Modified:
   pypy/trunk/src/pypy/interpreter/baseobjspace.py
   pypy/trunk/src/pypy/interpreter/error.py
   pypy/trunk/src/pypy/interpreter/extmodule.py
   pypy/trunk/src/pypy/module/builtin.py
   pypy/trunk/src/pypy/module/test/test_builtin.py
Log:

a first go at introducing classes (or arbitrary app-level 
constructs instead of just functions and basic types ) 
for ExtModules. Feel free to modify/enhance. 

M    pypy/interpreter/error.py
switched AUTO_DEBUG off because output is a bit annoying 

M    pypy/interpreter/extmodule.py

provided a mechanism to compile&exec source code for ExtModule's 
at app-level. 

prevent certain class-specific attributes from beeing sent
to app-level (__metaclass__, __init__, __new__).

M    pypy/module/builtin.py
added 'xrange' as a class which is acutally compiled&execed during i
initilization of the builtin module/class. 

M    pypy/interpreter/baseobjspace.py
reordered the initilization of builtins a bit to allow 
for early execution (which needs space.builtin to be somewhat functional) 

M    pypy/module/test/test_builtin.py
added a test to check that the builtin 'xrange' is really a class/type



Modified: pypy/trunk/src/pypy/interpreter/baseobjspace.py
==============================================================================
--- pypy/trunk/src/pypy/interpreter/baseobjspace.py	(original)
+++ pypy/trunk/src/pypy/interpreter/baseobjspace.py	Thu Sep 18 19:57:48 2003
@@ -33,11 +33,13 @@
             self.make_sys()
 
         from pypy.module import builtin
+
+        # the builtins are iteratively initialized 
         self.builtin = builtin.__builtin__(self)
         self.w_builtin = self.wrap(self.builtin)
-        #self.w_builtins = self.getattr(self.w_builtin, self.wrap("__dict__"))
         self.w_builtins = self.builtin.w_dict
 
+        # initialize with "bootstrap types" from objspace  (e.g. w_None)
         for name, value in self.__dict__.items():
             if name.startswith('w_'):
                 name = name[2:]
@@ -46,6 +48,11 @@
                 #print "setitem: space instance %-20s into builtins" % name
                 self.setitem(self.w_builtins, self.wrap(name), value)
 
+        # only here can we add those builtins that require 
+        # execution of source code -- because this requires 
+        # an almost functional 'builtin' attribute on the space
+        self.builtin._initcompiledbuiltins()
+
         self.sys._setmodule(self.w_builtin)
 
     def make_sys(self):

Modified: pypy/trunk/src/pypy/interpreter/error.py
==============================================================================
--- pypy/trunk/src/pypy/interpreter/error.py	(original)
+++ pypy/trunk/src/pypy/interpreter/error.py	Thu Sep 18 19:57:48 2003
@@ -1,6 +1,6 @@
 import sys
 
-AUTO_DEBUG = 1
+AUTO_DEBUG = 0
 
 
 class PyPyError(Exception):

Modified: pypy/trunk/src/pypy/interpreter/extmodule.py
==============================================================================
--- pypy/trunk/src/pypy/interpreter/extmodule.py	(original)
+++ pypy/trunk/src/pypy/interpreter/extmodule.py	Thu Sep 18 19:57:48 2003
@@ -29,6 +29,9 @@
                     elif hasattr(value, '__get__'):
                         continue  # ignore CPython functions
 
+                    # ignore tricky class-attrs we can't send from interp to app-level 
+                    if name in ('__metaclass__', '__init__', '__new__', ): 
+                        continue  
                     space.call_method(self.w_dict, 'setdefault', 
                                       space.wrap(name), space.wrap(value))
 
@@ -36,3 +39,15 @@
     def __initclass__(cls):
         gateway.exportall(RwDictProxy(cls))   # xxx() -> app_xxx()
         gateway.importall(RwDictProxy(cls))   # app_xxx() -> xxx()
+
+    def _eval_app_source(self, sourcestring):
+        """ compile/execute a sourcestring in the applevel module dictionary """
+        w = self.space.wrap
+        w_code = self.compile(w(sourcestring), w('<pypyinline>'), w('exec'))
+        code = self.space.unwrap(w_code)
+        code.exec_code(self.space, self.w_dict, self.w_dict)
+
+        # XXX do we actually want an interp-proxy to the app-level thing here? 
+        #     or no interp-level "mirror" at all? 
+        co = compile(sourcestring, '<inline>','exec', 4096)
+        exec co in self.__dict__

Modified: pypy/trunk/src/pypy/module/builtin.py
==============================================================================
--- pypy/trunk/src/pypy/module/builtin.py	(original)
+++ pypy/trunk/src/pypy/module/builtin.py	Thu Sep 18 19:57:48 2003
@@ -19,6 +19,10 @@
     open = cpy_builtin.open
     file = cpy_builtin.file
 
+    def _initcompiledbuiltins(self):
+        """ add 'compiled' builtins to app-level dict and interp-level """
+        self._eval_app_source(xrange_appsource)
+
     def _actframe(self, index=-1):
         return self.space.getexecutioncontext().framestack.items[index]
 
@@ -379,31 +383,31 @@
         except AttributeError:
             return False
 
-
-    def app_xrange(self, start, stop=None, step=1):
-        class xrange:
-            def __init__(self, start, stop=None, step=1):
-                if stop is None: 
-                    self.start = 0
-                    self.stop = start
+# source code for the builtin xrange-class
+xrange_appsource = """if 1: 
+    class xrange:
+        def __init__(self, start, stop=None, step=1):
+            if stop is None: 
+                self.start = 0
+                self.stop = start
+            else:
+                self.start = start
+                self.stop = stop
+            if step == 0:
+                raise ValueError, 'xrange() step-argument (arg 3) must not be zero'
+            self.step = step
+
+        def __iter__(self):
+            def gen(self):
+                start, stop, step = self.start, self.stop, self.step
+                i = start
+                if step > 0:
+                    while i < stop:
+                        yield i
+                        i+=step
                 else:
-                    self.start = start
-                    self.stop = stop
-                if step == 0:
-                    raise ValueError, 'xrange() step-argument (arg 3) must not be zero'
-                self.step = step
-
-            def __iter__(self):
-                def gen(self):
-                    start, stop, step = self.start, self.stop, self.step
-                    i = start
-                    if step > 0:
-                        while i < stop:
-                            yield i
-                            i+=step
-                    else:
-                        while i > stop:
-                            yield i
-                            i+=step
-                return gen(self)
-        return xrange(start, stop, step)
+                    while i > stop:
+                        yield i
+                        i+=step
+            return gen(self)
+"""

Modified: pypy/trunk/src/pypy/module/test/test_builtin.py
==============================================================================
--- pypy/trunk/src/pypy/module/test/test_builtin.py	(original)
+++ pypy/trunk/src/pypy/module/test/test_builtin.py	Thu Sep 18 19:57:48 2003
@@ -52,6 +52,9 @@
         self.assertEquals(iter_x.next(), 3)
         self.assertRaises(StopIteration, iter_x.next)
 
+    def test_xrange_has_type_identity(self):
+        self.assertEquals(type(xrange(1)), type(xrange(1)))
+
     def test_cmp(self):
         self.assertEquals(cmp(9,9), 0)
         self.assert_(cmp(0,9) < 0)
@@ -88,6 +91,11 @@
             import os
             os.remove(fn)
 
+    def test_xrange(self):
+        self.assert_(hasattr(self.space.builtin, 'xrange'))
+        self.assertEquals(self.space.builtin.xrange(3).stop, 3)
+
+
 if __name__ == '__main__':
     test.main()
  


More information about the Pypy-commit mailing list