Author: Philip Jenvey <[email protected]>
Branch: py3k
Changeset: r59855:0430488c1af7
Date: 2013-01-07 12:52 -0800
http://bitbucket.org/pypy/pypy/changeset/0430488c1af7/
Log: fix find_module's encoding check to look at line 2 and be more
forgiving
diff --git a/pypy/module/imp/interp_imp.py b/pypy/module/imp/interp_imp.py
--- a/pypy/module/imp/interp_imp.py
+++ b/pypy/module/imp/interp_imp.py
@@ -4,7 +4,7 @@
from pypy.interpreter.error import OperationError, operationerrfmt
from pypy.interpreter.module import Module
from pypy.interpreter.gateway import unwrap_spec
-from pypy.interpreter.pyparser import pytokenizer
+from pypy.interpreter.pyparser import pyparse
from pypy.objspace.std import unicodetype
from pypy.rlib import streamio
from pypy.module._io.interp_iobase import W_IOBase
@@ -76,10 +76,10 @@
encoding = None
if find_info.modtype == importing.PY_SOURCE:
# try to find the declared encoding
- firstline = stream.readline()
+ top = stream.readline()
+ top += stream.readline()
stream.seek(0, 0) # reset position
- if firstline.startswith('#'):
- encoding = pytokenizer.match_encoding_declaration(firstline)
+ encoding = pyparse._check_for_encoding(top)
if encoding is None:
encoding = unicodetype.getdefaultencoding(space)
#
diff --git a/pypy/module/imp/test/test_import.py
b/pypy/module/imp/test/test_import.py
--- a/pypy/module/imp/test/test_import.py
+++ b/pypy/module/imp/test/test_import.py
@@ -90,6 +90,9 @@
'a=5\nb=6\rc="""hello\r\nworld"""\r', mode='wb')
p.join('mod.py').write(
'a=15\nb=16\rc="""foo\r\nbar"""\r', mode='wb')
+ setuppkg("encoded",
+ # actually a line 2, setuppkg() sets up a line1
+ line2 = "# encoding: iso-8859-1\n")
# create compiled/x.py and a corresponding pyc file
p = setuppkg("compiled", x = "x = 84")
@@ -659,6 +662,12 @@
assert module.__name__ == 'a'
assert module.__file__ == 'invalid_path_name'
+ def test_source_encoding(self):
+ import imp
+ import encoded
+ fd = imp.find_module('line2', encoded.__path__)[0]
+ assert fd.encoding == 'iso-8859-1'
+
class TestAbi:
def test_abi_tag(self):
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit