From a38caf6641b352e4bf442fb2fef488d37546d763 2017-01-27 22:29:25 From: Min RK Date: 2017-01-27 22:29:25 Subject: [PATCH] Backport PR #10153: Use prompt_toolkit's patch_stdout_context when finding completions Closes gh-10113 --- diff --git a/IPython/terminal/ptutils.py b/IPython/terminal/ptutils.py index 84515e1..152899d 100644 --- a/IPython/terminal/ptutils.py +++ b/IPython/terminal/ptutils.py @@ -39,10 +39,14 @@ class IPythonPTCompleter(Completer): if not document.current_line.strip(): return - used, matches = self.ipy_completer.complete( - line_buffer=document.current_line, - cursor_pos=document.cursor_position_col - ) + # Some bits of our completion system may print stuff (e.g. if a module + # is imported). This context manager ensures that doesn't interfere with + # the prompt. + with self.shell.pt_cli.patch_stdout_context(): + used, matches = self.ipy_completer.complete( + line_buffer=document.current_line, + cursor_pos=document.cursor_position_col + ) start_pos = -len(used) for m in matches: if not m: