Skip to content

Commit b64a531

Browse files
committed
pythongh-111881: Use lazy import in doctest
Use lazy import for difflib and linecache imports in doctest to speedup Python startup and reduce the number of imports when running tests.
1 parent cc18b88 commit b64a531

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

Lib/doctest.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,7 @@ def _test():
9393
]
9494

9595
import __future__
96-
import difflib
9796
import inspect
98-
import linecache
9997
import os
10098
import pdb
10199
import re
@@ -932,6 +930,8 @@ def find(self, obj, name=None, module=None, globs=None, extraglobs=None):
932930
if file is None:
933931
source_lines = None
934932
else:
933+
import linecache
934+
935935
if module is not None:
936936
# Supply the module globals in case the module was
937937
# originally loaded via a PEP 302 loader and
@@ -1528,6 +1528,8 @@ def out(s):
15281528
self.debugger.reset()
15291529
pdb.set_trace = self.debugger.set_trace
15301530

1531+
import linecache
1532+
15311533
# Patch linecache.getlines, so we can see the example's source
15321534
# when we're inside the debugger.
15331535
self.save_linecache_getlines = linecache.getlines
@@ -1738,6 +1740,8 @@ def output_difference(self, example, got, optionflags):
17381740

17391741
# Check if we should use diff.
17401742
if self._do_a_fancy_diff(want, got, optionflags):
1743+
import difflib
1744+
17411745
# Split want & got into lines.
17421746
want_lines = want.splitlines(keepends=True)
17431747
got_lines = got.splitlines(keepends=True)

0 commit comments

Comments
 (0)