Skip to content

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

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 5 additions & 3 deletions Doc/library/inspect.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Lib/doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix :exc:`OSError` being raised when trying to run doctests on a class objects in the REPL. Patch by Sten Wessel.