From f9b3123cf3c32a71fe4ab5d9bde4c78a972641ca Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Sun, 22 Oct 2023 17:18:47 -0700 Subject: [PATCH 1/7] Add tests and some extra notes for interact --- Doc/library/pdb.rst | 7 +++++++ Lib/test/test_pdb.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/Doc/library/pdb.rst b/Doc/library/pdb.rst index 002eeef4c09b5d..7b1289e39f89dc 100644 --- a/Doc/library/pdb.rst +++ b/Doc/library/pdb.rst @@ -572,6 +572,13 @@ can be overridden by the local file. namespace contains all the (global and local) names found in the current scope. + .. note:: + + Because interact creates a new global namespace with the current global and + local namespace, assignment to variables will not affect the original + namespaces. However, modification to the mutable objects will be reflected + in the original namespaces. + .. versionadded:: 3.2 .. _debugger-aliases: diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index 4701fa0cc9656a..da635c788b2611 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -754,6 +754,36 @@ def test_pdb_where_command(): (Pdb) continue """ +def test_pdb_interact_command(): + """Test interact command + + >>> g = 0 + >>> dict_g = {} + + >>> def test_function(): + ... x = 1 + ... lst_local = [] + ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() + + >>> with PdbTestInput([ # doctest: +ELLIPSIS + ... 'interact', + ... 'x', + ... 'g', + ... 'x = 2', + ... 'g = 3', + ... 'dict_g["a"] = True', + ... 'lst_local.append(x)', + ... 'exit()', + ... 'p x', + ... 'p g', + ... 'p dict_g', + ... 'p lst_local', + ... 'continue', + ... ]): + ... test_function() + --Return-- + """ + def test_convenience_variables(): """Test convenience variables From 4edbbc0b363ea9f3c3ae02ff1d9c66b79373db5e Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Sun, 22 Oct 2023 17:25:56 -0700 Subject: [PATCH 2/7] Update docs --- Doc/library/pdb.rst | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Doc/library/pdb.rst b/Doc/library/pdb.rst index 7b1289e39f89dc..343c348da857b0 100644 --- a/Doc/library/pdb.rst +++ b/Doc/library/pdb.rst @@ -570,14 +570,16 @@ can be overridden by the local file. Start an interactive interpreter (using the :mod:`code` module) whose global namespace contains all the (global and local) names found in the current - scope. + scope. Use ``exit()`` or ``quit()`` to exit the interpreter and return to + the debugger. .. note:: - Because interact creates a new global namespace with the current global and - local namespace, assignment to variables will not affect the original - namespaces. However, modification to the mutable objects will be reflected - in the original namespaces. + Because interact creates a new global namespace with the current global + and local namespace for execution, assignment to variables will not + affect the original namespaces. + However, modification to the mutable objects will be reflected in the + original namespaces. .. versionadded:: 3.2 From a56a29436cdf69946b692d9b740db616053f5d3a Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Sun, 22 Oct 2023 20:41:15 -0700 Subject: [PATCH 3/7] Fix interact output --- Lib/pdb.py | 13 ++++++++++++- Lib/test/test_pdb.py | 25 ++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/Lib/pdb.py b/Lib/pdb.py index 1e4d0a20515fa3..3b1b566e06c900 100755 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -205,6 +205,15 @@ def namespace(self): ) +class _PdbInteractiveConsole(code.InteractiveConsole): + def __init__(self, ns, message): + self._message = message + super().__init__(locals=ns, local_exit=True) + + def write(self, data): + self._message(data) + + # Interaction prompt line will separate file and call info from code # text using value of line_prefix string. A newline and arrow may # be to your liking. You can set it once pdb is imported using the @@ -1741,7 +1750,9 @@ def do_interact(self, arg): contains all the (global and local) names found in the current scope. """ ns = {**self.curframe.f_globals, **self.curframe_locals} - code.interact("*interactive*", local=ns, local_exit=True) + console = _PdbInteractiveConsole(ns, message=self.message) + console.interact(banner="*pdb interact start*", + exitmsg="*exit from pdb interact command*") def do_alias(self, arg): """alias [name [command]] diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index da635c788b2611..e9caca34abed5c 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -765,7 +765,7 @@ def test_pdb_interact_command(): ... lst_local = [] ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() - >>> with PdbTestInput([ # doctest: +ELLIPSIS + >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE ... 'interact', ... 'x', ... 'g', @@ -782,6 +782,29 @@ def test_pdb_interact_command(): ... ]): ... test_function() --Return-- + > (4)test_function()->None + -> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() + (Pdb) interact + *pdb interact start* + ... x + 1 + ... g + 0 + ... x = 2 + ... g = 3 + ... dict_g["a"] = True + ... lst_local.append(x) + ... exit() + *exit from pdb interact command* + (Pdb) p x + 1 + (Pdb) p g + 0 + (Pdb) p dict_g + {'a': True} + (Pdb) p lst_local + [2] + (Pdb) continue """ def test_convenience_variables(): From 4611d18b1e0d2ef0ac7f41573229d4febfb5e8f8 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Mon, 23 Oct 2023 03:49:35 +0000 Subject: [PATCH 4/7] =?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-10-23-03-49-34.gh-issue-102980.aXBd54.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2023-10-23-03-49-34.gh-issue-102980.aXBd54.rst diff --git a/Misc/NEWS.d/next/Library/2023-10-23-03-49-34.gh-issue-102980.aXBd54.rst b/Misc/NEWS.d/next/Library/2023-10-23-03-49-34.gh-issue-102980.aXBd54.rst new file mode 100644 index 00000000000000..d4bae4790d6fa4 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-10-23-03-49-34.gh-issue-102980.aXBd54.rst @@ -0,0 +1 @@ +Redirect the output of ``interact`` command of :mod:`pdb` to the same channel as the debugger. Add tests and improve docs. From 2eca91f28a40e794e364e2764511791a6e231d12 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Mon, 23 Oct 2023 10:13:42 -0700 Subject: [PATCH 5/7] Remove blank line from the output --- Lib/pdb.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Lib/pdb.py b/Lib/pdb.py index 3b1b566e06c900..a6349ed4ffcc88 100755 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -211,7 +211,7 @@ def __init__(self, ns, message): super().__init__(locals=ns, local_exit=True) def write(self, data): - self._message(data) + self._message(data, end='') # Interaction prompt line will separate file and call info from code @@ -669,8 +669,8 @@ def handle_command_def(self, line): # interface abstraction functions - def message(self, msg): - print(msg, file=self.stdout) + def message(self, msg, end='\n'): + print(msg, end=end, file=self.stdout) def error(self, msg): print('***', msg, file=self.stdout) From 03e1dc0b66668c141353c97e0f04bb828c02e526 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Wed, 6 Dec 2023 18:53:57 -0800 Subject: [PATCH 6/7] Update docs about version info --- Doc/library/pdb.rst | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Doc/library/pdb.rst b/Doc/library/pdb.rst index 66d63e348848f8..e858ef7be97215 100644 --- a/Doc/library/pdb.rst +++ b/Doc/library/pdb.rst @@ -583,6 +583,14 @@ can be overridden by the local file. .. versionadded:: 3.2 + .. versionadded:: 3.13 + ``exit()`` and ``quit()`` can be used to exit :pdbcmd:`interact` + command. + + .. versionchanged:: 3.13 + :pdbcmd:`interact` will direct its output to the debugger's + output channel rather than :data:`sys.stderr`. + .. _debugger-aliases: .. pdbcommand:: alias [name [command]] From cde8ecfac41b198f4180602b4095d45850b7444d Mon Sep 17 00:00:00 2001 From: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> Date: Thu, 7 Dec 2023 11:02:03 +0000 Subject: [PATCH 7/7] Update Doc/library/pdb.rst --- Doc/library/pdb.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/pdb.rst b/Doc/library/pdb.rst index e858ef7be97215..2495dcf50bb17f 100644 --- a/Doc/library/pdb.rst +++ b/Doc/library/pdb.rst @@ -588,7 +588,7 @@ can be overridden by the local file. command. .. versionchanged:: 3.13 - :pdbcmd:`interact` will direct its output to the debugger's + :pdbcmd:`interact` directs its output to the debugger's output channel rather than :data:`sys.stderr`. .. _debugger-aliases: