fix recursion within import hook and source.decode in particular

This commit is contained in:
holger krekel 2013-04-30 12:05:58 +02:00
parent c5f9958783
commit 05c4ecf892
2 changed files with 24 additions and 4 deletions

View File

@ -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:
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:

View File

@ -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("""
<Module*>
""")