Merge pull request #7749 from bluetech/fix-get_source-crash

This commit is contained in:
Bruno Oliveira 2020-09-19 10:38:32 -03:00 committed by GitHub
commit 9bfd14a443
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 4 deletions

View File

@ -0,0 +1,3 @@
Fixed an internal error crash with ``IndexError: list index out of range`` when
collecting a module which starts with a decorated function, the decorator
raises, and assertion rewriting is enabled.

View File

@ -687,13 +687,18 @@ class AssertionRewriter(ast.NodeVisitor):
return return
expect_docstring = False expect_docstring = False
elif ( elif (
not isinstance(item, ast.ImportFrom) isinstance(item, ast.ImportFrom)
or item.level > 0 and item.level == 0
or item.module != "__future__" and item.module == "__future__"
): ):
lineno = item.lineno pass
else:
break break
pos += 1 pos += 1
# Special case: for a decorated function, set the lineno to that of the
# first decorator, not the `def`. Issue #4984.
if isinstance(item, ast.FunctionDef) and item.decorator_list:
lineno = item.decorator_list[0].lineno
else: else:
lineno = item.lineno lineno = item.lineno
imports = [ imports = [

View File

@ -1393,3 +1393,17 @@ class TestImportModeImportlib:
"* 1 failed in *", "* 1 failed in *",
] ]
) )
def test_does_not_crash_on_error_from_decorated_function(testdir: Testdir) -> None:
"""Regression test for an issue around bad exception formatting due to
assertion rewriting mangling lineno's (#4984)."""
testdir.makepyfile(
"""
@pytest.fixture
def a(): return 4
"""
)
result = testdir.runpytest()
# Not INTERNAL_ERROR
assert result.ret == ExitCode.INTERRUPTED