diff --git a/_pytest/assertion/rewrite.py b/_pytest/assertion/rewrite.py index e964ea9f8..8f51c30f3 100644 --- a/_pytest/assertion/rewrite.py +++ b/_pytest/assertion/rewrite.py @@ -215,11 +215,17 @@ def _rewrite_test(state, fn): if (not source.startswith(BOM_UTF8) and (not cookie_re.match(source[0:end1]) or not cookie_re.match(source[end1:end2]))): + if hasattr(state, "_indecode"): + return None # encodings imported us again, we don't rewrite + state._indecode = True try: - source.decode("ascii") - except UnicodeDecodeError: - # Let it fail in real import. - return None + try: + source.decode("ascii") + except UnicodeDecodeError: + # Let it fail in real import. + return None + finally: + del state._indecode # On Python versions which are not 2.7 and less than or equal to 3.1, the # parser expects *nix newlines. if REWRITE_NEWLINES: diff --git a/testing/test_assertion.py b/testing/test_assertion.py index 02199ef43..ff633b534 100644 --- a/testing/test_assertion.py +++ b/testing/test_assertion.py @@ -320,3 +320,17 @@ def test_warn_missing(testdir): result.stderr.fnmatch_lines([ "*WARNING*assert statements are not executed*", ]) + +def test_recursion_source_decode(testdir): + testdir.makepyfile(""" + def test_something(): + pass + """) + testdir.makeini(""" + [pytest] + python_files = *.py + """) + result = testdir.runpytest("--collectonly") + result.stdout.fnmatch_lines(""" + + """)