Skip to content

Add a co_lastlineno in code object #119811

Closed as not planned
Closed as not planned
@gaogaotiantian

Description

@gaogaotiantian

Feature or enhancement

Proposal:

We have a co_firstlineno in code object which is useful in many cases. However, co_lastlineno is very useful as well. With the two numbers we can get the full body of the defined function, rather than doing something crazy like:

cpython/Lib/inspect.py

Lines 1194 to 1211 in 13a5fdc

def getblock(lines):
"""Extract the block of code at the top of the given list of lines."""
blockfinder = BlockFinder()
try:
tokens = tokenize.generate_tokens(iter(lines).__next__)
for _token in tokens:
blockfinder.tokeneater(*_token)
except (EndOfBlock, IndentationError):
pass
except SyntaxError as e:
if "unmatched" not in e.msg:
raise e from None
_, *_token_info = _token
try:
blockfinder.tokeneater(tokenize.NEWLINE, *_token_info)
except (EndOfBlock, IndentationError):
pass
return lines[:blockfinder.last]

Not only inspect will benefit from the number, pdb will know whether a breakpoint is set in the function easily so it can turn off the trace in irrelevant functions.

I'm not the expert in compiler but I think the data should be there already, we just need to write it down. The code object itself is not super size sensitive (it's already pretty big, an extra integer is nothing) so it really should not cost anything.

@iritkatriel could you share your thoughts on this?

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions