Use inspect module in Code class instead of direct co_* attribute access#14536
Open
nicoddemus wants to merge 1 commit into
Open
Use inspect module in Code class instead of direct co_* attribute access#14536nicoddemus wants to merge 1 commit into
nicoddemus wants to merge 1 commit into
Conversation
As a follow-up to pytest-dev#14493, replace direct co_* accesses in Code.path and Code.getargs with inspect.getfile and inspect.getfullargspec respectively, delegating argument-decoding logic to well-tested stdlib APIs rather than re-implementing it with raw bitmask arithmetic. Code.firstlineno and Code.name still access co_firstlineno and co_name directly: the inspect module has no equivalent for these that does not require reading the source file from disk, which would regress correctness for compile()'d code and performance for every traceback formatting path. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
a121a89 to
fa59a08
Compare
nicoddemus
commented
May 30, 2026
| # co_argcount is needed to exclude kwonly when var=False. | ||
| args, varargs, varkw = inspect.getargs(self.raw) | ||
| if not var: | ||
| return tuple(args[: self.raw.co_argcount]) |
Member
Author
There was a problem hiding this comment.
We still access co_argcount here, but in the end using inspect.getargs seems more future-proof than bitwise handling of co_flags.
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.
As a follow-up to #14493, replace direct co_* accesses in Code.path and Code.getargs with inspect.getfile and inspect.getargs respectively, delegating argument-decoding logic to well-tested stdlib APIs rather than re-implementing it with raw bitmask arithmetic.
Code.firstlineno and Code.name still access co_firstlineno and co_name directly: the inspect module has no equivalent for these that does not require reading the source file from disk, which would regress correctness for compile()'d code and performance for every traceback formatting path.