Author: Amaury Forgeot d'Arc <[email protected]>
Branch: py3k
Changeset: r58119:7ac22e106432
Date: 2012-10-15 08:10 +0200
http://bitbucket.org/pypy/pypy/changeset/7ac22e106432/
Log: compile() and eval() accept unicode strings with a "-*- coding: -*-"
cookie, it is simply ignored.
diff --git a/pypy/interpreter/astcompiler/consts.py
b/pypy/interpreter/astcompiler/consts.py
--- a/pypy/interpreter/astcompiler/consts.py
+++ b/pypy/interpreter/astcompiler/consts.py
@@ -20,3 +20,4 @@
PyCF_SOURCE_IS_UTF8 = 0x0100
PyCF_DONT_IMPLY_DEDENT = 0x0200
PyCF_ONLY_AST = 0x0400
+PyCF_IGNORE_COOKIE = 0x0800
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
@@ -113,7 +113,8 @@
textsrc = bytessrc
elif compile_info.flags & consts.PyCF_SOURCE_IS_UTF8:
enc = 'utf-8'
- if _check_for_encoding(bytessrc) is not None:
+ 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
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
@@ -167,11 +167,16 @@
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"
+ info.flags |= consts.PyCF_IGNORE_COOKIE
+ self.parse(input, info=info)
+ assert info.encoding == "utf-8"
+ info.flags &= ~(consts.PyCF_SOURCE_IS_UTF8 | consts.PyCF_IGNORE_COOKIE)
+ #
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"
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
@@ -25,6 +25,13 @@
in addition to any features explicitly specified.
"""
+ ec = space.getexecutioncontext()
+ if flags & ~(ec.compiler.compiler_flags | consts.PyCF_ONLY_AST |
+ consts.PyCF_DONT_IMPLY_DEDENT | consts.PyCF_SOURCE_IS_UTF8):
+ raise OperationError(space.w_ValueError,
+ space.wrap("compile() unrecognized flags"))
+
+ flags |= consts.PyCF_SOURCE_IS_UTF8
ast_node = None
w_ast_type = space.gettypeobject(ast.AST.typedef)
source_str = None
@@ -32,17 +39,11 @@
ast_node = space.interp_w(ast.mod, w_source)
ast_node.sync_app_attrs(space)
elif space.isinstance_w(w_source, space.w_bytes):
- source_str = space.bytes_w(w_source)
+ source_str = space.bytes0_w(w_source)
else:
- source_str = space.str_w(w_source)
- # This flag tells the parser to reject any coding cookies it sees.
- flags |= consts.PyCF_SOURCE_IS_UTF8
+ source_str = space.str0_w(w_source)
+ flags |= consts.PyCF_IGNORE_COOKIE
- ec = space.getexecutioncontext()
- if flags & ~(ec.compiler.compiler_flags | consts.PyCF_ONLY_AST |
- consts.PyCF_DONT_IMPLY_DEDENT | consts.PyCF_SOURCE_IS_UTF8):
- raise OperationError(space.w_ValueError,
- space.wrap("compile() unrecognized flags"))
if not dont_inherit:
caller = ec.gettopframe_nohidden()
if caller:
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit