Merge pull request #7749 from bluetech/fix-get_source-crash
This commit is contained in:
commit
9bfd14a443
|
@ -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.
|
|
@ -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 = [
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue