Skip to content

gh-108885: Use subtests for doctest examples run by unittest #134890

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 1 commit into
base: main
Choose a base branch
from

Conversation

serhiy-storchaka
Copy link
Member

@serhiy-storchaka serhiy-storchaka commented May 29, 2025

Run each example as a subtest in unit tests synthesized by doctest.DocFileSuite() and doctest.DocTestSuite().


📚 Documentation preview 📚: https://cpython-previews--134890.org.readthedocs.build/

Lib/doctest.py Outdated
Comment on lines 1409 to 1411
except:
exception = sys.exc_info()
exc_info = sys.exc_info()
exc_info = (*exc_info[:2], exc_info[2].tb_next)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to use sys here:

except BaseException as e:
    exc_info = type(e), e, e.__traceback__.tb_next

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, save just the exception here and access its parts below where you need them.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does not work, because a traceback here is a not a real traceback. You cannot set it as the part of the exception. Fortunately, all traceback formatting functions and TestCase.addSubTest() work with the exc_info triple.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, here it is a real traceback, but in other place we use a mock traceback, so it is easier to use the same pattern here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I meant that you extract it from the exception object when you need it below (in this function) and pass that to the internal functions that want the triplet. Just get it from the exception and not from sys.exc_info(). It's the same thing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is actually a part of #134858.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I applied your suggestion there.

Run each example as a subtest in unit tests synthesized by
doctest.DocFileSuite() and doctest.DocTestSuite().
@serhiy-storchaka
Copy link
Member Author

Before merging this PR I am going to create a merge PR for documentation fixes, because the current documentation has some errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants