[pypy-svn] r16580 - in pypy/dist/pypy: fakecompiler interpreter lib/_stablecompiler

tismer at codespeak.net tismer at codespeak.net
Fri Aug 26 11:54:34 CEST 2005


Author: tismer
Date: Fri Aug 26 11:54:33 2005
New Revision: 16580

Modified:
   pypy/dist/pypy/fakecompiler/fakecompiler.py
   pypy/dist/pypy/fakecompiler/readme.txt
   pypy/dist/pypy/interpreter/pycompiler.py
   pypy/dist/pypy/lib/_stablecompiler/apphook.py
Log:
worked hard on making the compiled pypy do testing.

This had the side effect of many fixes, so it was not lost.

But there are still problems.

First thing: when I use the half-faked compiler to compile test_complex,
pypy crashes with a core dump.
For that reason, I also added a mode to fake the complete compiler.
That finally works, but then _sre is missing.

We need to decide if we want to continue with this right now.

Modified: pypy/dist/pypy/fakecompiler/fakecompiler.py
==============================================================================
--- pypy/dist/pypy/fakecompiler/fakecompiler.py	(original)
+++ pypy/dist/pypy/fakecompiler/fakecompiler.py	Fri Aug 26 11:54:33 2005
@@ -1,13 +1,27 @@
-import parser, marshal, os
+import parser, marshal, os, __future__
 
 DUMPFILE = 'this_is_the_marshal_file'
 
-def reallycompile(tuples, filename, mode, flag_names):
-    # XXX : use the flags if possible
-    return parser.compileast(parser.tuple2ast(tuples), filename)
+def reallycompile(tuples_or_src, filename, mode, flag_names):
+    if type(tuples_or_src) is str:
+        flags = 0
+        if 'nested_scopes' in flag_names:
+            flags |= __future__.CO_NESTED
+        if 'generators' in flag_names:
+            flags |= __future__.CO_GENERATOR_ALLOWED
+        if 'division' in flag_names:
+            flags |= __future__.CO_FUTURE_DIVISION
+        return compile(tuples_or_src, filename, mode, flags)
+    return parser.compileast(parser.tuple2ast(tuples_or_src), filename)
 
 if __name__ == '__main__':
-    tuples, filename, mode, done, flag_names = marshal.load(file(DUMPFILE, "rb"))
-    code = reallycompile(tuples, filename, mode, flag_names)
+    s = file(DUMPFILE, "rb").read()
+    tup = marshal.loads(s)
+    tuples_or_src, filename, mode, done, flag_names = tup
+    try:
+        code = reallycompile(tuples_or_src, filename, mode, flag_names)
+    except SyntaxError, e:
+        code = e.msg, (e.filename, e.lineno, e.offset, e.text)
     done = True
-    marshal.dump( (code, filename, mode, done ), file(DUMPFILE, "wb"), 1)
+    tup = (code, filename, mode, done, flag_names)
+    marshal.dump( tup, file(DUMPFILE, "wb"))

Modified: pypy/dist/pypy/fakecompiler/readme.txt
==============================================================================
--- pypy/dist/pypy/fakecompiler/readme.txt	(original)
+++ pypy/dist/pypy/fakecompiler/readme.txt	Fri Aug 26 11:54:33 2005
@@ -14,3 +14,15 @@
 - there must be a file named "pythonname" which contains the
   complete filename of a Python 1.4.2 compatible binary.
   Please, add this by hand for your installation.
+
+Latest addtion:
+
+The current compiler seems to be instable. For instance, it
+crashes completely without a message on test_complex.
+Therefore, I added a new option:
+
+- If the folder with fakecompiler.py also contains a file
+  named "fakecompletely", the whole compilation process
+  is redirected to a real CPython process.
+
+I very much hope that this crap can be removed, ASAP (chris)

Modified: pypy/dist/pypy/interpreter/pycompiler.py
==============================================================================
--- pypy/dist/pypy/interpreter/pycompiler.py	(original)
+++ pypy/dist/pypy/interpreter/pycompiler.py	Fri Aug 26 11:54:33 2005
@@ -5,7 +5,7 @@
 from codeop import PyCF_DONT_IMPLY_DEDENT
 from pypy.interpreter.error import OperationError
 from pypy.rpython.objectmodel import we_are_translated
-
+import os
 
 class AbstractCompiler:
     """Abstract base class for a bytecode compiler."""
@@ -117,10 +117,10 @@
                                                        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)))
+        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 PyCode(space)._from_code(c)
     compile._annspecialcase_ = "override:cpy_compile"
@@ -182,7 +182,7 @@
 
     XXX: This class should override the baseclass implementation of
          compile_command() in order to optimize it, especially in case
-         of incomplete inputs (e.g. we shouldn't re-compile from sracth
+         of incomplete inputs (e.g. we shouldn't re-compile from scratch
          the whole source after having only added a new '\n')
     """
     def compile(self, source, filename, mode, flags):
@@ -231,23 +231,24 @@
                                                        space.wrap(e.offset),
                                                        space.wrap(e.text)])])
             raise OperationError(space.w_SyntaxError, w_synerr)
-	except UnicodeDecodeError, e:
+        except UnicodeDecodeError, e:
             raise OperationError(space.w_UnicodeDecodeError, space.newtuple([
                                  space.wrap(e.encoding), space.wrap(e.object), space.wrap(e.start),
                                  space.wrap(e.end), space.wrap(e.reason)]))
-        except ValueError,e:
+        except ValueError, e:
             if e.__class__ != ValueError:
                  extra_msg = "(Really go %s)" % e.__class__.__name__
             else:
                 extra_msg = ""
-            raise OperationError(space.w_ValueError,space.wrap(str(e)+extra_msg))
-        except TypeError,e:
-            raise OperationError(space.w_TypeError,space.wrap(str(e)))
+            raise OperationError(space.w_ValueError, space.wrap(str(e)+extra_msg))
+        except TypeError, e:
+            raise OperationError(space.w_TypeError, space.wrap(str(e)))
         # __________ end of XXX above
         from pypy.interpreter.pycode import PyCode
         return PyCode(space)._from_code(c)
     compile_parse_result._annspecialcase_ = 'override:cpy_stablecompiler'
 
+
 class PythonCompilerApp(PythonCompiler):
     """Temporary.  Calls the stablecompiler package at app-level."""
 
@@ -259,7 +260,21 @@
         self._load_compilers()
         debug_print(" done")
 
+    def compile(self, source, filename, mode, flags):
+        if (os.path.exists('fakecompletely') and
+            os.path.exists('fakecompiler.py')):
+            self.printmessage("faking all of compiler, cause"
+                              " fakecompletely and fakecompiler.py"
+                              " are in curdir")
+            return self.fakecompile(source, filename, mode, flags)
+        return PythonCompiler.compile(self, source, filename, mode, flags)
+
     def _load_compilers(self):
+        # note: all these helpers, including the
+        # remote compiler, must be initialized early,
+        # or we get annotation errors. An alternative
+        # would be to use applevel instances, instead.
+        # But this code should anyway go away, soon.
         self.w_compileapp = self.space.appexec([], r'''():
             from _stablecompiler import apphook
             return apphook.applevelcompile
@@ -279,7 +294,6 @@
         space.call_function(self.w_printmessage, space.wrap(msg))
 
     def _get_compiler(self, mode):
-        from pypy.interpreter.error import debug_print
         import os
         if os.path.exists('fakecompiler.py') and mode != 'single':
             self.printmessage("faking compiler, because fakecompiler.py"
@@ -320,6 +334,40 @@
                                  space.wrap("code object expected"))
         return code
 
+    def fakecompile(self, source, filename, mode, flags):
+        flags |= __future__.generators.compiler_flag   # always on (2.2 compat)
+        flag_names = get_flag_names(flags)
+        space = self.space
+
+        w_flag_names = space.newlist( [ space.wrap(n) for n in flag_names ] )
+        try:
+            w_code = space.call_function(self.w_compilefake,
+                                         space.wrap(source),
+                                         space.wrap(filename),
+                                         space.wrap(mode),
+                                         w_flag_names)
+        except OperationError, err:
+            if not err.match(space, space.w_SyntaxError):
+                raise
+            # unfortunately, we need to unnormalize the wrapped SyntaxError,
+            # or the w_value comparison will not work.
+            w_inst = err.w_value
+            w = space.wrap
+            w_synerr = space.newtuple(
+                [space.getattr(w_inst, w('msg')),
+                 space.newtuple([space.getattr(w_inst, w('filename')),
+                                 space.getattr(w_inst, w('lineno')),
+                                 space.getattr(w_inst, w('offset')),
+                                 space.getattr(w_inst, w('text'))])])
+            raise OperationError(space.w_SyntaxError, w_synerr)
+            
+        code = space.interpclass_w(w_code)
+        from pypy.interpreter.pycode import PyCode
+        if not isinstance(code, PyCode):
+            raise OperationError(space.w_RuntimeError,
+                                 space.wrap("code object expected"))
+        return code
+
 
 class PythonAstCompiler(CPythonCompiler):
     """Uses the stdlib's python implementation of compiler
@@ -384,6 +432,58 @@
     #compile_parse_result._annspecialcase_ = 'override:cpy_stablecompiler'
 
 
+class CPythonRemoteCompiler(AbstractCompiler):
+    """Faked implementation of a compiler, using an external Python's compile()."""
+
+    def __init__(self, space):
+        AbstractCompiler.__init__(self, space)
+        self.w_compilefake = self.space.appexec([], r'''():
+            from _stablecompiler import apphook
+            return apphook.fakeapplevelcompile
+        ''')
+
+    def compile(self, source, filename, mode, flags):
+        flags |= __future__.generators.compiler_flag   # always on (2.2 compat)
+        flag_names = get_flag_names(flags)
+        space = self.space
+        return None
+
+        w_code = space.call_function(self.w_compilefake,
+                                     space.wrap(source),
+                                     space.wrap(filename),
+                                     space.wrap(mode),
+                                     space.wrap(flag_names))
+        code = space.interpclass_w(w_code)
+        return code
+
+        try:
+            w_code = space.call_function(self.w_compilefake,
+                                         space.wrap(source),
+                                         space.wrap(filename),
+                                         space.wrap(mode),
+                                         space.wrap(flag_names))
+        except OperationError, err:
+            if not err.match(space, space.w_SyntaxError):
+                raise
+            # unfortunately, we need to unnormalize the wrapped SyntaxError,
+            # or the w_value comparison will not work.
+            w_inst = err.w_value
+            w = space.wrap
+            w_synerr = space.newtuple(
+                [space.getattr(w_inst, w('msg')),
+                 space.newtuple([space.getattr(w_inst, w('filename')),
+                                 space.getattr(w_inst, w('lineno')),
+                                 space.getattr(w_inst, w('offset')),
+                                 space.getattr(w_inst, w('text'))])])
+            raise OperationError(space.w_SyntaxError, w_synerr)
+            
+        code = space.interpclass_w(w_code)
+        from pypy.interpreter.pycode import PyCode
+        if not isinstance(code, PyCode):
+            raise OperationError(space.w_RuntimeError,
+                                 space.wrap("code object expected"))
+        return code
+
 
 class PyPyCompiler(CPythonCompiler):
     """Uses the PyPy implementation of Compiler

Modified: pypy/dist/pypy/lib/_stablecompiler/apphook.py
==============================================================================
--- pypy/dist/pypy/lib/_stablecompiler/apphook.py	(original)
+++ pypy/dist/pypy/lib/_stablecompiler/apphook.py	Fri Aug 26 11:54:33 2005
@@ -26,10 +26,10 @@
 
 DUMPFILE = 'this_is_the_marshal_file'
 
-def fakeapplevelcompile(tuples, filename, mode, flag_names):
+def fakeapplevelcompile(tuples_or_src, filename, mode, flag_names):
     import os, marshal
     done = False
-    data = marshal.dumps( (tuples, filename, mode, done, flag_names) )
+    data = marshal.dumps( (tuples_or_src, filename, mode, done, flag_names))
     f = file(DUMPFILE, "wb")
     f.write(data)
     f.close()
@@ -37,10 +37,12 @@
     f = file(DUMPFILE, "rb")
     data = f.read()
     f.close()
-    code, filename, mode, done = marshal.loads(data)
+    code_or_syntax, filename, mode, done, flag_names = marshal.loads(data)
     if not done:
         raise ValueError, "could not fake compile!"
-    return code
+    if type(code_or_syntax) is tuple:
+        raise SyntaxError(*code_or_syntax)
+    return code_or_syntax
 
 def get_python():
     try:



More information about the Pypy-commit mailing list