Skip to content

Commit d7ac427

Browse files
gh-117618: Make package.module searchable for breakpoints and clean up docs (#117619)
1 parent 4a5ad84 commit d7ac427

File tree

4 files changed

+69
-15
lines changed

4 files changed

+69
-15
lines changed

Doc/library/pdb.rst

+13-6
Original file line numberDiff line numberDiff line change
@@ -328,12 +328,16 @@ can be overridden by the local file.
328328

329329
.. pdbcommand:: b(reak) [([filename:]lineno | function) [, condition]]
330330

331-
With a *lineno* argument, set a break there in the current file. With a
332-
*function* argument, set a break at the first executable statement within
333-
that function. The line number may be prefixed with a filename and a colon,
334-
to specify a breakpoint in another file (probably one that hasn't been loaded
335-
yet). The file is searched on :data:`sys.path`. Note that each breakpoint
336-
is assigned a number to which all the other breakpoint commands refer.
331+
With a *lineno* argument, set a break at line *lineno* in the current file.
332+
The line number may be prefixed with a *filename* and a colon,
333+
to specify a breakpoint in another file (possibly one that hasn't been loaded
334+
yet). The file is searched on :data:`sys.path`. Accepatable forms of *filename*
335+
are ``/abspath/to/file.py``, ``relpath/file.py``, ``module`` and
336+
``package.module``.
337+
338+
With a *function* argument, set a break at the first executable statement within
339+
that function. *function* can be any expression that evaluates to a function
340+
in the current namespace.
337341

338342
If a second argument is present, it is an expression which must evaluate to
339343
true before the breakpoint is honored.
@@ -342,6 +346,9 @@ can be overridden by the local file.
342346
of times that breakpoint has been hit, the current ignore count, and the
343347
associated condition if any.
344348

349+
Each breakpoint is assigned a number to which all the other
350+
breakpoint commands refer.
351+
345352
.. pdbcommand:: tbreak [([filename:]lineno | function) [, condition]]
346353

347354
Temporary breakpoint, which is removed automatically when it is first hit.

Lib/pdb.py

+15-9
Original file line numberDiff line numberDiff line change
@@ -2007,17 +2007,23 @@ def lookupmodule(self, filename):
20072007
20082008
lookupmodule() translates (possibly incomplete) file or module name
20092009
into an absolute file name.
2010+
2011+
filename could be in format of:
2012+
* an absolute path like '/path/to/file.py'
2013+
* a relative path like 'file.py' or 'dir/file.py'
2014+
* a module name like 'module' or 'package.module'
2015+
2016+
files and modules will be searched in sys.path.
20102017
"""
2011-
if os.path.isabs(filename) and os.path.exists(filename):
2012-
return filename
2013-
f = os.path.join(sys.path[0], filename)
2014-
if os.path.exists(f) and self.canonic(f) == self.mainpyfile:
2015-
return f
2016-
root, ext = os.path.splitext(filename)
2017-
if ext == '':
2018-
filename = filename + '.py'
2018+
if not filename.endswith('.py'):
2019+
# A module is passed in so convert it to equivalent file
2020+
filename = filename.replace('.', os.sep) + '.py'
2021+
20192022
if os.path.isabs(filename):
2020-
return filename
2023+
if os.path.exists(filename):
2024+
return filename
2025+
return None
2026+
20212027
for dirname in sys.path:
20222028
while os.path.islink(dirname):
20232029
dirname = os.readlink(dirname)

Lib/test/test_pdb.py

+40
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,46 @@ def test_pdb_breakpoint_commands():
354354
4
355355
"""
356356

357+
def test_pdb_breakpoint_with_filename():
358+
"""Breakpoints with filename:lineno
359+
360+
>>> def test_function():
361+
... # inspect_fodder2 is a great module as the line number is stable
362+
... from test.test_inspect import inspect_fodder2 as mod2
363+
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
364+
... mod2.func88()
365+
... mod2.func114()
366+
... # Be a good citizen and clean up the mess
367+
... reset_Breakpoint()
368+
369+
First, need to clear bdb state that might be left over from previous tests.
370+
Otherwise, the new breakpoints might get assigned different numbers.
371+
372+
>>> reset_Breakpoint()
373+
374+
>>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS
375+
... 'break test.test_inspect.inspect_fodder2:90',
376+
... 'continue', # will stop at func88
377+
... 'break test/test_inspect/inspect_fodder2.py:115',
378+
... 'continue', # will stop at func114
379+
... 'continue',
380+
... ]):
381+
... test_function()
382+
> <doctest test.test_pdb.test_pdb_breakpoint_with_filename[0]>(5)test_function()
383+
-> mod2.func88()
384+
(Pdb) break test.test_inspect.inspect_fodder2:90
385+
Breakpoint 1 at ...inspect_fodder2.py:90
386+
(Pdb) continue
387+
> ...inspect_fodder2.py(90)func88()
388+
-> return 90
389+
(Pdb) break test/test_inspect/inspect_fodder2.py:115
390+
Breakpoint 2 at ...inspect_fodder2.py:115
391+
(Pdb) continue
392+
> ...inspect_fodder2.py(115)func114()
393+
-> return 115
394+
(Pdb) continue
395+
"""
396+
357397
def test_pdb_breakpoints_preserved_across_interactive_sessions():
358398
"""Breakpoints are remembered between interactive sessions
359399
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Support ``package.module`` as ``filename`` for ``break`` command of :mod:`pdb`

0 commit comments

Comments
 (0)