[pypy-svn] r15239 - in pypy/dist/pypy: interpreter interpreter/pyparser objspace/std tool

arigo at codespeak.net arigo at codespeak.net
Thu Jul 28 14:53:25 CEST 2005


Author: arigo
Date: Thu Jul 28 14:53:18 2005
New Revision: 15239

Modified:
   pypy/dist/pypy/interpreter/baseobjspace.py
   pypy/dist/pypy/interpreter/error.py
   pypy/dist/pypy/interpreter/pycompiler.py
   pypy/dist/pypy/interpreter/pyparser/tuplebuilder.py
   pypy/dist/pypy/objspace/std/objspace.py
   pypy/dist/pypy/tool/ansi_print.py
Log:
A new option --compiler=pyparseapp  (IN-PROGRESS, not working yet):

This option construct and initialize the object space just like now,
and then import at app-level the 'compiler' package, and finally
patch the executioncontext.compiler to use this 'compiler'
package.  The resulting object space is what translate_pypy will
see.

Ultimately the compiler and marshal should both be at
interp-level anyway.



Modified: pypy/dist/pypy/interpreter/baseobjspace.py
==============================================================================
--- pypy/dist/pypy/interpreter/baseobjspace.py	(original)
+++ pypy/dist/pypy/interpreter/baseobjspace.py	Thu Jul 28 14:53:18 2005
@@ -214,12 +214,10 @@
     def createcompiler(self):
         "Factory function creating a compiler object."
         # XXX simple selection logic for now
-        if self.options.compiler == 'pyparse':
+        if self.options.compiler in ('pyparse', 'pyparseapp'):
             return PythonCompiler(self)
         elif self.options.compiler == 'cpython':
             return CPythonCompiler(self)
-        #elif self.options.compiler == 'pyparseapp':
-        #    return PythonCompilerApp(self)
         else:
             raise ValueError('unknown --compiler option value: %r' % (
                 self.options.compiler,))

Modified: pypy/dist/pypy/interpreter/error.py
==============================================================================
--- pypy/dist/pypy/interpreter/error.py	(original)
+++ pypy/dist/pypy/interpreter/error.py	Thu Jul 28 14:53:18 2005
@@ -197,8 +197,9 @@
 # Utilities
 from pypy.tool.ansi_print import ansi_print
 
-def debug_print(text, file=None):
-    ansi_print(text, esc="31", file=file) # ANSI color code "red"
+def debug_print(text, file=None, newline=True):
+    # 31: ANSI color code "red"
+    ansi_print(text, esc="31", file=file, newline=newline)
 
 ### installing the excepthook for OperationErrors
 ##def operr_excepthook(exctype, value, traceback):

Modified: pypy/dist/pypy/interpreter/pycompiler.py
==============================================================================
--- pypy/dist/pypy/interpreter/pycompiler.py	(original)
+++ pypy/dist/pypy/interpreter/pycompiler.py	Thu Jul 28 14:53:18 2005
@@ -233,37 +233,55 @@
     compile_parse_result._annspecialcase_ = 'override:cpy_stablecompiler'
 
 
-##class PythonCompilerApp(PythonCompiler):
-##    """Temporary.  Calls the stablecompiler package at app-level."""
+class PythonCompilerApp(PythonCompiler):
+    """Temporary.  Calls the stablecompiler package at app-level."""
 
-##    def __init__(self, space):
-##        PythonCompiler.__init__(self, space)
-##        space.appexec(r'''():
-##            # NOT_RPYTHON
-##            from pypy.interpreter import stablecompiler
-##            from pypy.interpreter.stablecompiler.pycodegen import ModuleCodeGenerator
-##            from pypy.interpreter.stablecompiler.pycodegen import InteractiveCodeGenerator
-##            from pypy.interpreter.stablecompiler.pycodegen import ExpressionCodeGenerator
-##            from pypy.interpreter.stablecompiler.transformer import Transformer
-##            transformer = Transformer()
-##            tree = transformer.compile_node(tuples)
-##            stablecompiler.misc.set_filename(filename, tree)
-##            if mode == 'exec':
-##                codegenerator = ModuleCodeGenerator(tree)
-##            elif mode == 'single':
-##                codegenerator = InteractiveCodeGenerator(tree)
-##            else: # mode == 'eval':
-##                codegenerator = ExpressionCodeGenerator(tree)
-##            c = codegenerator.getCode()
-##        ''')
-
-##    def compile_parse_result(self, parse_result, filename, mode):
-
-
-
-
-        
-        
+    def __init__(self, space):
+        from pypy.interpreter.error import debug_print
+        PythonCompiler.__init__(self, space)
+        debug_print("importing the 'compiler' package at app-level...",
+                    newline=False)
+        self.w_applevelcompile = space.appexec([], r'''():
+            from compiler.misc import set_filename
+            from compiler.pycodegen import ModuleCodeGenerator
+            from compiler.pycodegen import InteractiveCodeGenerator
+            from compiler.pycodegen import ExpressionCodeGenerator
+            from compiler.transformer import Transformer
+
+            def applevelcompile(tuples, filename, mode):
+                transformer = Transformer()
+                tree = transformer.compile_node(tuples)
+                set_filename(filename, tree)
+                if mode == 'exec':
+                    codegenerator = ModuleCodeGenerator(tree)
+                elif mode == 'single':
+                    codegenerator = InteractiveCodeGenerator(tree)
+                else: # mode == 'eval':
+                    codegenerator = ExpressionCodeGenerator(tree)
+                return codegenerator.getCode()
+
+            return applevelcompile
+        ''')
+        debug_print(" done")
+
+    def compile_parse_result(self, parse_result, filename, mode):
+        space = self.space
+        source_encoding, stack_element = parse_result
+        w_nested_tuples = stack_element.as_w_tuple(space, lineno=True)
+        if source_encoding is not None:
+            w_nested_tuples = space.newtuple([
+                space.wrap(pysymbol.encoding_decl),
+                w_nested_tuples,
+                space.wrap(source_encoding)])
+
+        from pypy.interpreter.error import debug_print
+        debug_print("app-level compiling...", newline=False)
+        w_code = space.call_function(self.w_applevelcompile,
+                                     w_nested_tuples,
+                                     space.wrap(filename),
+                                     space.wrap(mode))
+        debug_print(" done")
+        return w_code
 
 
 class PyPyCompiler(CPythonCompiler):

Modified: pypy/dist/pypy/interpreter/pyparser/tuplebuilder.py
==============================================================================
--- pypy/dist/pypy/interpreter/pyparser/tuplebuilder.py	(original)
+++ pypy/dist/pypy/interpreter/pyparser/tuplebuilder.py	Thu Jul 28 14:53:18 2005
@@ -16,6 +16,14 @@
         else:
             return self.nodes[0][:-1]
 
+    def as_w_tuple(self, space, lineno=False):
+        num, value, lineno = self.nodes[0]
+        content = [space.wrap(num), space.wrap(value)]
+        if lineno:
+            content.append(space.wrap(lineno))
+        return space.newtuple(content)
+
+
 class NonTerminal(StackElement):
     def __init__(self, num, nodes):
         """rulename should always be None with regular Python grammar"""
@@ -25,8 +33,13 @@
     def as_tuple(self, lineno=False):
         l = [self.num] + [node.as_tuple(lineno) for node in self.nodes]
         return tuple(l)
-    
-        
+
+    def as_w_tuple(self, space, lineno=False):
+        l = [space.wrap(self.num)]
+        l += [node.as_w_tuple(space, lineno) for node in self.nodes]
+        return space.newtuple(l)
+
+
 def expand_nodes(stack_elements):
     """generate a nested tuples from a list of stack elements"""
     expanded = []

Modified: pypy/dist/pypy/objspace/std/objspace.py
==============================================================================
--- pypy/dist/pypy/objspace/std/objspace.py	(original)
+++ pypy/dist/pypy/objspace/std/objspace.py	Thu Jul 28 14:53:18 2005
@@ -99,6 +99,11 @@
         if self.options.uselibfile:
             self.setuselibfile() 
 
+        # XXX hack!: patch the compiler after initialization is complete
+        if self.options.compiler == 'pyparseapp':
+            from pypy.interpreter.pycompiler import PythonCompilerApp
+            self.getexecutioncontext().compiler = PythonCompilerApp(self)
+
     def enable_old_style_classes_as_default_metaclass(self):
         self.setitem(self.builtin.w_dict, self.wrap('__metaclass__'), self.w_classobj)
 

Modified: pypy/dist/pypy/tool/ansi_print.py
==============================================================================
--- pypy/dist/pypy/tool/ansi_print.py	(original)
+++ pypy/dist/pypy/tool/ansi_print.py	Thu Jul 28 14:53:18 2005
@@ -4,14 +4,16 @@
 
 import sys
 
-def ansi_print(text, esc, file=None):
+def ansi_print(text, esc, file=None, newline=True):
     if file is None: file = sys.stderr
     text = text.rstrip()
     if esc and sys.platform != "win32" and file.isatty():
         text = ('\x1b[%sm' % esc  +  
                 text +
                 '\x1b[0m')     # ANSI color code "reset"
-    file.write(text + '\n')
+    if newline:
+        text += '\n'
+    file.write(text)
 
 def ansi_log(msg): 
     keywords = list(msg.keywords)



More information about the Pypy-commit mailing list