From 76a633e87d00c6dc331a8b6bf0f03caa0f2d7078 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Fri, 7 Jun 2024 18:37:29 -0700 Subject: [PATCH 1/9] Add commands argument to pdb.Pdb --- Lib/pdb.py | 5 ++++- Lib/test/test_pdb.py | 12 ++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/Lib/pdb.py b/Lib/pdb.py index ba84a29aa2f669..a6db733963b755 100644 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -307,7 +307,7 @@ class Pdb(bdb.Bdb, cmd.Cmd): _file_mtime_table = {} def __init__(self, completekey='tab', stdin=None, stdout=None, skip=None, - nosigint=False, readrc=True): + nosigint=False, readrc=True, commands=None): bdb.Bdb.__init__(self, skip=skip) cmd.Cmd.__init__(self, completekey, stdin, stdout) sys.audit("pdb.Pdb") @@ -346,6 +346,9 @@ def __init__(self, completekey='tab', stdin=None, stdout=None, skip=None, except OSError: pass + if commands is not None: + self.rcLines.extend(commands) + self.commands = {} # associates a command list to breakpoint numbers self.commands_doprompt = {} # for each bp num, tells if the prompt # must be disp. after execing the cmd list diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index cf69bc415c9b69..153f0deac307e0 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -833,6 +833,18 @@ def test_pdb_where_command(): """ +def test_pdb_commands_at_init(): + """Test that commands can be passed to the constructor + + >>> def test_function(): + ... x = 1 + ... import pdb; pdb.Pdb(nosigint=True, readrc=False, commands=['p x', 'c']).set_trace() + + >>> test_function() + 1 + """ + + # skip this test if sys.flags.no_site = True; # exit() isn't defined unless there's a site module. if not sys.flags.no_site: From 88635c2cad9b495e06b1454c26a2038be1cda27c Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Fri, 7 Jun 2024 18:41:39 -0700 Subject: [PATCH 2/9] Add docs --- Doc/library/pdb.rst | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Doc/library/pdb.rst b/Doc/library/pdb.rst index cd6496203949ea..8fa00609f66724 100644 --- a/Doc/library/pdb.rst +++ b/Doc/library/pdb.rst @@ -192,7 +192,7 @@ The ``run*`` functions and :func:`set_trace` are aliases for instantiating the access further features, you have to do this yourself: .. class:: Pdb(completekey='tab', stdin=None, stdout=None, skip=None, \ - nosigint=False, readrc=True) + nosigint=False, readrc=True, commands=None) :class:`Pdb` is the debugger class. @@ -211,6 +211,9 @@ access further features, you have to do this yourself: The *readrc* argument defaults to true and controls whether Pdb will load .pdbrc files from the filesystem. + The *commands* argument, if given, would be a list of commands to execute + when the debugger starts. It has similar effects to the :file:`.pdbrc` file. + Example call to enable tracing with *skip*:: import pdb; pdb.Pdb(skip=['django.*']).set_trace() @@ -227,6 +230,9 @@ access further features, you have to do this yourself: .. versionchanged:: 3.6 The *readrc* argument. + .. versionadded:: 3.14 + The *commands* argument. + .. method:: run(statement, globals=None, locals=None) runeval(expression, globals=None, locals=None) runcall(function, *args, **kwds) From ac5c2ac47f468936165c755f27e07f40d857b4d6 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sat, 8 Jun 2024 03:29:03 +0000 Subject: [PATCH 3/9] =?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/2024-06-08-03-29-01.gh-issue-120254.h682ke.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2024-06-08-03-29-01.gh-issue-120254.h682ke.rst diff --git a/Misc/NEWS.d/next/Library/2024-06-08-03-29-01.gh-issue-120254.h682ke.rst b/Misc/NEWS.d/next/Library/2024-06-08-03-29-01.gh-issue-120254.h682ke.rst new file mode 100644 index 00000000000000..c6f54f6a64e647 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-06-08-03-29-01.gh-issue-120254.h682ke.rst @@ -0,0 +1 @@ +Added `commands` argument to :class:`Pdb` which allows user to send debugger commands in the source file. From b1887299ec676b46198cf73f9c2c92401be32807 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Fri, 7 Jun 2024 22:00:51 -0700 Subject: [PATCH 4/9] Update 2024-06-08-03-29-01.gh-issue-120254.h682ke.rst --- .../next/Library/2024-06-08-03-29-01.gh-issue-120254.h682ke.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2024-06-08-03-29-01.gh-issue-120254.h682ke.rst b/Misc/NEWS.d/next/Library/2024-06-08-03-29-01.gh-issue-120254.h682ke.rst index c6f54f6a64e647..b0b3e2ca3dd02c 100644 --- a/Misc/NEWS.d/next/Library/2024-06-08-03-29-01.gh-issue-120254.h682ke.rst +++ b/Misc/NEWS.d/next/Library/2024-06-08-03-29-01.gh-issue-120254.h682ke.rst @@ -1 +1 @@ -Added `commands` argument to :class:`Pdb` which allows user to send debugger commands in the source file. +Added `commands` argument to :class:`pdb.Pdb` which allows user to send debugger commands in the source file. From 613442fb9ef98a3b7e3c2cfc791742d01fb8f3d4 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Fri, 7 Jun 2024 22:02:34 -0700 Subject: [PATCH 5/9] Update 2024-06-08-03-29-01.gh-issue-120254.h682ke.rst --- .../next/Library/2024-06-08-03-29-01.gh-issue-120254.h682ke.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2024-06-08-03-29-01.gh-issue-120254.h682ke.rst b/Misc/NEWS.d/next/Library/2024-06-08-03-29-01.gh-issue-120254.h682ke.rst index b0b3e2ca3dd02c..a12556319dc92c 100644 --- a/Misc/NEWS.d/next/Library/2024-06-08-03-29-01.gh-issue-120254.h682ke.rst +++ b/Misc/NEWS.d/next/Library/2024-06-08-03-29-01.gh-issue-120254.h682ke.rst @@ -1 +1 @@ -Added `commands` argument to :class:`pdb.Pdb` which allows user to send debugger commands in the source file. +Added ``commands`` argument to :class:`pdb.Pdb` which allows user to send debugger commands in the source file. From 80826abdb739a3f7d0ab283695712fa3c3e37bd1 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Sat, 13 Jul 2024 23:35:01 -0700 Subject: [PATCH 6/9] Move commands argument to set_trace --- Doc/library/pdb.rst | 13 ++++++++----- Lib/doctest.py | 4 ++-- Lib/pdb.py | 18 ++++++++++-------- Lib/test/test_pdb.py | 7 +++---- 4 files changed, 23 insertions(+), 19 deletions(-) diff --git a/Doc/library/pdb.rst b/Doc/library/pdb.rst index e19ca75f02f751..42cafa476c0d2c 100644 --- a/Doc/library/pdb.rst +++ b/Doc/library/pdb.rst @@ -159,12 +159,15 @@ slightly different way: is entered. -.. function:: set_trace(*, header=None) +.. function:: set_trace(*, header=None, commands=None) Enter the debugger at the calling stack frame. This is useful to hard-code a breakpoint at a given point in a program, even if the code is not otherwise being debugged (e.g. when an assertion fails). If given, *header* is printed to the console just before debugging begins. + The *commands* argument, if given, is a list of commands to execute + when the debugger starts. + .. versionchanged:: 3.7 The keyword-only argument *header*. @@ -173,6 +176,9 @@ slightly different way: :func:`set_trace` will enter the debugger immediately, rather than on the next line of code to be executed. + .. versionadded:: 3.14 + The *commands* argument. + .. function:: post_mortem(traceback=None) Enter post-mortem debugging of the given *traceback* object. If no @@ -192,7 +198,7 @@ The ``run*`` functions and :func:`set_trace` are aliases for instantiating the access further features, you have to do this yourself: .. class:: Pdb(completekey='tab', stdin=None, stdout=None, skip=None, \ - nosigint=False, readrc=True, commands=None) + nosigint=False, readrc=True) :class:`Pdb` is the debugger class. @@ -211,9 +217,6 @@ access further features, you have to do this yourself: The *readrc* argument defaults to true and controls whether Pdb will load .pdbrc files from the filesystem. - The *commands* argument, if given, would be a list of commands to execute - when the debugger starts. It has similar effects to the :file:`.pdbrc` file. - Example call to enable tracing with *skip*:: import pdb; pdb.Pdb(skip=['django.*']).set_trace() diff --git a/Lib/doctest.py b/Lib/doctest.py index ea7d275c91db04..bb281fc483c41c 100644 --- a/Lib/doctest.py +++ b/Lib/doctest.py @@ -389,11 +389,11 @@ def __init__(self, out): # still use input() to get user input self.use_rawinput = 1 - def set_trace(self, frame=None): + def set_trace(self, frame=None, *, commands=None): self.__debugger_used = True if frame is None: frame = sys._getframe().f_back - pdb.Pdb.set_trace(self, frame) + pdb.Pdb.set_trace(self, frame, commands=commands) def set_continue(self): # Calling set_continue unconditionally would break unit test diff --git a/Lib/pdb.py b/Lib/pdb.py index ad6126e5a1e5e5..3297b8c79c25dd 100644 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -309,7 +309,7 @@ class Pdb(bdb.Bdb, cmd.Cmd): _last_pdb_instance = None def __init__(self, completekey='tab', stdin=None, stdout=None, skip=None, - nosigint=False, readrc=True, commands=None): + nosigint=False, readrc=True): bdb.Bdb.__init__(self, skip=skip) cmd.Cmd.__init__(self, completekey, stdin, stdout) sys.audit("pdb.Pdb") @@ -348,9 +348,6 @@ def __init__(self, completekey='tab', stdin=None, stdout=None, skip=None, except OSError: pass - if commands is not None: - self.rcLines.extend(commands) - self.commands = {} # associates a command list to breakpoint numbers self.commands_doprompt = {} # for each bp num, tells if the prompt # must be disp. after execing the cmd list @@ -364,10 +361,14 @@ def __init__(self, completekey='tab', stdin=None, stdout=None, skip=None, self._chained_exceptions = tuple() self._chained_exception_index = 0 - def set_trace(self, frame=None): + def set_trace(self, frame=None, *, commands=None): Pdb._last_pdb_instance = self if frame is None: frame = sys._getframe().f_back + + if commands is not None: + self.rcLines.extend(commands) + super().set_trace(frame) def sigint_handler(self, signum, frame): @@ -2353,13 +2354,14 @@ def runcall(*args, **kwds): """ return Pdb().runcall(*args, **kwds) -def set_trace(*, header=None): +def set_trace(*, header=None, commands=None): """Enter the debugger at the calling stack frame. This is useful to hard-code a breakpoint at a given point in a program, even if the code is not otherwise being debugged (e.g. when an assertion fails). If given, *header* is printed to the console - just before debugging begins. + just before debugging begins. *commands* is an optional list of + pdb commands to run when the debugger starts. """ if Pdb._last_pdb_instance is not None: pdb = Pdb._last_pdb_instance @@ -2367,7 +2369,7 @@ def set_trace(*, header=None): pdb = Pdb() if header is not None: pdb.message(header) - pdb.set_trace(sys._getframe().f_back) + pdb.set_trace(sys._getframe().f_back, commands=commands) # Post-Mortem interface diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index 339e91a9a60c05..73de431f177f48 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -901,13 +901,12 @@ def test_pdb_where_command(): (Pdb) continue """ - -def test_pdb_commands_at_init(): - """Test that commands can be passed to the constructor +def test_pdb_commands_with_set_trace(): + """Test that commands can be passed to Pdb.set_trace() >>> def test_function(): ... x = 1 - ... import pdb; pdb.Pdb(nosigint=True, readrc=False, commands=['p x', 'c']).set_trace() + ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace(commands=['p x', 'c']) >>> test_function() 1 From 0eb5d3b70343a8b70f162f82e6813788e093ecee Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Sat, 13 Jul 2024 23:35:49 -0700 Subject: [PATCH 7/9] Remove old added entry --- Doc/library/pdb.rst | 3 --- 1 file changed, 3 deletions(-) diff --git a/Doc/library/pdb.rst b/Doc/library/pdb.rst index 42cafa476c0d2c..0a0410ceecd932 100644 --- a/Doc/library/pdb.rst +++ b/Doc/library/pdb.rst @@ -233,9 +233,6 @@ access further features, you have to do this yourself: .. versionchanged:: 3.6 The *readrc* argument. - .. versionadded:: 3.14 - The *commands* argument. - .. method:: run(statement, globals=None, locals=None) runeval(expression, globals=None, locals=None) runcall(function, *args, **kwds) From 7db612bd62e0962026baac8fc679ba1ea9c34730 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Sat, 13 Jul 2024 23:37:13 -0700 Subject: [PATCH 8/9] Update 2024-06-08-03-29-01.gh-issue-120254.h682ke.rst --- .../next/Library/2024-06-08-03-29-01.gh-issue-120254.h682ke.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2024-06-08-03-29-01.gh-issue-120254.h682ke.rst b/Misc/NEWS.d/next/Library/2024-06-08-03-29-01.gh-issue-120254.h682ke.rst index a12556319dc92c..4405cc71575b42 100644 --- a/Misc/NEWS.d/next/Library/2024-06-08-03-29-01.gh-issue-120254.h682ke.rst +++ b/Misc/NEWS.d/next/Library/2024-06-08-03-29-01.gh-issue-120254.h682ke.rst @@ -1 +1 @@ -Added ``commands`` argument to :class:`pdb.Pdb` which allows user to send debugger commands in the source file. +Added ``commands`` argument to :func:`pdb.set_trace` which allows user to send debugger commands from the source file. From f802cd6be0f3a64a8baa3bbd7cef7559f01a7453 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Sat, 13 Jul 2024 23:37:30 -0700 Subject: [PATCH 9/9] Update 2024-06-08-03-29-01.gh-issue-120254.h682ke.rst --- .../next/Library/2024-06-08-03-29-01.gh-issue-120254.h682ke.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2024-06-08-03-29-01.gh-issue-120254.h682ke.rst b/Misc/NEWS.d/next/Library/2024-06-08-03-29-01.gh-issue-120254.h682ke.rst index 4405cc71575b42..33ef1c91591c54 100644 --- a/Misc/NEWS.d/next/Library/2024-06-08-03-29-01.gh-issue-120254.h682ke.rst +++ b/Misc/NEWS.d/next/Library/2024-06-08-03-29-01.gh-issue-120254.h682ke.rst @@ -1 +1 @@ -Added ``commands`` argument to :func:`pdb.set_trace` which allows user to send debugger commands from the source file. +Added ``commands`` argument to :func:`pdb.set_trace` which allows users to send debugger commands from the source file.