From be64bc8b4f87491298750506297166c1da642ad0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Mon, 26 Aug 2024 18:33:30 +0200 Subject: [PATCH 1/3] fix out-of-bounds linenos in `findsource` --- Lib/inspect.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Lib/inspect.py b/Lib/inspect.py index 90c44cf74007a8..12d9646671e348 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -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__ From 4906c2ddddf0f7faa68c2b80135c6d095ec14b0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Mon, 26 Aug 2024 18:35:35 +0200 Subject: [PATCH 2/3] blurb --- .../next/Library/2024-08-26-18-35-32.gh-issue-123339.o6rDJ9.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2024-08-26-18-35-32.gh-issue-123339.o6rDJ9.rst diff --git a/Misc/NEWS.d/next/Library/2024-08-26-18-35-32.gh-issue-123339.o6rDJ9.rst b/Misc/NEWS.d/next/Library/2024-08-26-18-35-32.gh-issue-123339.o6rDJ9.rst new file mode 100644 index 00000000000000..225c368510b3c6 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-08-26-18-35-32.gh-issue-123339.o6rDJ9.rst @@ -0,0 +1,2 @@ +Fix handling of ``__firstlineno__`` in :func:`inspect.findsource` for +classes. Patch by Bénédikt Tran. From ba2580314f9caed51dbec3869ef3d4f1d402eabd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Mon, 26 Aug 2024 18:43:54 +0200 Subject: [PATCH 3/3] inspect.findsource does not exist? --- .../next/Library/2024-08-26-18-35-32.gh-issue-123339.o6rDJ9.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2024-08-26-18-35-32.gh-issue-123339.o6rDJ9.rst b/Misc/NEWS.d/next/Library/2024-08-26-18-35-32.gh-issue-123339.o6rDJ9.rst index 225c368510b3c6..965c51b2282c91 100644 --- a/Misc/NEWS.d/next/Library/2024-08-26-18-35-32.gh-issue-123339.o6rDJ9.rst +++ b/Misc/NEWS.d/next/Library/2024-08-26-18-35-32.gh-issue-123339.o6rDJ9.rst @@ -1,2 +1,2 @@ -Fix handling of ``__firstlineno__`` in :func:`inspect.findsource` for +Fix handling of ``__firstlineno__`` in :func:`!inspect.findsource` for classes. Patch by Bénédikt Tran.