8000 gh-111881: Use lazy import in doctest by vstinner · Pull Request #111888 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

8000 gh-111881: Use lazy import in doctest #111888

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

Closed
wants to merge 1 commit into from
Closed
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
8000
Diff view
8 changes: 6 additions & 2 deletions Lib/doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ def _test():
]

import __future__
import difflib
import inspect
import linecache
import os
import pdb
import re
Expand Down 10000 Expand Up @@ -932,6 +930,8 @@ def find(self, obj, name=None, module=None, globs=None, extraglobs=None):
if file is None:
source_lines = None
else:
import linecache

if module is not None:
# Supply the module globals in case the module was
# originally loaded via a PEP 302 loader and
Expand Down Expand Up @@ -1528,6 +1528,8 @@ def out(s):
self.debugger.reset()
pdb.set_trace = self.debugger.set_trace

import linecache
Copy link
Member
@AlexWaygood AlexWaygood Nov 9, 2023

Choose a reason for hiding this comment

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

Not sure there's much point in making this one a lazy import. How many times does someone import doctest and then not call this method? It's pretty core to the module's functionality, no?

If there are plausible, common situations where someone might import doctest but not call this method, it's worth it to make it a lazy import. But if it just gets imported anyway as soon as doctest is actually used, I think it would be better to continue importing it eagerly at the top of the module, as we're doing now.

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 wrote this change while trying hard to reduce the number of imports in libregrtest. Maybe #111884 makes this change less important.

I converted this PR as a draft for now. I will see how it goes with other changes to decide later if this change is "needed" or not.


# Patch linecache.getlines, so we can see the example's source
# when we're inside the debugger.
self.save_linecache_getlines = linecache.getlines
Expand Down Expand Up @@ -1738,6 +1740,8 @@ def output_difference(self, example, got, optionflags):

# Check if we should use diff.
if self._do_a_fancy_diff(want, got, optionflags):
import difflib

# Split want & got into lines.
want_lines = want.splitlines(keepends=True)
got_lines = got.splitlines(keepends=True)
Expand Down
0