Skip to content

Commit 45e1a93

Browse files
committed
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.
1 parent 713e428 commit 45e1a93

2 files changed

Lines changed: 6 additions & 4 deletions

File tree

Doc/library/inspect.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,7 @@ Retrieving source code
560560
.. function:: getfile(object)
561561

562562
Return the name of the (text or binary) file in which an object was defined.
563+
An :exc:`OSError` is raised if the source code cannot be retrieved.
563564
This will fail with a :exc:`TypeError` if the object is a built-in module,
564565
class, or function.
565566

@@ -573,9 +574,10 @@ Retrieving source code
573574
.. function:: getsourcefile(object)
574575

575576
Return the name of the Python source file in which an object was defined
576-
or ``None`` if no way can be identified to get the source. This
577-
will fail with a :exc:`TypeError` if the object is a built-in module, class, or
578-
function.
577+
or ``None`` if no way can be identified to get the source. An :exc:`OSError` is
578+
raised if the source code cannot be retrieved.
579+
This will fail with a :exc:`TypeError` if the object is a built-in module,
580+
class, or function.
579581

580582

581583
.. function:: getsourcelines(object)

Lib/doctest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,7 @@ def find(self, obj, name=None, module=None, globs=None, extraglobs=None):
923923
# given object's docstring.
924924
try:
925925
file = inspect.getsourcefile(obj)
926-
except TypeError:
926+
except (TypeError, OSError):
927927
source_lines = None
928928
else:
929929
if not file:

0 commit comments

Comments
 (0)