[Pytest-commit] commit/pytest: gutworth: fix coding cookie detection logic

commits-noreply at bitbucket.org commits-noreply at bitbucket.org
Sat Oct 5 21:03:17 CEST 2013


1 new commit in pytest:

https://bitbucket.org/hpk42/pytest/commits/95fc848e108a/
Changeset:   95fc848e108a
User:        gutworth
Date:        2013-10-05 21:03:04
Summary:     fix coding cookie detection logic
Affected #:  3 files

diff -r e669246287ee862e662587c29341174418a987c4 -r 95fc848e108a9917b2261593e8b3ca26147b42db CHANGELOG
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,3 +1,8 @@
+Changes between 2.4.2 and 2.4.3
+-----------------------------------
+
+- In assertion rewriting mode on Python 2, fix the detection of coding cookies.
+
 Changes between 2.4.1 and 2.4.2
 -----------------------------------
 

diff -r e669246287ee862e662587c29341174418a987c4 -r 95fc848e108a9917b2261593e8b3ca26147b42db _pytest/assertion/rewrite.py
--- a/_pytest/assertion/rewrite.py
+++ b/_pytest/assertion/rewrite.py
@@ -196,7 +196,7 @@
 RN = "\r\n".encode("utf-8")
 N = "\n".encode("utf-8")
 
-cookie_re = re.compile("coding[:=]\s*[-\w.]+")
+cookie_re = re.compile(r"^[ \t\f]*#.*coding[:=][ \t]*[-\w.]+")
 BOM_UTF8 = '\xef\xbb\xbf'
 
 def _rewrite_test(state, fn):
@@ -220,8 +220,8 @@
         end1 = source.find("\n")
         end2 = source.find("\n", end1 + 1)
         if (not source.startswith(BOM_UTF8) and
-            (not cookie_re.match(source[0:end1]) or
-            not cookie_re.match(source[end1:end2]))):
+            cookie_re.match(source[0:end1]) is None and
+            cookie_re.match(source[end1:end2]) is None):
             if hasattr(state, "_indecode"):
                 return None  # encodings imported us again, we don't rewrite
             state._indecode = True

diff -r e669246287ee862e662587c29341174418a987c4 -r 95fc848e108a9917b2261593e8b3ca26147b42db testing/test_assertrewrite.py
--- a/testing/test_assertrewrite.py
+++ b/testing/test_assertrewrite.py
@@ -441,7 +441,6 @@
             '* 3 passed*',
         ])
 
-
     @pytest.mark.skipif("sys.version_info[0] >= 3")
     def test_assume_ascii(self, testdir):
         content = "u'\xe2\x99\xa5'"
@@ -450,6 +449,13 @@
         assert res.ret != 0
         assert "SyntaxError: Non-ASCII character" in res.stdout.str()
 
+    @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 -*-
+u"St\xc3\xa4d"
+def test_rewritten():
+    assert "@py_builtins" in globals()""", "wb")
+        assert testdir.runpytest().ret == 0
 
     def test_write_pyc(self, testdir, tmpdir, monkeypatch):
         from _pytest.assertion.rewrite import _write_pyc

Repository URL: https://bitbucket.org/hpk42/pytest/

--

This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.


More information about the pytest-commit mailing list