Skip to content

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

@stenwessel

Description

@stenwessel

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

added
stdlibPython modules in the Lib dir
3.11only security fixes
3.12only security fixes
3.13bugs and security fixes
on Dec 20, 2023
gaogaotiantian

gaogaotiantian commented on Dec 20, 2023

@gaogaotiantian
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.

added a commit that references this issue on Dec 20, 2023

pythongh-113329: Catch OSError in doctest finder

45e1a93
added a commit that references this issue on Dec 20, 2023
serhiy-storchaka

serhiy-storchaka commented on Dec 22, 2023

@serhiy-storchaka
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.

serhiy-storchaka

serhiy-storchaka commented on Dec 22, 2023

@serhiy-storchaka
Member

@ambv, please take your attention.

stenwessel

stenwessel commented on Dec 22, 2023

@stenwessel
Author

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)

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.11only security fixes3.12only security fixes3.13bugs and security fixesstdlibPython modules in the Lib dirtopic-replRelated to the interactive shelltype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Participants

      @serhiy-storchaka@stenwessel@gaogaotiantian@AlexWaygood

      Issue actions

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