Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-123339: fix out-of-bounds values in inspect.findsource #123347

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -973,7 +973,10 @@ def findsource(object):
firstlineno = vars(object)['__firstlineno__']
except (TypeError, KeyError):
raise OSError('source code not available')
return lines, firstlineno - 1
lnum = firstlineno - 1
if lnum >= len(lines) or lnum < 0:
raise OSError('lineno is out of bounds')
return lines, lnum

if ismethod(object):
object = object.__func__
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix handling of ``__firstlineno__`` in :func:`!inspect.findsource` for
classes. Patch by Bénédikt Tran.
Loading