rewrite file newlines when the python parser is picky
This commit is contained in:
parent
5c32421f2e
commit
2bc4065a00
|
@ -1,6 +1,7 @@
|
||||||
Changes between 2.1.1 and [NEXT VERSION]
|
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
|
- refine test discovery by package/module name (--pyargs), thanks Florian Mayer
|
||||||
- fix issue69 / assertion rewriting fixed on some boolean operations
|
- fix issue69 / assertion rewriting fixed on some boolean operations
|
||||||
- fix issue68 / packages now work with assertion rewriting
|
- fix issue68 / packages now work with assertion rewriting
|
||||||
|
|
|
@ -38,6 +38,8 @@ else:
|
||||||
PYC_EXT = ".py" + "c" if __debug__ else "o"
|
PYC_EXT = ".py" + "c" if __debug__ else "o"
|
||||||
PYC_TAIL = "." + PYTEST_TAG + PYC_EXT
|
PYC_TAIL = "." + PYTEST_TAG + PYC_EXT
|
||||||
|
|
||||||
|
REWRITE_NEWLINES = sys.version_info[:2] != (2, 7) and sys.version_info < (3, 2)
|
||||||
|
|
||||||
class AssertionRewritingHook(object):
|
class AssertionRewritingHook(object):
|
||||||
"""Import hook which rewrites asserts."""
|
"""Import hook which rewrites asserts."""
|
||||||
|
|
||||||
|
@ -181,12 +183,19 @@ def _write_pyc(co, source_path, pyc):
|
||||||
fp.close()
|
fp.close()
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
RN = "\r\n".encode("utf-8")
|
||||||
|
N = "\n".encode("utf-8")
|
||||||
|
|
||||||
def _rewrite_test(state, fn):
|
def _rewrite_test(state, fn):
|
||||||
"""Try to read and rewrite *fn* and return the code object."""
|
"""Try to read and rewrite *fn* and return the code object."""
|
||||||
try:
|
try:
|
||||||
source = fn.read("rb")
|
source = fn.read("rb")
|
||||||
except EnvironmentError:
|
except EnvironmentError:
|
||||||
return None
|
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:
|
try:
|
||||||
tree = ast.parse(source)
|
tree = ast.parse(source)
|
||||||
except SyntaxError:
|
except SyntaxError:
|
||||||
|
|
|
@ -357,3 +357,9 @@ def test_optimized():
|
||||||
def test_rewritten():
|
def test_rewritten():
|
||||||
assert "@py_builtins" in globals()""")
|
assert "@py_builtins" in globals()""")
|
||||||
assert testdir.runpytest().ret == 0
|
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
|
||||||
|
|
Loading…
Reference in New Issue