From 45e1a93cd282ba2e4969dfafab828ed00c24311a Mon Sep 17 00:00:00 2001 From: Sten Wessel Date: Wed, 20 Dec 2023 22:47:57 +0100 Subject: [PATCH 1/2] gh-113329: Catch OSError in doctest finder Fixes #113329, which occurs when running doctests in a class docstring in a REPL environment. Since Python 3.10, an OSError is raised when the class source code cannot be found. --- Doc/library/inspect.rst | 8 +++++--- Lib/doctest.py | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/Doc/library/inspect.rst b/Doc/library/inspect.rst index f8b3e39c4f54f0..251708bc56c831 100644 --- a/Doc/library/inspect.rst +++ b/Doc/library/inspect.rst @@ -560,6 +560,7 @@ Retrieving source code .. function:: getfile(object) Return the name of the (text or binary) file in which an object was defined. + An :exc:`OSError` is raised if the source code cannot be retrieved. This will fail with a :exc:`TypeError` if the object is a built-in module, class, or function. @@ -573,9 +574,10 @@ Retrieving source code .. function:: getsourcefile(object) Return the name of the Python source file in which an object was defined - or ``None`` if no way can be identified to get the source. This - will fail with a :exc:`TypeError` if the object is a built-in module, class, or - function. + or ``None`` if no way can be identified to get the source. An :exc:`OSError` is + raised if the source code cannot be retrieved. + This will fail with a :exc:`TypeError` if the object is a built-in module, + class, or function. .. function:: getsourcelines(object) diff --git a/Lib/doctest.py b/Lib/doctest.py index 114aac62a34e95..284b69594ba6ad 100644 --- a/Lib/doctest.py +++ b/Lib/doctest.py @@ -923,7 +923,7 @@ def find(self, obj, name=None, module=None, globs=None, extraglobs=None): # given object's docstring. try: file = inspect.getsourcefile(obj) - except TypeError: + except (TypeError, OSError): source_lines = None else: if not file: From 6d9f5ccfbe581a6b361cd12fe32dbb6f839019f1 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Wed, 20 Dec 2023 22:05:41 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2023-12-20-22-05-40.gh-issue-113329.1CJy3o.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2023-12-20-22-05-40.gh-issue-113329.1CJy3o.rst diff --git a/Misc/NEWS.d/next/Library/2023-12-20-22-05-40.gh-issue-113329.1CJy3o.rst b/Misc/NEWS.d/next/Library/2023-12-20-22-05-40.gh-issue-113329.1CJy3o.rst new file mode 100644 index 00000000000000..d5abc0ec28dc9b --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-12-20-22-05-40.gh-issue-113329.1CJy3o.rst @@ -0,0 +1 @@ +Fix :exc:`OSError` being raised when trying to run doctests on a class objects in the REPL. Patch by Sten Wessel.