https://github.com/python/cpython/commit/791f38c886426549c30f41db0008e84562da5149
commit: 791f38c886426549c30f41db0008e84562da5149
branch: 3.15
author: Miss Islington (bot) <[email protected]>
committer: hugovk <[email protected]>
date: 2026-08-14T17:48:04+09:00
summary:

[3.15] gh-113329: Catch OSError in doctest finder and document that 
inspect.getsourcefile/getfile can raise OSError (GH-113335) (#155658)

gh-113329: Catch OSError in doctest finder and document that 
inspect.getsourcefile/getfile can raise OSError (GH-113335)

Fixes GH-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.
(cherry picked from commit 025e7d278e29f3df2e95ed9daf9d9835760564c8)

Co-authored-by: Sten Wessel <[email protected]>

files:
A Misc/NEWS.d/next/Library/2023-12-20-22-05-40.gh-issue-113329.1CJy3o.rst
M Doc/library/inspect.rst
M Lib/doctest.py

diff --git a/Doc/library/inspect.rst b/Doc/library/inspect.rst
index eddc98824d4810..07b0b005ceb3f5 100644
--- a/Doc/library/inspect.rst
+++ b/Doc/library/inspect.rst
@@ -743,6 +743,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.
 
@@ -756,9 +757,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 be950079e396de..7311ce4c92693b 100644
--- a/Lib/doctest.py
+++ b/Lib/doctest.py
@@ -922,7 +922,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:
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.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to