Author: Philip Jenvey <[email protected]>
Branch: py3k
Changeset: r58423:d28fd7705d86
Date: 2012-10-25 15:48 -0700
http://bitbucket.org/pypy/pypy/changeset/d28fd7705d86/

Log:    check for the magic encoding comment even w/ bytes and or
        PyCF_SOURCE_IS_UTF8 (which seems irrelevant now that the default
        encoding is utf8 anyway)

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
@@ -102,7 +102,12 @@
         """
         # Detect source encoding.
         enc = None
-        if bytessrc.startswith("\xEF\xBB\xBF"):
+        if compile_info.flags & consts.PyCF_SOURCE_IS_UTF8:
+            enc = 'utf-8'
+
+        if compile_info.flags & consts.PyCF_IGNORE_COOKIE:
+            textsrc = bytessrc
+        elif bytessrc.startswith("\xEF\xBB\xBF"):
             bytessrc = bytessrc[3:]
             enc = 'utf-8'
             # If an encoding is explicitly given check that it is utf-8.
@@ -111,13 +116,6 @@
                 raise error.SyntaxError("UTF-8 BOM with non-utf8 coding 
cookie",
                                         filename=compile_info.filename)
             textsrc = bytessrc
-        elif compile_info.flags & consts.PyCF_SOURCE_IS_UTF8:
-            enc = 'utf-8'
-            if (not compile_info.flags & consts.PyCF_IGNORE_COOKIE and
-                _check_for_encoding(bytessrc) is not None):
-                raise error.SyntaxError("coding declaration in unicode string",
-                                        filename=compile_info.filename)
-            textsrc = bytessrc
         else:
             enc = _normalize_encoding(_check_for_encoding(bytessrc))
             if enc is None:
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
@@ -168,10 +168,14 @@
         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
-        assert exc.msg == "coding declaration in unicode string"
+        input = "#\nx"
+        info.encoding = None
+        self.parse(input, info=info)
+        assert info.encoding == "utf-8"
+        input = "# coding: latin1\nquux"
+        self.parse(input, info=info)
+        assert info.encoding == "latin1"
         info.flags |= consts.PyCF_IGNORE_COOKIE
         self.parse(input, info=info)
         assert info.encoding == "utf-8"
diff --git a/pypy/module/__builtin__/compiling.py 
b/pypy/module/__builtin__/compiling.py
--- a/pypy/module/__builtin__/compiling.py
+++ b/pypy/module/__builtin__/compiling.py
@@ -45,7 +45,6 @@
         flags |= consts.PyCF_IGNORE_COOKIE
     elif space.isinstance_w(w_source, space.w_bytes):
         source_str = space.bytes0_w(w_source)
-        flags |= consts.PyCF_IGNORE_COOKIE
     else:
         msg = space.wrap(
             "compile() arg 1 must be a string, bytes, AST or code object")
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to