[pypy-svn] r11967 - in pypy/dist/pypy: interpreter module/builtin

arigo at codespeak.net arigo at codespeak.net
Thu May 5 16:56:45 CEST 2005


Author: arigo
Date: Thu May  5 16:56:44 2005
New Revision: 11967

Modified:
   pypy/dist/pypy/interpreter/compiler.py
   pypy/dist/pypy/interpreter/interactive.py
   pypy/dist/pypy/interpreter/pyframe.py
   pypy/dist/pypy/interpreter/pyopcode.py
   pypy/dist/pypy/module/builtin/compiling.py
Log:
Plugged the compiler.py module into our built-in compile(),
and into the interactive command-line interpreter.


Modified: pypy/dist/pypy/interpreter/compiler.py
==============================================================================
--- pypy/dist/pypy/interpreter/compiler.py	(original)
+++ pypy/dist/pypy/interpreter/compiler.py	Thu May  5 16:56:44 2005
@@ -78,17 +78,16 @@
 # faked compiler
 
 import __future__
-feature_compiler_flags = 0
+compiler_flags = 0
 for fname in __future__.all_feature_names:
-    feature = getattr(__future__, fname)
-    feature_compiler_flags |= feature.compiler_flag
-    del feature, fname
+    compiler_flags |= getattr(__future__, fname).compiler_flag
 
 
 class CPythonCompiler(Compiler):
     """Faked implementation of a compiler, using the underlying compile()."""
 
     def compile(self, source, filename, mode, flags):
+        flags |= __future__.generators.compiler_flag   # always on (2.2 compat)
         space = self.space
         try:
             # hack to make the flow space happy: 'warnings' should not look
@@ -114,8 +113,10 @@
 
     def getcodeflags(self, code):
         from pypy.interpreter.pycode import PyCode
-        assert isinstance(code, PyCode)
-        return code.co_flags & feature_compiler_flags
+        if isinstance(code, PyCode):
+            return code.co_flags & compiler_flags
+        else:
+            return 0
 
     def _warn_explicit(self, message, category, filename, lineno,
                        module=None, registry=None):

Modified: pypy/dist/pypy/interpreter/interactive.py
==============================================================================
--- pypy/dist/pypy/interpreter/interactive.py	(original)
+++ pypy/dist/pypy/interpreter/interactive.py	Thu May  5 16:56:44 2005
@@ -92,6 +92,7 @@
         self.verbose = verbose
         space = self.space
         self.ec = space.createexecutioncontext()
+        self.console_compiler_flags = 0
 
         mainmodule = main.ensure__main__(space)
         self.w_globals = mainmodule.w_dict
@@ -158,26 +159,32 @@
             raise
 
     def runcode(self, code):
-        # 'code' is a CPython code object
-        from pypy.interpreter.pycode import PyCode
-        pycode = PyCode(self.space)._from_code(code)
+        raise NotImplementedError
+
+    def runsource(self, source, ignored_filename="<input>", symbol="single"):
+        hacked_filename = '<inline>' + source
+        compiler = self.space.getexecutioncontext().compiler
+        
         def doit():
+            # compile the provided input
+            code = compiler.compile_command(source, hacked_filename, symbol,
+                                            self.console_compiler_flags)
+            if code is None:
+                raise IncompleteInput
+            self.console_compiler_flags |= compiler.getcodeflags(code)
+
+            # execute it
             self.settrace()
-            pycode.exec_code(self.space, self.w_globals, self.w_globals)
+            code.exec_code(self.space, self.w_globals, self.w_globals)
             self.checktrace()
-        main.run_toplevel(self.space, doit, verbose=self.verbose)
 
-    def runsource(self, source, ignored_filename="<input>", symbol="single"):
-        hacked_filename = '<inline>' + source
+        # run doit() in an exception-catching box
         try:
-            code = self.compile(source, hacked_filename, symbol)
-        except (OverflowError, SyntaxError, ValueError):
-            self.showsyntaxerror(self.filename)
-            return 0
-        if code is None:
+            main.run_toplevel(self.space, doit, verbose=self.verbose)
+        except IncompleteInput:
             return 1
-        self.runcode(code)
-        return 0
+        else:
+            return 0
 
     def settrace(self):
         if self.tracelevel:
@@ -216,6 +223,10 @@
 
         self.tracelevel = tracelevel
 
+
+class IncompleteInput(Exception):
+    pass
+
 if __name__ == '__main__':
     try:
         import readline

Modified: pypy/dist/pypy/interpreter/pyframe.py
==============================================================================
--- pypy/dist/pypy/interpreter/pyframe.py	(original)
+++ pypy/dist/pypy/interpreter/pyframe.py	Thu May  5 16:56:44 2005
@@ -14,11 +14,6 @@
     g[op] = opcode.opmap[op]
 HAVE_ARGUMENT = opcode.HAVE_ARGUMENT
 
-import __future__
-compiler_flags = 0
-for fname in __future__.all_feature_names:
-    compiler_flags |= getattr(__future__, fname).compiler_flag
-
 
 def cpython_tb():
    """NOT_RPYTHON"""
@@ -76,9 +71,6 @@
     def getclosure(self):
         return None
 
-    def get_compile_flags(self):
-        return self.code.co_flags & compiler_flags
-
     def eval(self, executioncontext):
         "Interpreter main loop!"
         try:

Modified: pypy/dist/pypy/interpreter/pyopcode.py
==============================================================================
--- pypy/dist/pypy/interpreter/pyopcode.py	(original)
+++ pypy/dist/pypy/interpreter/pyopcode.py	Thu May  5 16:56:44 2005
@@ -346,7 +346,8 @@
         w_locals  = f.valuestack.pop()
         w_globals = f.valuestack.pop()
         w_prog    = f.valuestack.pop()
-        w_compile_flags = f.space.wrap(f.get_compile_flags())
+        flags = f.space.getexecutioncontext().compiler.getcodeflags(f.code)
+        w_compile_flags = f.space.wrap(flags)
         w_resulttuple = prepare_exec(f.space, f.space.wrap(f), w_prog,
                                      w_globals, w_locals,
                                      w_compile_flags, f.space.wrap(f.builtin),

Modified: pypy/dist/pypy/module/builtin/compiling.py
==============================================================================
--- pypy/dist/pypy/module/builtin/compiling.py	(original)
+++ pypy/dist/pypy/module/builtin/compiling.py	Thu May  5 16:56:44 2005
@@ -6,82 +6,24 @@
 from pypy.interpreter.baseobjspace import W_Root, ObjSpace
 from pypy.interpreter.error import OperationError 
 from pypy.interpreter.gateway import NoneNotWrapped
-import __builtin__ as cpy_builtin
-import warnings
 
-def setup_warn_explicit(space): 
-    """ NOT_RPYTHON 
-   
-    this is a hack until we have our own parsing/compiling 
-    in place: we bridge certain warnings to the applevel 
-    warnings module to let it decide what to do with
-    a syntax warning ... 
-    """ 
-    def warn_explicit(message, category, filename, lineno,
-                      module=None, registry=None):
-        if hasattr(category, '__bases__') and \
-           issubclass(category, SyntaxWarning): 
-            assert isinstance(message, str) 
-            w_mod = space.sys.getmodule('warnings')
-            if w_mod is not None: 
-                w_dict = w_mod.getdict() 
-                w_reg = space.call_method(w_dict, 'setdefault', 
-                                          space.wrap("__warningregistry__"),     
-                                          space.newdict([]))
-                try: 
-                    space.call_method(w_mod, 'warn_explicit', 
-                                      space.wrap(message), 
-                                      space.w_SyntaxWarning, 
-                                      space.wrap(filename), 
-                                      space.wrap(lineno), 
-                                      space.w_None, 
-                                      space.w_None) 
-                except OperationError, e: 
-                    if e.match(space, space.w_SyntaxWarning): 
-                        raise OperationError(
-                                space.w_SyntaxError, 
-                                space.wrap(message))
-                    raise 
-    old_warn_explicit = warnings.warn_explicit 
-    warnings.warn_explicit = warn_explicit 
-    return old_warn_explicit 
-
-def compile(space, w_str_, filename, startstr,
-            supplied_flags=0, dont_inherit=0):
-    if space.is_true(space.isinstance(w_str_, space.w_unicode)):
-        str_ = space.unwrap(w_str_) # xxx generic unwrap
+def compile(space, w_source, filename, mode, flags=0, dont_inherit=0):
+    if space.is_true(space.isinstance(w_source, space.w_unicode)):
+        str_ = space.unwrap(w_source) # xxx generic unwrap
     else:
-        str_ = space.str_w(w_str_)
-    #print (str_, filename, startstr, supplied_flags, dont_inherit)
-    # XXX we additionally allow GENERATORS because compiling some builtins
-    #     requires it. doesn't feel quite right to do that here.
-    supplied_flags |= 4096 
+        str_ = space.str_w(w_source)
+
+    ec = space.getexecutioncontext()
     if not dont_inherit:
         try:
-            caller = space.getexecutioncontext().framestack.top()
+            caller = ec.framestack.top()
         except IndexError:
-            caller = None
+            pass
         else:
-            from pypy.interpreter import pyframe
-            if isinstance(caller, pyframe.PyFrame): 
-                supplied_flags |= caller.get_compile_flags()
-    try:
-        old = setup_warn_explicit(space)
-        try: 
-            c = cpy_builtin.compile(str_, filename, startstr, supplied_flags, 1)
-        finally: 
-            warnings.warn_explicit = old 
-            
-    # 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:
-        raise OperationError(space.w_SyntaxError,space.wrap(e.args))
-    except ValueError,e:
-        raise OperationError(space.w_ValueError,space.wrap(str(e)))
-    except TypeError,e:
-        raise OperationError(space.w_TypeError,space.wrap(str(e)))
-    return space.wrap(PyCode(space)._from_code(c))
+            flags |= ec.compiler.getcodeflags(caller.code)
+
+    code = ec.compiler.compile(str_, filename, mode, flags)
+    return space.wrap(code)
 #
 compile.unwrap_spec = [ObjSpace,W_Root,str,str,int,int]
 



More information about the Pypy-commit mailing list