assertion/rewrite: fix internal error on collection error due to decorated function#7749
Merged
Merged
Conversation
…rated function For decorated functions, the lineno of the FunctionDef AST node points to the `def` line, not to the first decorator line. On the other hand, in code objects, the `co_firstlineno` points to the first decorator line. Assertion rewriting inserts some imports to code it rewrites. The imports are inserted at the lineno of the first statement in the AST. In turn, the code object compiled from the rewritten AST uses the lineno of the first statement (which is the first inserted import). This means that given a module like this, ```py @foo @bar def baz(): pass ``` the lineno of the code object without assertion rewriting (`--assertion=plain`) is 1, but with assertion rewriting it is 3. And *this* causes some issues for the exception repr when e.g. the decorator line is invalid and raises during collection. The code becomes confused and crashes with INTERNALERROR> File "_pytest/_code/code.py", line 638, in get_source INTERNALERROR> lines.append(space_prefix + source.lines[line_index].strip()) INTERNALERROR> IndexError: list index out of range Fix it by special casing decorators. Maybe there are other cases like this but off hand I can't think of another Python construct where the lineno of the item would be after its first line, and this is the only such issue we have had reported.
DahlitzFlorian
approved these changes
Sep 13, 2020
DahlitzFlorian
left a comment
Member
There was a problem hiding this comment.
Looks good to me. However, someone else should have a look at it, too.
nicoddemus
added a commit
to nicoddemus/pytest
that referenced
this pull request
Sep 19, 2020
Member
|
Backport: #7771 |
nicoddemus
added a commit
that referenced
this pull request
Sep 19, 2020
[6.0.x] Merge pull request #7749 from bluetech/fix-get_source-crash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4984.
Note: interesting change in the 2nd commit, first one is just preparation.
For decorated functions, the lineno of the FunctionDef AST node points to the
defline, not to the first decorator line. On the other hand, in code objects, theco_firstlinenopoints to the first decorator line.Assertion rewriting inserts some imports to code it rewrites. The imports are inserted at the lineno of the first statement in the AST. In turn, the code object compiled from the rewritten AST uses the lineno of the first statement (which is the first inserted import).
This means that given a module like this,
the lineno of the code object without assertion rewriting (
--assertion=plain) is 1, but with assertion rewriting it is 3.And this causes some issues for the exception repr when e.g. the decorator line is invalid and raises during collection. The code becomes confused and crashes with
Fix it by special casing decorators. Maybe there are other cases like this but off hand I can't think of another Python construct where the lineno of the item would be after its first line, and this is the only such issue we have had reported.