From 0d33fdb114a6cb3b2723870e4b9e4431bced22fc Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Thu, 13 Jul 2023 18:43:15 -0700 Subject: [PATCH 1/6] Disable tab completion in multiline mode --- Lib/pdb.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Lib/pdb.py b/Lib/pdb.py index 3db3e6a5be1a7b..0845ad9a76eeac 100755 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -444,9 +444,16 @@ def default(self, line): if line[:1] == '!': line = line[1:].strip() locals = self.curframe_locals globals = self.curframe.f_globals + readline = None try: if (code := codeop.compile_command(line + '\n', '', 'single')) is None: # Multi-line mode + try: + import readline + # Disable tab-completion temporarily + readline.parse_and_bind('tab: self-insert') + except ImportError: + pass buffer = line continue_prompt = "... " while (code := codeop.compile_command(buffer, '', 'single')) is None: @@ -483,6 +490,9 @@ def default(self, line): sys.displayhook = save_displayhook except: self._error_exc() + finally: + if readline: + readline.parse_and_bind('tab: complete') def precmd(self, line): """Handle alias expansion and ';;' separator.""" From d7f5d585faa8557ab93bb27aaa2b18df2093697e Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Fri, 14 Jul 2023 01:47:41 +0000 Subject: [PATCH 2/6] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2023-07-14-01-47-39.gh-issue-106734.eMYSoz.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2023-07-14-01-47-39.gh-issue-106734.eMYSoz.rst diff --git a/Misc/NEWS.d/next/Library/2023-07-14-01-47-39.gh-issue-106734.eMYSoz.rst b/Misc/NEWS.d/next/Library/2023-07-14-01-47-39.gh-issue-106734.eMYSoz.rst new file mode 100644 index 00000000000000..37d2ab19ed1017 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-07-14-01-47-39.gh-issue-106734.eMYSoz.rst @@ -0,0 +1 @@ +Disable tab completion in multiline mode of :mod:`pdb` From 8b326644a134436b51b900bd54a46e7daffc7cfe Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Sat, 9 Sep 2023 13:37:14 -0700 Subject: [PATCH 3/6] Use context manager to disable tab completion --- Lib/pdb.py | 68 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 38 insertions(+), 30 deletions(-) diff --git a/Lib/pdb.py b/Lib/pdb.py index cff4a1af8b3987..13f39a3ab22315 100755 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -514,6 +514,22 @@ def displayhook(self, obj): if obj is not None: self.message(repr(obj)) + @contextmanager + def _disable_tab_completion(self): + if self.use_rawinput and self.completekey == 'tab': + try: + import readline + except ImportError: + pass + if readline and self.use_rawinput and self.completekey == 'tab': + try: + readline.parse_and_bind('tab: self-insert') + yield + finally: + readline.parse_and_bind('tab: complete') + else: + yield + def default(self, line): if line[:1] == '!': line = line[1:].strip() locals = self.curframe_locals @@ -522,34 +538,29 @@ def default(self, line): try: if (code := codeop.compile_command(line + '\n', '', 'single')) is None: # Multi-line mode - try: - import readline - # Disable tab-completion temporarily - readline.parse_and_bind('tab: self-insert') - except ImportError: - pass - buffer = line - continue_prompt = "... " - while (code := codeop.compile_command(buffer, '', 'single')) is None: - if self.use_rawinput: - try: - line = input(continue_prompt) - except (EOFError, KeyboardInterrupt): - self.lastcmd = "" - print('\n') - return - else: - self.stdout.write(continue_prompt) - self.stdout.flush() - line = self.stdin.readline() - if not len(line): - self.lastcmd = "" - self.stdout.write('\n') - self.stdout.flush() - return + with self._disable_tab_completion(): + buffer = line + continue_prompt = "... " + while (code := codeop.compile_command(buffer, '', 'single')) is None: + if self.use_rawinput: + try: + line = input(continue_prompt) + except (EOFError, KeyboardInterrupt): + self.lastcmd = "" + print('\n') + return else: - line = line.rstrip('\r\n') - buffer += '\n' + line + self.stdout.write(continue_prompt) + self.stdout.flush() + line = self.stdin.readline() + if not len(line): + self.lastcmd = "" + self.stdout.write('\n') + self.stdout.flush() + return + else: + line = line.rstrip('\r\n') + buffer += '\n' + line save_stdout = sys.stdout save_stdin = sys.stdin save_displayhook = sys.displayhook @@ -564,9 +575,6 @@ def default(self, line): sys.displayhook = save_displayhook except: self._error_exc() - finally: - if readline: - readline.parse_and_bind('tab: complete') def precmd(self, line): """Handle alias expansion and ';;' separator.""" From 2df5b19ab4b6c55d2eb10340750285c31513f89c Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Sat, 9 Sep 2023 13:37:48 -0700 Subject: [PATCH 4/6] Remove dead variable --- Lib/pdb.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/pdb.py b/Lib/pdb.py index 13f39a3ab22315..cf1a32c035085b 100755 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -534,7 +534,6 @@ def default(self, line): if line[:1] == '!': line = line[1:].strip() locals = self.curframe_locals globals = self.curframe.f_globals - readline = None try: if (code := codeop.compile_command(line + '\n', '', 'single')) is None: # Multi-line mode From a4ea4ff1591322118e950ff21450a411e2d802f8 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Sat, 9 Sep 2023 18:36:37 -0700 Subject: [PATCH 5/6] Fix readline check --- Lib/pdb.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Lib/pdb.py b/Lib/pdb.py index cf1a32c035085b..8582eddb417c92 100755 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -520,8 +520,10 @@ def _disable_tab_completion(self): try: import readline except ImportError: - pass - if readline and self.use_rawinput and self.completekey == 'tab': + yield + return + + if self.use_rawinput and self.completekey == 'tab': try: readline.parse_and_bind('tab: self-insert') yield From dc7aee3d328d58cb3d99c56a6949bc9c5a9dc78d Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Mon, 11 Sep 2023 12:54:23 -0700 Subject: [PATCH 6/6] Remove redundant code Co-authored-by: Brandt Bucher --- Lib/pdb.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/Lib/pdb.py b/Lib/pdb.py index 8582eddb417c92..e231d3d7eae475 100755 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -522,15 +522,13 @@ def _disable_tab_completion(self): except ImportError: yield return - - if self.use_rawinput and self.completekey == 'tab': - try: - readline.parse_and_bind('tab: self-insert') - yield - finally: - readline.parse_and_bind('tab: complete') - else: + try: + readline.parse_and_bind('tab: self-insert') yield + finally: + readline.parse_and_bind('tab: complete') + else: + yield def default(self, line): if line[:1] == '!': line = line[1:].strip()