From 3842a180bed17c2a526bdb46cadea4496fc2e541 Mon Sep 17 00:00:00 2001 From: Tal Einat Date: Sun, 11 Aug 2019 14:18:29 +0300 Subject: [PATCH] bpo-37821: IDLE completion namespace fix --- Lib/idlelib/autocomplete.py | 16 +++++++++++++--- Lib/idlelib/run.py | 2 +- .../2019-08-11-14-20-28.bpo-37821.OVbIt_.rst | 2 ++ 3 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 Misc/NEWS.d/next/IDLE/2019-08-11-14-20-28.bpo-37821.OVbIt_.rst diff --git a/Lib/idlelib/autocomplete.py b/Lib/idlelib/autocomplete.py index c623d45a153423..0dac2a4bfa2efa 100644 --- a/Lib/idlelib/autocomplete.py +++ b/Lib/idlelib/autocomplete.py @@ -30,8 +30,9 @@ class AutoComplete: - def __init__(self, editwin=None): + def __init__(self, editwin=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.autocompletewindow = None @@ -208,8 +209,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() diff --git a/Lib/idlelib/run.py b/Lib/idlelib/run.py index 41e0ded4402937..846dc4699d879e 100644 --- a/Lib/idlelib/run.py +++ b/Lib/idlelib/run.py @@ -539,7 +539,7 @@ def __init__(self, rpchandler): self.rpchandler = rpchandler self.locals = __main__.__dict__ self.calltip = calltip.Calltip() - self.autocomplete = autocomplete.AutoComplete() + self.autocomplete = autocomplete.AutoComplete(namespace=self.locals) def runcode(self, code): global interruptable diff --git a/Misc/NEWS.d/next/IDLE/2019-08-11-14-20-28.bpo-37821.OVbIt_.rst b/Misc/NEWS.d/next/IDLE/2019-08-11-14-20-28.bpo-37821.OVbIt_.rst new file mode 100644 index 00000000000000..87e05eac64d7dd --- /dev/null +++ b/Misc/NEWS.d/next/IDLE/2019-08-11-14-20-28.bpo-37821.OVbIt_.rst @@ -0,0 +1,2 @@ +Fix the namespace used for completions to be that of the running shell, when +a shell window exists.