[pypy-svn] pypy compile-from-stream: Fix encoding detection when source starts with \n.

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


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

Log:	Fix encoding detection when source starts with \n.

diff --git a/pypy/interpreter/pyparser/test/test_pyparse.py b/pypy/interpreter/pyparser/test/test_pyparse.py
--- a/pypy/interpreter/pyparser/test/test_pyparse.py
+++ b/pypy/interpreter/pyparser/test/test_pyparse.py
@@ -38,27 +38,38 @@
 """, info=info)
         assert tree.type == syms.file_input
         assert info.encoding == "iso-8859-1"
+
         sentence = u"u'Die M&#228;nner &#228;rgen sich!'"
         input = (u"# coding: utf-7\nstuff = %s" % (sentence,)).encode("utf-7")
         tree = self.parse(input, info=info)
         assert info.encoding == "utf-7"
+
         input = "# coding: iso-8859-15\nx"
         self.parse(input, info=info)
         assert info.encoding == "iso-8859-15"
+
+        input = "\n# coding: iso-8859-15\nx"
+        self.parse(input, info=info)
+        assert info.encoding == "iso-8859-15"
+
         input = "\xEF\xBB\xBF# coding: utf-8\nx"
         self.parse(input, info=info)
         assert info.encoding == "utf-8"
+
         input = "# coding: utf-8\nx"
         info.flags |= consts.PyCF_SOURCE_IS_UTF8
         exc = py.test.raises(SyntaxError, self.parse, input, info=info).value
         info.flags &= ~consts.PyCF_SOURCE_IS_UTF8
         assert exc.msg == "coding declaration in unicode string"
+
         input = "\xEF\xBB\xBF# coding: latin-1\nx"
         exc = py.test.raises(SyntaxError, self.parse, input).value
         assert exc.msg == "UTF-8 BOM with non-utf8 coding cookie"
+
         input = "# coding: not-here"
         exc = py.test.raises(SyntaxError, self.parse, input).value
         assert exc.msg == "Unknown encoding: not-here"
+
         input = u"# coding: ascii\n\xe2".encode('utf-8')
         exc = py.test.raises(SyntaxError, self.parse, input).value
         if isinstance(self, TestPythonFileParser):

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
@@ -42,7 +42,7 @@
         enc = _check_line_for_encoding(s1[:eol])
     if enc:
         return enc
-    if eol:
+    if eol >= 0:
         if s2:
             s = s1 + s2
         else:


More information about the Pypy-commit mailing list