Skip to content

Commit 276f791

Browse files
committed
Don't reify lazy imports when completing global names in the REPL
fancycompleter's global_matches() eval()ed every matching name to pick its color, which imported lazily imported modules on <Tab>. Look the value up in the namespace and builtins instead, as rlcompleter does.
1 parent 5a22a62 commit 276f791

3 files changed

Lines changed: 12 additions & 4 deletions

File tree

‎Lib/_pyrepl/fancycompleter.py‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# All Rights Reserved
55
"""Colorful tab completion for Python prompt"""
66
from _colorize import ANSIColors, get_colors, get_theme
7+
import builtins
78
import rlcompleter
89
import keyword
910
import types
@@ -112,10 +113,8 @@ def global_matches(self, text):
112113
if keyword.iskeyword(clean_name) or keyword.issoftkeyword(clean_name):
113114
values.append(None)
114115
else:
115-
try:
116-
values.append(eval(name, self.namespace))
117-
except Exception:
118-
values.append(None)
116+
values.append(self.namespace.get(
117+
clean_name, builtins.__dict__.get(clean_name)))
119118
if self.use_colors and names:
120119
return self.colorize_matches(names, values)
121120
return names

‎Lib/test/test_pyrepl/test_fancycompleter.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,13 @@ def test_module_attributes_do_not_reify_lazy_imports(self):
142142
self.assertEqual(compl.attr_matches(f"{name}.j"), [f"{name}.json"])
143143
self.assertIs(type(module.__dict__["json"]), types.LazyImportType)
144144

145+
def test_globals_do_not_reify_lazy_imports(self):
146+
namespace = {}
147+
exec("lazy import json\nlazy import jsonz_missing\n", namespace)
148+
compl = Completer(namespace, use_colors=True)
149+
self.assertEqual(len(compl.global_matches("json")), 2)
150+
self.assertIs(type(namespace["json"]), types.LazyImportType)
151+
145152
def test_complete_colored_single_match(self):
146153
"""No coloring, via commonprefix."""
147154
compl = Completer({'foobar': 42}, use_colors=True)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Tab completion of global names in the :term:`REPL` no longer imports
2+
lazily imported modules.

0 commit comments

Comments
 (0)