Skip to content

gh-82002: IDLE completion namespace fix #15207

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 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Diff view
17 changes: 14 additions & 3 deletions Lib/idlelib/autocomplete.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@

class AutoComplete:

def __init__(self, editwin=None, tags=None):
def __init__(self, editwin=None, tags=None, namespace=None):

self.editwin = editwin
self.namespace = namespace
if editwin is not None: # not in subprocess or no-gui test
self.text = editwin.text
self.tags = tags
Expand Down Expand Up @@ -217,8 +219,17 @@ def fetch_completions(self, what, mode):
return smalll, bigl

def get_entity(self, name):
"Lookup name in a namespace spanning sys.modules and __main.dict__."
return eval(name, {**sys.modules, **__main__.__dict__})
"""Evaluate an expression in a the execution namespace."""
namespace = self.namespace
if namespace is None:
try:
# Fallback when running without a subprocess.
namespace = self.editwin.flist.pyshell.interp.locals
except AttributeError:
# Fallback when there isn't a running shell.
namespace = {**sys.modules, **__main__.__dict__}

return eval(name, namespace)


AutoComplete.reload()
Expand Down
2 changes: 1 addition & 1 deletion Lib/idlelib/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ def __init__(self, rpchandler):
if idlelib.testing is False:
self.locals = __main__.__dict__
self.calltip = calltip.Calltip()
self.autocomplete = autocomplete.AutoComplete()
self.autocomplete = autocomplete.AutoComplete(namespace=self.locals)
else:
self.locals = {}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix the namespace used for completions to be that of the running shell, when
a shell window exists.