[pypy-svn] r65682 - in pypy/branch/parser-compiler/pypy/interpreter/pyparser: . test

benjamin at codespeak.net benjamin at codespeak.net
Tue Jun 9 05:26:53 CEST 2009


Author: benjamin
Date: Tue Jun  9 05:26:50 2009
New Revision: 65682

Modified:
   pypy/branch/parser-compiler/pypy/interpreter/pyparser/pyparse.py
   pypy/branch/parser-compiler/pypy/interpreter/pyparser/test/test_pyparse.py
Log:
if the file is encoded, put the whole tree in a encoding_decl node

Modified: pypy/branch/parser-compiler/pypy/interpreter/pyparser/pyparse.py
==============================================================================
--- pypy/branch/parser-compiler/pypy/interpreter/pyparser/pyparse.py	(original)
+++ pypy/branch/parser-compiler/pypy/interpreter/pyparser/pyparse.py	Tue Jun  9 05:26:50 2009
@@ -67,6 +67,7 @@
     def parse_source(self, textsrc, mode="exec", flags=0):
         """Parse a python source according to goal"""
         # Detect source encoding.
+        enc = None
         if textsrc[:3] == '\xEF\xBB\xBF':
             textsrc = textsrc[3:]
             enc = 'utf-8'
@@ -105,6 +106,10 @@
                 msg = "invalid syntax"
             raise new_err(msg, e.lineno, e.column, e.line)
         else:
-            return self.root
+            tree = self.root
         finally:
             self.root = None
+        if enc is not None:
+            # Wrap the tree in an encoding_decl node for the AST builder.
+            tree = parser.Node(pygram.syms.encoding_decl, enc, [tree], 0, 0)
+        return tree

Modified: pypy/branch/parser-compiler/pypy/interpreter/pyparser/test/test_pyparse.py
==============================================================================
--- pypy/branch/parser-compiler/pypy/interpreter/pyparser/test/test_pyparse.py	(original)
+++ pypy/branch/parser-compiler/pypy/interpreter/pyparser/test/test_pyparse.py	Tue Jun  9 05:26:50 2009
@@ -14,6 +14,13 @@
         tree = self.parser.parse_source("name = 32")
         assert self.parser.root is None
 
+    def test_encoding(self):
+        tree = self.parser.parse_source("""# coding: latin-1
+stuff = "nothing"
+""")
+        assert tree.type == syms.encoding_decl
+        assert tree.value == "iso-8859-1"
+
     def test_syntax_error(self):
         parse = self.parser.parse_source
         exc = py.test.raises(SyntaxError, parse, "name another for").value



More information about the Pypy-commit mailing list