From cc59b1027cf3d236fe6469c12cd9f04c1f39b1e1 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Sun, 7 Apr 2024 20:07:29 -0700 Subject: [PATCH 1/8] Make module searchable for breakpoints and clean up docs --- Doc/library/pdb.rst | 17 ++++++++++++----- Lib/pdb.py | 24 +++++++++++++++--------- Lib/test/test_pdb.py | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 14 deletions(-) diff --git a/Doc/library/pdb.rst b/Doc/library/pdb.rst index ac3007f70c3534..3b0b26e3f1d5fd 100644 --- a/Doc/library/pdb.rst +++ b/Doc/library/pdb.rst @@ -328,12 +328,16 @@ can be overridden by the local file. .. pdbcommand:: b(reak) [([filename:]lineno | function) [, condition]] - With a *lineno* argument, set a break there in the current file. With a - *function* argument, set a break at the first executable statement within - that function. The line number may be prefixed with a filename and a colon, + With a *lineno* argument, set a break at line *lineno* in the current file. + The line number may be prefixed with a *filename* and a colon, to specify a breakpoint in another file (probably one that hasn't been loaded - yet). The file is searched on :data:`sys.path`. Note that each breakpoint - is assigned a number to which all the other breakpoint commands refer. + yet). The file is searched on :data:`sys.path`. Accepatable form of *filename* + includes ``/abspath/to/file.py``, ``relpath/file.py``, ``module`` or + ``package.module``. + + With a *function* argument, set a break at the first executable statement within + that function. *function* can be any expression that evaluates to a function + in the current namespace. If a second argument is present, it is an expression which must evaluate to true before the breakpoint is honored. @@ -341,6 +345,9 @@ can be overridden by the local file. Without argument, list all breaks, including for each breakpoint, the number of times that breakpoint has been hit, the current ignore count, and the associated condition if any. + + Note that each breakpoint is assigned a number to which all the other + breakpoint commands refer. .. pdbcommand:: tbreak [([filename:]lineno | function) [, condition]] diff --git a/Lib/pdb.py b/Lib/pdb.py index d4138b95d3c332..fa2e1ec94be235 100755 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -2007,17 +2007,23 @@ def lookupmodule(self, filename): lookupmodule() translates (possibly incomplete) file or module name into an absolute file name. + + filename could be in format of: + * an absolute path like '/path/to/file.py' + * a relative path like 'file.py' or 'dir/file.py' + * a module name like 'module' or 'package.module' + + files and modules will be searched in sys.path. """ - if os.path.isabs(filename) and os.path.exists(filename): - return filename - f = os.path.join(sys.path[0], filename) - if os.path.exists(f) and self.canonic(f) == self.mainpyfile: - return f - root, ext = os.path.splitext(filename) - if ext == '': - filename = filename + '.py' + if not filename.endswith('.py'): + # A module is passed in so convert it to equivalent file + filename = filename.replace('.', os.sep) + '.py' + if os.path.isabs(filename): - return filename + if os.path.exists(filename): + return filename + return None + for dirname in sys.path: while os.path.islink(dirname): dirname = os.readlink(dirname) diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index 2d057e2647f13c..60162b2d194049 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -354,6 +354,46 @@ def test_pdb_breakpoint_commands(): 4 """ +def test_pdb_breakpoint_with_filename(): + """Breakpoints with filename:lineno + + >>> def test_function(): + ... # inspect_fodder2 is a great module as the line number is stable + ... from test.test_inspect import inspect_fodder2 as mod2 + ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() + ... mod2.func88() + ... mod2.func114() + ... # Be a good citizen and clean up the mess + ... reset_Breakpoint() + + First, need to clear bdb state that might be left over from previous tests. + Otherwise, the new breakpoints might get assigned different numbers. + + >>> reset_Breakpoint() + + >>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE + ... 'break test.test_inspect.inspect_fodder2:90', + ... 'continue', # will stop at func88 + ... 'break test/test_inspect/inspect_fodder2.py:115', + ... 'continue', # will stop at func114 + ... 'continue', + ... ]): + ... test_function() + > (5)test_function() + -> mod2.func88() + (Pdb) break test.test_inspect.inspect_fodder2:90 + Breakpoint 1 at /home/gaogaotiantian/programs/mycpython/Lib/test/test_inspect/inspect_fodder2.py:90 + (Pdb) continue + > /home/gaogaotiantian/programs/mycpython/Lib/test/test_inspect/inspect_fodder2.py(90)func88() + -> return 90 + (Pdb) break test/test_inspect/inspect_fodder2.py:115 + Breakpoint 2 at /home/gaogaotiantian/programs/mycpython/Lib/test/test_inspect/inspect_fodder2.py:115 + (Pdb) continue + > /home/gaogaotiantian/programs/mycpython/Lib/test/test_inspect/inspect_fodder2.py(115)func114() + -> return 115 + (Pdb) continue + """ + def test_pdb_breakpoints_preserved_across_interactive_sessions(): """Breakpoints are remembered between interactive sessions From 9a3962a1799177bc0968e56caccfd641c9943eef Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Sun, 7 Apr 2024 20:13:07 -0700 Subject: [PATCH 2/8] Clean up empty lines --- Doc/library/pdb.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/library/pdb.rst b/Doc/library/pdb.rst index 3b0b26e3f1d5fd..ed3af12d61a667 100644 --- a/Doc/library/pdb.rst +++ b/Doc/library/pdb.rst @@ -334,7 +334,7 @@ can be overridden by the local file. yet). The file is searched on :data:`sys.path`. Accepatable form of *filename* includes ``/abspath/to/file.py``, ``relpath/file.py``, ``module`` or ``package.module``. - + With a *function* argument, set a break at the first executable statement within that function. *function* can be any expression that evaluates to a function in the current namespace. @@ -345,7 +345,7 @@ can be overridden by the local file. Without argument, list all breaks, including for each breakpoint, the number of times that breakpoint has been hit, the current ignore count, and the associated condition if any. - + Note that each breakpoint is assigned a number to which all the other breakpoint commands refer. From 53b646e6e5154891d64258a79f9cc70658d48eca Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Mon, 8 Apr 2024 03:23:25 +0000 Subject: [PATCH 3/8] =?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-04-08-03-23-22.gh-issue-117618.-4DCUw.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2024-04-08-03-23-22.gh-issue-117618.-4DCUw.rst diff --git a/Misc/NEWS.d/next/Library/2024-04-08-03-23-22.gh-issue-117618.-4DCUw.rst b/Misc/NEWS.d/next/Library/2024-04-08-03-23-22.gh-issue-117618.-4DCUw.rst new file mode 100644 index 00000000000000..2ef8e3dfc3f159 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-04-08-03-23-22.gh-issue-117618.-4DCUw.rst @@ -0,0 +1 @@ +Support ``package.module`` as ``filename`` for :pdbcommand:`break` From 56cda809324bc4eaa599bd7a1ecfce990daa9034 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Sun, 7 Apr 2024 20:30:23 -0700 Subject: [PATCH 4/8] Update 2024-04-08-03-23-22.gh-issue-117618.-4DCUw.rst --- .../next/Library/2024-04-08-03-23-22.gh-issue-117618.-4DCUw.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2024-04-08-03-23-22.gh-issue-117618.-4DCUw.rst b/Misc/NEWS.d/next/Library/2024-04-08-03-23-22.gh-issue-117618.-4DCUw.rst index 2ef8e3dfc3f159..861b1780977feb 100644 --- a/Misc/NEWS.d/next/Library/2024-04-08-03-23-22.gh-issue-117618.-4DCUw.rst +++ b/Misc/NEWS.d/next/Library/2024-04-08-03-23-22.gh-issue-117618.-4DCUw.rst @@ -1 +1 @@ -Support ``package.module`` as ``filename`` for :pdbcommand:`break` +Support ``package.module`` as ``filename`` for ``break`` command From ce7df93dbd9c1fae39ec50afcd9d5b9fb6853a91 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Sun, 7 Apr 2024 22:06:07 -0700 Subject: [PATCH 5/8] Remove absolute path in test --- Lib/test/test_pdb.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index 60162b2d194049..491904d4290004 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -371,7 +371,7 @@ def test_pdb_breakpoint_with_filename(): >>> reset_Breakpoint() - >>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE + >>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS ... 'break test.test_inspect.inspect_fodder2:90', ... 'continue', # will stop at func88 ... 'break test/test_inspect/inspect_fodder2.py:115', @@ -382,14 +382,14 @@ def test_pdb_breakpoint_with_filename(): > (5)test_function() -> mod2.func88() (Pdb) break test.test_inspect.inspect_fodder2:90 - Breakpoint 1 at /home/gaogaotiantian/programs/mycpython/Lib/test/test_inspect/inspect_fodder2.py:90 + Breakpoint 1 at ...test/test_inspect/inspect_fodder2.py:90 (Pdb) continue - > /home/gaogaotiantian/programs/mycpython/Lib/test/test_inspect/inspect_fodder2.py(90)func88() + > ...test/test_inspect/inspect_fodder2.py(90)func88() -> return 90 (Pdb) break test/test_inspect/inspect_fodder2.py:115 - Breakpoint 2 at /home/gaogaotiantian/programs/mycpython/Lib/test/test_inspect/inspect_fodder2.py:115 + Breakpoint 2 at ...test/test_inspect/inspect_fodder2.py:115 (Pdb) continue - > /home/gaogaotiantian/programs/mycpython/Lib/test/test_inspect/inspect_fodder2.py(115)func114() + > ...test/test_inspect/inspect_fodder2.py(115)func114() -> return 115 (Pdb) continue """ From 99f59ebc0a59c0255c2cac1fce63e53d9bc72476 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Sun, 7 Apr 2024 23:10:16 -0700 Subject: [PATCH 6/8] Also fix windows --- Lib/test/test_pdb.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index 491904d4290004..5635d17c99d2a3 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -382,14 +382,14 @@ def test_pdb_breakpoint_with_filename(): > (5)test_function() -> mod2.func88() (Pdb) break test.test_inspect.inspect_fodder2:90 - Breakpoint 1 at ...test/test_inspect/inspect_fodder2.py:90 + Breakpoint 1 at ...inspect_fodder2.py:90 (Pdb) continue - > ...test/test_inspect/inspect_fodder2.py(90)func88() + > ...inspect_fodder2.py(90)func88() -> return 90 (Pdb) break test/test_inspect/inspect_fodder2.py:115 - Breakpoint 2 at ...test/test_inspect/inspect_fodder2.py:115 + Breakpoint 2 at ...inspect_fodder2.py:115 (Pdb) continue - > ...test/test_inspect/inspect_fodder2.py(115)func114() + > ...inspect_fodder2.py(115)func114() -> return 115 (Pdb) continue """ From 2a449eabe514f45bf6019875e33a0e912e545147 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Tue, 30 Apr 2024 00:10:59 -0700 Subject: [PATCH 7/8] Update 2024-04-08-03-23-22.gh-issue-117618.-4DCUw.rst --- .../next/Library/2024-04-08-03-23-22.gh-issue-117618.-4DCUw.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2024-04-08-03-23-22.gh-issue-117618.-4DCUw.rst b/Misc/NEWS.d/next/Library/2024-04-08-03-23-22.gh-issue-117618.-4DCUw.rst index 861b1780977feb..569c5d57effdcb 100644 --- a/Misc/NEWS.d/next/Library/2024-04-08-03-23-22.gh-issue-117618.-4DCUw.rst +++ b/Misc/NEWS.d/next/Library/2024-04-08-03-23-22.gh-issue-117618.-4DCUw.rst @@ -1 +1 @@ -Support ``package.module`` as ``filename`` for ``break`` command +Support ``package.module`` as ``filename`` for ``break`` command of :mod:`pdb` From 009fa1aba68e94e1c054d47deea2e3be56f3c32e Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Tue, 30 Apr 2024 10:34:03 -0700 Subject: [PATCH 8/8] Update docs Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> --- Doc/library/pdb.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Doc/library/pdb.rst b/Doc/library/pdb.rst index ed3af12d61a667..a640976493caba 100644 --- a/Doc/library/pdb.rst +++ b/Doc/library/pdb.rst @@ -330,9 +330,9 @@ can be overridden by the local file. With a *lineno* argument, set a break at line *lineno* in the current file. The line number may be prefixed with a *filename* and a colon, - to specify a breakpoint in another file (probably one that hasn't been loaded - yet). The file is searched on :data:`sys.path`. Accepatable form of *filename* - includes ``/abspath/to/file.py``, ``relpath/file.py``, ``module`` or + to specify a breakpoint in another file (possibly one that hasn't been loaded + yet). The file is searched on :data:`sys.path`. Accepatable forms of *filename* + are ``/abspath/to/file.py``, ``relpath/file.py``, ``module`` and ``package.module``. With a *function* argument, set a break at the first executable statement within @@ -346,7 +346,7 @@ can be overridden by the local file. of times that breakpoint has been hit, the current ignore count, and the associated condition if any. - Note that each breakpoint is assigned a number to which all the other + Each breakpoint is assigned a number to which all the other breakpoint commands refer. .. pdbcommand:: tbreak [([filename:]lineno | function) [, condition]]