diff options
author | Benjamin Peterson <benjamin@python.org> | 2013-10-05 15:16:08 -0400 |
---|---|---|
committer | Benjamin Peterson <benjamin@python.org> | 2013-10-05 15:16:08 -0400 |
commit | 56e6ae567c85106cdc24d00c8e901126a689f287 (patch) | |
tree | e02d195c3cdab8952e3db329c0ba0e60c87cb2f0 | |
parent | 33b663e03d7673fc92d7dee27c845d0431890b44 (diff) | |
download | pytest-56e6ae567c85106cdc24d00c8e901126a689f287.tar.gz |
fix detection of the coding cookie when it is on the second line of the file (fixes #330)
-rw-r--r-- | CHANGELOG | 3 | ||||
-rw-r--r-- | _pytest/assertion/rewrite.py | 2 | ||||
-rw-r--r-- | testing/test_assertrewrite.py | 11 |
3 files changed, 13 insertions, 3 deletions
@@ -1,7 +1,8 @@ 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 ----------------------------------- diff --git a/_pytest/assertion/rewrite.py b/_pytest/assertion/rewrite.py index 8ef27ac15..f79698ae4 100644 --- a/_pytest/assertion/rewrite.py +++ b/_pytest/assertion/rewrite.py @@ -221,7 +221,7 @@ def _rewrite_test(state, fn): end2 = source.find("\n", end1 + 1) if (not source.startswith(BOM_UTF8) 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"): return None # encodings imported us again, we don't rewrite state._indecode = True diff --git a/testing/test_assertrewrite.py b/testing/test_assertrewrite.py index fa2d5380f..db7cc5063 100644 --- a/testing/test_assertrewrite.py +++ b/testing/test_assertrewrite.py @@ -451,7 +451,16 @@ class TestAssertionRewriteHookDetails(object): @pytest.mark.skipif("sys.version_info[0] >= 3") 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" def test_rewritten(): assert "@py_builtins" in globals()""", "wb") |