fix detection of the coding cookie when it is on the second line of the file (fixes #330)

This commit is contained in:
Benjamin Peterson 2013-10-05 15:16:08 -04:00
parent 33b663e03d
commit 56e6ae567c
3 changed files with 13 additions and 3 deletions

View File

@ -1,7 +1,8 @@
Changes between 2.4.2 and 2.4.3 Changes between 2.4.2 and 2.4.3
----------------------------------- -----------------------------------
- In assertion rewriting mode on Python 2, fix the detection of coding cookies. - In assertion rewriting mode on Python 2, fix the detection of coding
cookies. See issue #330.
Changes between 2.4.1 and 2.4.2 Changes between 2.4.1 and 2.4.2
----------------------------------- -----------------------------------

View File

@ -221,7 +221,7 @@ def _rewrite_test(state, fn):
end2 = source.find("\n", end1 + 1) end2 = source.find("\n", end1 + 1)
if (not source.startswith(BOM_UTF8) and if (not source.startswith(BOM_UTF8) and
cookie_re.match(source[0:end1]) is None and cookie_re.match(source[0:end1]) is None and
cookie_re.match(source[end1:end2]) is None): cookie_re.match(source[end1 + 1:end2]) is None):
if hasattr(state, "_indecode"): if hasattr(state, "_indecode"):
return None # encodings imported us again, we don't rewrite return None # encodings imported us again, we don't rewrite
state._indecode = True state._indecode = True

View File

@ -451,7 +451,16 @@ class TestAssertionRewriteHookDetails(object):
@pytest.mark.skipif("sys.version_info[0] >= 3") @pytest.mark.skipif("sys.version_info[0] >= 3")
def test_detect_coding_cookie(self, testdir): def test_detect_coding_cookie(self, testdir):
testdir.tmpdir.join("test_utf8.py").write("""# -*- coding: utf-8 -*- testdir.tmpdir.join("test_cookie.py").write("""# -*- coding: utf-8 -*-
u"St\xc3\xa4d"
def test_rewritten():
assert "@py_builtins" in globals()""", "wb")
assert testdir.runpytest().ret == 0
@pytest.mark.skipif("sys.version_info[0] >= 3")
def test_detect_coding_cookie_second_line(self, testdir):
testdir.tmpdir.join("test_cookie.py").write("""#!/usr/bin/env python
# -*- coding: utf-8 -*-
u"St\xc3\xa4d" u"St\xc3\xa4d"
def test_rewritten(): def test_rewritten():
assert "@py_builtins" in globals()""", "wb") assert "@py_builtins" in globals()""", "wb")