[pypy-svn] r12766 - in pypy/branch/pycompiler: interpreter interpreter/test tool

ludal at codespeak.net ludal at codespeak.net
Tue May 24 14:18:30 CEST 2005


Author: ludal
Date: Tue May 24 14:18:30 2005
New Revision: 12766

Modified:
   pypy/branch/pycompiler/interpreter/baseobjspace.py
   pypy/branch/pycompiler/interpreter/compiler.py
   pypy/branch/pycompiler/interpreter/executioncontext.py
   pypy/branch/pycompiler/interpreter/test/test_compiler.py
   pypy/branch/pycompiler/tool/option.py
Log:
 * first shot at introducing options for baseobjspace and such


Modified: pypy/branch/pycompiler/interpreter/baseobjspace.py
==============================================================================
--- pypy/branch/pycompiler/interpreter/baseobjspace.py	(original)
+++ pypy/branch/pycompiler/interpreter/baseobjspace.py	Tue May 24 14:18:30 2005
@@ -1,9 +1,11 @@
 from pypy.interpreter.executioncontext import ExecutionContext
 from pypy.interpreter.error import OperationError
 from pypy.interpreter.argument import Arguments
+from pypy.interpreter.compiler import CPythonCompiler, PythonCompiler
 from pypy.interpreter.miscutils import ThreadLocals
 from pypy.tool.cache import Cache 
 from pypy.rpython.rarithmetic import r_uint
+import pypy.tool.option
 
 __all__ = ['ObjSpace', 'OperationError', 'Wrappable', 'BaseWrappable',
            'W_Root']
@@ -92,12 +94,17 @@
     
     full_exceptions = True  # full support for exceptions (normalization & more)
 
-    def __init__(self):
+    def __init__(self, options = None):
         "NOT_RPYTHON: Basic initialization of objects."
         self.fromcache = InternalSpaceCache(self).getorbuild
         self.threadlocals = ThreadLocals()
         # set recursion limit
         # sets all the internal descriptors
+        
+        # XXX: Options in option.py is replaced by a function so
+        # it's not really clean to do a from option import Options
+        # since changing import order can change the Options object
+        self.options = options or pypy.tool.option.Options()
         self.initialize()
 
     def __repr__(self):
@@ -133,9 +140,17 @@
 
         # XXX we need to resolve unwrapping issues to 
         #     make this the default _sre module
-        #self.setbuiltinmodule("_sre", "_sre_pypy") 
-
-        # XXX disabled: self.setbuiltinmodule('parser')
+        #self.setbuiltinmodule("_sre", "_sre_pypy")
+        if self.options.parser == "recparser":
+            self.setbuiltinmodule('parser','recparser')
+        elif self.options.parser == "parser":
+            self.setbuiltinmodule('parser')
+
+        if self.options.uselibfile:
+            self.appexec([], '''():
+                from _file import file
+                __builtins__.file = __builtins__.open = file
+            ''')
 
         # initialize with "bootstrap types" from objspace  (e.g. w_None)
         for name, value in self.__dict__.items():
@@ -176,6 +191,13 @@
         "Factory function for execution contexts."
         return ExecutionContext(self)
 
+    def createcompiler(self):
+        "Factory function creating a compiler object."
+        # XXX: cache the compiler object ?
+        if self.options.compiler == "recparser":
+            return PythonCompiler(self)
+        return CPythonCompiler(self)
+
     # Following is a friendly interface to common object space operations
     # that can be defined in term of more primitive ones.  Subclasses
     # may also override specific functions for performance.

Modified: pypy/branch/pycompiler/interpreter/compiler.py
==============================================================================
--- pypy/branch/pycompiler/interpreter/compiler.py	(original)
+++ pypy/branch/pycompiler/interpreter/compiler.py	Tue May 24 14:18:30 2005
@@ -6,7 +6,7 @@
 from pypy.interpreter.error import OperationError
 
 
-class Compiler:
+class AbstractCompiler:
     """Abstract base class for a bytecode compiler."""
 
     # The idea is to grow more methods here over the time,
@@ -80,11 +80,14 @@
 import warnings
 import __future__
 compiler_flags = 0
+compiler_features = {}
 for fname in __future__.all_feature_names:
-    compiler_flags |= getattr(__future__, fname).compiler_flag
+    flag = getattr(__future__, fname).compiler_flag
+    compiler_flags |= flag
+    compiler_features[fname] = flag
 
 
-class CPythonCompiler(Compiler):
+class CPythonCompiler(AbstractCompiler):
     """Faked implementation of a compiler, using the underlying compile()."""
 
     def compile(self, source, filename, mode, flags):
@@ -162,3 +165,88 @@
 
     def restore_warn_explicit(self, warnings, old_warn_explicit):
         warnings.warn_explicit = old_warn_explicit
+
+class PythonCompiler(CPythonCompiler):
+    """Pure python compiler, using the compile()."""
+
+    def compile(self, source, filename, mode, flags):
+        flags |= __future__.generators.compiler_flag   # always on (2.2 compat)
+        space = self.space
+        try:
+            from pypy.module.recparser import compat
+            # HACK use our parser instead of the CPython's one
+            compat.transformer.parser = compat
+            c = compat.pycompile(source, filename, mode)
+        # It would be nice to propagate all exceptions to app level,
+        # but here we only propagate the 'usual' ones, until we figure
+        # out how to do it generically.
+        except SyntaxError,e:
+            w_synerr = space.newtuple([space.wrap(e.msg),
+                                       space.newtuple([space.wrap(e.filename),
+                                                       space.wrap(e.lineno),
+                                                       space.wrap(e.offset),
+                                                       space.wrap(e.text)])])
+            raise OperationError(space.w_SyntaxError, w_synerr)
+        except ValueError,e:
+            raise OperationError(space.w_ValueError,space.wrap(str(e)))
+        except TypeError,e:
+            raise OperationError(space.w_TypeError,space.wrap(str(e)))
+        from pypy.interpreter.pycode import PyCode
+        return space.wrap(PyCode(space)._from_code(c))
+
+class PyPyCompiler(CPythonCompiler):
+    """Pure python compiler, using the compile()."""
+    
+    def compile(self, source, filename, mode, flags):
+        flags |= __future__.generators.compiler_flag   # always on (2.2 compat)
+        space = self.space
+        try:
+            tree = # get the parse tree
+            gen = 
+            # HACK use our parser instead of the CPython's one
+            compat.transformer.parser = compat
+            c = compat.pycompile(source, filename, mode, flags)
+            if mode == "single":
+                gen = InteractiveCodeGenerator(tree)
+            elif mode == "exec":
+                gen = ModuleCodeGenerator(tree)
+            elif mode == "eval":
+                gen = ExpressionCodeGenerator(tree)
+            else:
+                raise OperationError(space.w_ValueError,
+                                     space.wrap("compile() 3rd arg must be 'exec' or "
+                                                "'eval' or 'single'") )
+
+            # set compiler flags (doesn't work, code is generated at __init__ time)
+            #genflags = []
+            #for feature,flag in compiler_features.items():
+            #    if flags | flag:
+            #        genflags.append(feature)
+            #gen.futures = tuple(genflags)
+            
+                
+            
+        # It would be nice to propagate all exceptions to app level,
+        # but here we only propagate the 'usual' ones, until we figure
+        # out how to do it generically.
+        except SyntaxError,e:
+            w_synerr = space.newtuple([space.wrap(e.msg),
+                                       space.newtuple([space.wrap(e.filename),
+                                                       space.wrap(e.lineno),
+                                                       space.wrap(e.offset),
+                                                       space.wrap(e.text)])])
+            raise OperationError(space.w_SyntaxError, w_synerr)
+        except ValueError,e:
+            raise OperationError(space.w_ValueError,space.wrap(str(e)))
+        except TypeError,e:
+            raise OperationError(space.w_TypeError,space.wrap(str(e)))
+        from pypy.interpreter.pycode import PyCode
+        return space.wrap(PyCode(space)._from_code(c))
+
+    def compile_module(self, source, filename, mode ):
+        pass
+
+    def compile_interactive
+        
+
+Compiler = PythonCompiler

Modified: pypy/branch/pycompiler/interpreter/executioncontext.py
==============================================================================
--- pypy/branch/pycompiler/interpreter/executioncontext.py	(original)
+++ pypy/branch/pycompiler/interpreter/executioncontext.py	Tue May 24 14:18:30 2005
@@ -1,7 +1,6 @@
 import sys
 from pypy.interpreter.miscutils import Stack
 from pypy.interpreter.error import OperationError
-from pypy.interpreter.compiler import CPythonCompiler
 
 class ExecutionContext:
     """An ExecutionContext holds the state of an execution thread
@@ -14,7 +13,7 @@
         self.w_tracefunc = None
         self.w_profilefunc = None
         self.is_tracing = 0
-        self.compiler = CPythonCompiler(space)
+        self.compiler = space.createcompiler()
 
     def enter(self, frame):
         if self.framestack.depth() > self.space.sys.recursionlimit:

Modified: pypy/branch/pycompiler/interpreter/test/test_compiler.py
==============================================================================
--- pypy/branch/pycompiler/interpreter/test/test_compiler.py	(original)
+++ pypy/branch/pycompiler/interpreter/test/test_compiler.py	Tue May 24 14:18:30 2005
@@ -1,13 +1,13 @@
 import __future__
 import autopath
 import py
-from pypy.interpreter.compiler import CPythonCompiler, Compiler
+from pypy.interpreter.compiler import CPythonCompiler, PythonCompiler
 from pypy.interpreter.pycode import PyCode
 
 
-class TestCompiler:
+class BaseTestCompiler:
     def setup_method(self, method):
-        self.compiler = CPythonCompiler(self.space)
+        self.compiler = self.space.createcompiler()
 
     def test_compile(self):
         code = self.compiler.compile('6*7', '<hello>', 'eval', 0)
@@ -48,6 +48,16 @@
         assert flags == flags2
 
 
-class TestECCompiler(TestCompiler):
+class TestECCompiler(BaseTestCompiler):
     def setup_method(self, method):
         self.compiler = self.space.getexecutioncontext().compiler
+
+class TestPyCCompiler(BaseTestCompiler):
+    def setup_method(self, method):
+        self.compiler = CPythonCompiler(self.space)
+
+
+class TestPyPyCompiler(BaseTestCompiler):
+    def setup_method(self, method):
+        self.compiler = PythonCompiler(self.space)
+

Modified: pypy/branch/pycompiler/tool/option.py
==============================================================================
--- pypy/branch/pycompiler/tool/option.py	(original)
+++ pypy/branch/pycompiler/tool/option.py	Tue May 24 14:18:30 2005
@@ -10,6 +10,8 @@
     spaces = []
     oldstyle = 0
     uselibfile = 0
+    parser = "recparser"
+    compiler = "recparser"
 
 def run_tb_server(option, opt, value, parser):
     from pypy.tool import tb_server
@@ -55,7 +57,6 @@
 
     this is configured via the environment variable OBJSPACE
     """
-    
     if not name:
         if Options.spaces:
             name = Options.spaces[-1]
@@ -67,10 +68,10 @@
     except KeyError:
         module = __import__("pypy.objspace.%s" % name, None, None, ["Space"])
         Space = module.Space
-        space = Space()
+        space = Space( Options() )
         if name == 'std' and Options.oldstyle:
             space.enable_old_style_classes_as_default_metaclass()
-        if Options.uselibfile:
+        if False and Options.uselibfile:
             space.appexec([], '''():
                 from _file import file
                 __builtins__.file = __builtins__.open = file



More information about the Pypy-commit mailing list