[pypy-svn] pypy compile-from-stream: Override Parser.classify() to handle __future__.print_function, and remove python_grammar_no_print.

amauryfa commits-noreply at bitbucket.org
Mon Mar 21 21:47:47 CET 2011


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: compile-from-stream
Changeset: r42827:5244713e15e6
Date: 2011-03-21 14:23 +0100
http://bitbucket.org/pypy/pypy/changeset/5244713e15e6/

Log:	Override Parser.classify() to handle __future__.print_function, and
	remove python_grammar_no_print. Later, this may avoid the first pass
	to detect __future__ statements.

diff --git a/pypy/interpreter/pyparser/pyparse.py b/pypy/interpreter/pyparser/pyparse.py
--- a/pypy/interpreter/pyparser/pyparse.py
+++ b/pypy/interpreter/pyparser/pyparse.py
@@ -217,14 +217,15 @@
 
         return self.build_tree(source_lines, compile_info)
 
+    def classify(self, token_type, value, *args):
+        if self.compile_info.flags & consts.CO_FUTURE_PRINT_FUNCTION:
+            if token_type == self.grammar.KEYWORD_TOKEN and value == 'print':
+                return self.grammar.token_ids[pygram.tokens.NAME]
+        return parser.Parser.classify(self, token_type, value, *args)
+
     def build_tree(self, source_lines, compile_info):
         """Builds the parse tree from a list of source lines"""
 
-        if compile_info.flags & consts.CO_FUTURE_PRINT_FUNCTION:
-            self.grammar = pygram.python_grammar_no_print
-        else:
-            self.grammar = pygram.python_grammar
-
         if source_lines and source_lines[-1]:
             last_line = source_lines[-1]
             if last_line:
@@ -235,6 +236,7 @@
                     source_lines[-1] += '\n'
 
         self.prepare(_targets[compile_info.mode])
+        self.compile_info = compile_info
         tp = 0
         try:
             try:
@@ -264,4 +266,5 @@
         finally:
             # Avoid hanging onto the tree.
             self.root = None
+            self.compile_info = None
         return tree

diff --git a/pypy/interpreter/pyparser/pygram.py b/pypy/interpreter/pyparser/pygram.py
--- a/pypy/interpreter/pyparser/pygram.py
+++ b/pypy/interpreter/pyparser/pygram.py
@@ -19,9 +19,6 @@
 
 
 python_grammar = _get_python_grammar()
-python_grammar_no_print = python_grammar.shared_copy()
-python_grammar_no_print.keyword_ids = python_grammar_no_print.keyword_ids.copy()
-del python_grammar_no_print.keyword_ids["print"]
 
 class _Tokens(object):
     pass


More information about the Pypy-commit mailing list