FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Running doctest examples on a class results in OSError in REPL · Issue #113329 · python/cpython · GitHub

Repository navigation

Running doctest examples on a class results in OSError in REPL #113329

Closed

Description

Bug report

Bug description:

In the REPL, running doctests contained in the docstring of a class is broken since version 3.10. Example:

import doctest

class A:
    """Docstring of A, with tests.
    >>> 1 + 2
    3
    """
    pass

doctest.run_docstring_examples(A, globals(), name='A', verbose=True)

In version 3.9, this code works and executes the doctest in the docstring of the class A. In version 3.10 (and onward), the call to doctest.run_docstring_examples fails with OSError: source code not available.

This only fails in the REPL and not in regular Python files. In particular, this also fails in (Jupyter) notebook environments.

Cause

This change in behavior is caused by 48a6255 as a fix for #88814. Previously, inspect.getsourcefile() could only fail with TypeError, but this can now also be OSError. The new exception type is not being handled by doctest, only TypeError is caught:

cpython/Lib/doctest.py

Lines 924 to 927 in 713e428

try:
file = inspect.getsourcefile(obj)
except TypeError:
source_lines = None

The call to inspect.getsourcefile() is only used by doctest to determine line numbers, if available. Thus, failing to find the source file can safely fail silently in environments where line numbers are not available (such as the REPL).

Proposed changes

  • Also catch OSError in the doctest finder (in the linked snippet).
  • Update the documentation to reflect that inspect.getsourcefile() can also fail with OSError.

An alternative would be to change the exception type in 48a6255 to also be TypeError in the case the source file is not available. I do not think this a desired change.

I am happy to contribute a pull request with these changes.

CPython versions tested on:

3.9, 3.10, 3.11

Operating systems tested on:

Linux

Linked PRs

Activity

  1. added
    stdlibStandard Library Python modules in the Lib/ directory
    3.11only security fixes
    3.12only security fixes
    3.13only security fixes
    on Dec 20, 2023
  2. gaogaotiantian commented on Dec 20, 2023

    Member

    This makes sense to me as we documented inspect.getsourcelines that it could raise OSError. The changes to doctest is reasonable to me as well.

    I think the change should not be too difficult, so maybe you can make the PR if you want? I'm fairly confident that the doctest fix would be accepted, even if it's not, the documentation should definitely be updated.

  3. added 2 commits that reference this issue on Dec 20, 2023
  4. serhiy-storchaka commented on Dec 22, 2023

    Member

    Thank you for your report and PR @stenwessel. It LGTM in general if we keep OSError (we need also to add versionchanged directives and a What's New entry). But I wonder whether changing the exception type to TypeError is a better solution. I do not feel that OSError fits here.

  5. serhiy-storchaka commented on Dec 22, 2023

    Member

    @ambv, please take your attention.

  6. stenwessel commented on Dec 22, 2023

    ContributorAuthor

    Hi @serhiy-storchaka, I agree that the OSError might be an undesired exception type in this case, especially since this may break functionality in more places than just this doctest issue.

    On the other hand, inspect.getsourcelines and inspect.getsource do fail with OSError as well when the source code cannot be found since version 3.3 (and this is documented as such). Additionally changing to TypeError will break code that catches TypeError assuming this means builtin class, while this is not necessarily the case.

    Either way, I am of course happy to change the PR when necessary (also with the other updates for e.g. documentation and release notes, as I am not entirely sure what is required)

  7. mforbes commented on Aug 19, 2025

    Is there a workaround for this that would allow use in Jupyter notebooks?

  8. deleted a comment from on Oct 14, 2025
  9. added
    3.14bugs and security fixes
    3.15bugs and security fixes
    3.16new features, bugs and security fixes
    and removed
    3.11only security fixes
    3.12only security fixes
    on Aug 13, 2026
  10. added a commit that references this issue on Aug 13, 2026
  11. added 2 commits that reference this issue on Aug 13, 2026
  12. added a commit that references this issue on Aug 14, 2026
  13. added a commit that references this issue on Aug 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.13only security fixes3.14bugs and security fixes3.15bugs and security fixes3.16new features, bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytopic-replRelated to the interactive shelltype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL