[py-svn] commit/pytest: gutworth: rewrite file newlines when the python parser is picky

Bitbucket commits-noreply at bitbucket.org
Tue Sep 20 23:53:13 CEST 2011


1 new changeset in pytest:

http://bitbucket.org/hpk42/pytest/changeset/a7e4bf1c36af/
changeset:   a7e4bf1c36af
user:        gutworth
date:        2011-09-20 23:53:07
summary:     rewrite file newlines when the python parser is picky
affected #:  3 files (-1 bytes)

--- a/CHANGELOG	Mon Sep 12 08:57:35 2011 +0200
+++ b/CHANGELOG	Tue Sep 20 17:53:07 2011 -0400
@@ -1,6 +1,7 @@
 Changes between 2.1.1 and [NEXT VERSION]
 ----------------------------------------
 
+- fix assertion rewriting on files with windows newlines on some Python versions
 - refine test discovery by package/module name (--pyargs), thanks Florian Mayer
 - fix issue69 / assertion rewriting fixed on some boolean operations
 - fix issue68 / packages now work with assertion rewriting


--- a/_pytest/assertion/rewrite.py	Mon Sep 12 08:57:35 2011 +0200
+++ b/_pytest/assertion/rewrite.py	Tue Sep 20 17:53:07 2011 -0400
@@ -38,6 +38,8 @@
 PYC_EXT = ".py" + "c" if __debug__ else "o"
 PYC_TAIL = "." + PYTEST_TAG + PYC_EXT
 
+REWRITE_NEWLINES = sys.version_info[:2] != (2, 7) and sys.version_info < (3, 2)
+
 class AssertionRewritingHook(object):
     """Import hook which rewrites asserts."""
 
@@ -181,12 +183,19 @@
         fp.close()
     return True
 
+RN = "\r\n".encode("utf-8")
+N = "\n".encode("utf-8")
+
 def _rewrite_test(state, fn):
     """Try to read and rewrite *fn* and return the code object."""
     try:
         source = fn.read("rb")
     except EnvironmentError:
         return None
+    # On Python versions which are not 2.7 and less than or equal to 3.1, the
+    # parser expects *nix newlines.
+    if REWRITE_NEWLINES:
+        source = source.replace(RN, N) + N
     try:
         tree = ast.parse(source)
     except SyntaxError:


--- a/testing/test_assertrewrite.py	Mon Sep 12 08:57:35 2011 +0200
+++ b/testing/test_assertrewrite.py	Tue Sep 20 17:53:07 2011 -0400
@@ -357,3 +357,9 @@
 def test_rewritten():
     assert "@py_builtins" in globals()""")
         assert testdir.runpytest().ret == 0
+
+    def test_translate_newlines(self, testdir):
+        content = "def test_rewritten():\r\n assert '@py_builtins' in globals()"
+        b = content.encode("utf-8")
+        testdir.tmpdir.join("test_newlines.py").write(b, "wb")
+        assert testdir.runpytest().ret == 0

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