[pypy-svn] r16268 - in pypy/dist/pypy: fakecompiler interpreter interpreter/pyparser

tismer at codespeak.net tismer at codespeak.net
Tue Aug 23 15:59:33 CEST 2005


Author: tismer
Date: Tue Aug 23 15:59:27 2005
New Revision: 16268

Added:
   pypy/dist/pypy/fakecompiler/
   pypy/dist/pypy/fakecompiler/fakecompiler.py   (contents, props changed)
   pypy/dist/pypy/fakecompiler/readme.txt   (contents, props changed)
   pypy/dist/pypy/interpreter/pyparser/symbol.py   (contents, props changed)
Modified:
   pypy/dist/pypy/interpreter/pycompiler.py
   pypy/dist/pypy/interpreter/pyparser/pysymbol.py
Log:
added a compiler faking feature in order to support quicker testing
using the compiled pypy.

Run your binary from the fakecompiler folder;
please the readme.txt file, first!


Added: pypy/dist/pypy/fakecompiler/fakecompiler.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/fakecompiler/fakecompiler.py	Tue Aug 23 15:59:27 2005
@@ -0,0 +1,29 @@
+import parser, marshal, os
+
+DUMPFILE = 'this_is_the_marshal_file'
+
+def fakeapplevelcompile(tuples, filename, mode):
+    done = False
+    data = marshal.dumps( (tuples, filename, mode, done) )
+    file(DUMPFILE, "wb").write(data)
+    os.system('%s fakecompiler.py' % get_python())
+    data = file(DUMPFILE, "rb").read()
+    code, filename, mode, done = marshal.loads(data)
+    if not done:
+        raise ValueError, "could not fake compile!"
+    return code
+
+def reallycompile(tuples, filename, mode):
+    return parser.compileast(parser.tuple2ast(tuples), filename)
+
+def get_python():
+    try:
+        return file('pythonname').read().strip()
+    except IOError:
+        raise ValueError, "I need a local file 'pythonname'"
+
+if __name__ == '__main__':
+    tuples, filename, mode, done = marshal.load(file(DUMPFILE, "rb"))
+    code = reallycompile(tuples, filename, mode)
+    done = True
+    marshal.dump( (code, filename, mode, done), file(DUMPFILE, "wb"), 1)

Added: pypy/dist/pypy/fakecompiler/readme.txt
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/fakecompiler/readme.txt	Tue Aug 23 15:59:27 2005
@@ -0,0 +1,16 @@
+Attention!
+
+This folder's purpose is to support testing using the compiled PyPy.
+At the moment, our applevel compiler is a bit slow to make this
+useful. Therefore, this folder exists.
+
+If the compiled pypy's current directory has a file named
+"fakecompiler.py", like this folder, then every compilation
+that is either "exec" or "eval" gets compiled using CPython.
+
+Requirements:
+
+- this folder must be writable
+- 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.

Modified: pypy/dist/pypy/interpreter/pycompiler.py
==============================================================================
--- pypy/dist/pypy/interpreter/pycompiler.py	(original)
+++ pypy/dist/pypy/interpreter/pycompiler.py	Tue Aug 23 15:59:27 2005
@@ -240,15 +240,26 @@
         PythonCompiler.__init__(self, space)
         debug_print("importing the 'compiler' package at app-level...",
                     newline=False)
-        self._load_compiler()
+        self._load_compiler('single')
         debug_print(" done")
 
-    def _load_compiler(self):
-        # doing this all the time, to allow patching
-        self.w_applevelcompile = self.space.appexec([], r'''():
-            from _stablecompiler import apphook
-            return apphook.applevelcompile
-        ''')
+    def _load_compiler(self, mode):
+        if mode == 'single':
+            self.w_applevelcompile = self.space.appexec([], r'''():
+                from _stablecompiler import apphook
+                return apphook.applevelcompile
+            ''')
+        else:
+            self.w_applevelcompile = self.space.appexec([], r'''():
+                import os
+                from _stablecompiler import apphook
+                if os.path.exists('fakecompiler.py'):
+                    print "faking compiler, because fakecompiler.py is in the current dir"
+                    import fakecompiler
+                    return fakecompiler.fakeapplevelcompile
+                else:
+                    return apphook.applevelcompile
+            ''')
 
     def compile_parse_result(self, parse_result, filename, mode):
         space = self.space
@@ -268,7 +279,7 @@
                 w_nested_tuples,
                 space.wrap(source_encoding)])
 
-        self._load_compiler()
+        self._load_compiler(mode)
         w_code = space.call_function(self.w_applevelcompile,
                                      w_nested_tuples,
                                      space.wrap(filename),

Modified: pypy/dist/pypy/interpreter/pyparser/pysymbol.py
==============================================================================
--- pypy/dist/pypy/interpreter/pyparser/pysymbol.py	(original)
+++ pypy/dist/pypy/interpreter/pyparser/pysymbol.py	Tue Aug 23 15:59:27 2005
@@ -1,27 +1,32 @@
 # replacement for the CPython symbol module
-from pytoken import N_TOKENS
+from pypy.interpreter.pyparser import symbol
 
 # try to avoid numeric values conflict with tokens
 # it's important for CPython, but I'm not so sure it's still
 # important here
-SYMBOL_START = N_TOKENS+30
-del N_TOKENS
 
-_count = SYMBOL_START
 _anoncount = -10
+_count = 0
 
 sym_name = {}
 sym_values = {}
 
+for _name, _value in symbol.__dict__.items():
+    if type(_value) is type(0):
+        _count = max(_count, _value)
+
 def add_symbol( sym ):
-    global _count
     assert type(sym)==str
     if not sym_values.has_key( sym ):
-        val = _count
+        if hasattr(symbol, sym):
+            val = getattr(symbol, sym)
+        else:
+            global _count
+            _count += 1
+            val = _count
         sym_values[sym] = val
         sym_name[val] = sym
         globals()[sym] = val
-        _count += 1
         return val
     return sym_values[ sym ]
 

Added: pypy/dist/pypy/interpreter/pyparser/symbol.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/interpreter/pyparser/symbol.py	Tue Aug 23 15:59:27 2005
@@ -0,0 +1,107 @@
+#! /usr/bin/env python
+
+"""Non-terminal symbols of Python grammar (from "graminit.h")."""
+
+#  This file is automatically generated; please don't muck it up!
+#
+#  To update the symbols in this file, 'cd' to the top directory of
+#  the python source tree after building the interpreter and run:
+#
+#    python Lib/symbol.py
+
+#--start constants--
+single_input = 256
+file_input = 257
+eval_input = 258
+decorator = 259
+decorators = 260
+funcdef = 261
+parameters = 262
+varargslist = 263
+fpdef = 264
+fplist = 265
+stmt = 266
+simple_stmt = 267
+small_stmt = 268
+expr_stmt = 269
+augassign = 270
+print_stmt = 271
+del_stmt = 272
+pass_stmt = 273
+flow_stmt = 274
+break_stmt = 275
+continue_stmt = 276
+return_stmt = 277
+yield_stmt = 278
+raise_stmt = 279
+import_stmt = 280
+import_name = 281
+import_from = 282
+import_as_name = 283
+dotted_as_name = 284
+import_as_names = 285
+dotted_as_names = 286
+dotted_name = 287
+global_stmt = 288
+exec_stmt = 289
+assert_stmt = 290
+compound_stmt = 291
+if_stmt = 292
+while_stmt = 293
+for_stmt = 294
+try_stmt = 295
+except_clause = 296
+suite = 297
+test = 298
+and_test = 299
+not_test = 300
+comparison = 301
+comp_op = 302
+expr = 303
+xor_expr = 304
+and_expr = 305
+shift_expr = 306
+arith_expr = 307
+term = 308
+factor = 309
+power = 310
+atom = 311
+listmaker = 312
+testlist_gexp = 313
+lambdef = 314
+trailer = 315
+subscriptlist = 316
+subscript = 317
+sliceop = 318
+exprlist = 319
+testlist = 320
+testlist_safe = 321
+dictmaker = 322
+classdef = 323
+arglist = 324
+argument = 325
+list_iter = 326
+list_for = 327
+list_if = 328
+gen_iter = 329
+gen_for = 330
+gen_if = 331
+testlist1 = 332
+encoding_decl = 333
+#--end constants--
+
+sym_name = {}
+for _name, _value in globals().items():
+    if type(_value) is type(0):
+        sym_name[_value] = _name
+
+
+def main():
+    import sys
+    import token
+    if len(sys.argv) == 1:
+        sys.argv = sys.argv + ["Include/graminit.h", "Lib/symbol.py"]
+    token.main()
+
+if __name__ == "__main__":
+    main()



More information about the Pypy-commit mailing list