diff --git a/Lib/idlelib/autocomplete.py b/Lib/idlelib/autocomplete.py index 032d31225315fb..c95c9f1a322127 100644 --- a/Lib/idlelib/autocomplete.py +++ b/Lib/idlelib/autocomplete.py @@ -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 @@ -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() diff --git a/Lib/idlelib/run.py b/Lib/idlelib/run.py index 577c49eb67b20d..edd28b4e1985b1 100644 --- a/Lib/idlelib/run.py +++ b/Lib/idlelib/run.py @@ -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 = {} 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.