1. Pypy
  2. Untitled project
  3. pypy

Commits

Philip Jenvey  committed d28fd77

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)

  • Participants
  • Parent commits 178958b
  • Branches py3k

Comments (0)

Files changed (3)

File pypy/interpreter/pyparser/pyparse.py

View file
         """
         # 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.
                 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:

File pypy/interpreter/pyparser/test/test_pyparse.py

View file
         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"

File pypy/module/__builtin__/compiling.py

View file
         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")