Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 243ad66

Browse files
committed
Merged revisions 72322 via svnmerge from
svn+ssh://[email protected]/python/trunk ........ r72322 | georg.brandl | 2009-05-05 10:54:11 +0200 (Di, 05 Mai 2009) | 1 line #5142: add module skipping feature to pdb. ........
1 parent 991f920 commit 243ad66

5 files changed

Lines changed: 164 additions & 7 deletions

File tree

Doc/library/pdb.rst

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
.. _debugger:
32

43
:mod:`pdb` --- The Python Debugger
@@ -50,7 +49,16 @@ after normal exit of the program), pdb will restart the program. Automatic
5049
restarting preserves pdb's state (such as breakpoints) and in most cases is more
5150
useful than quitting the debugger upon program's exit.
5251

53-
Typical usage to inspect a crashed program is::
52+
The typical usage to break into the debugger from a running program is to
53+
insert ::
54+
55+
import pdb; pdb.set_trace()
56+
57+
at the location you want to break into the debugger. You can then step through
58+
the code following this statement, and continue running without debugger using
59+
the ``c`` command.
60+
61+
The typical usage to inspect a crashed program is::
5462

5563
>>> import pdb
5664
>>> import mymodule
@@ -67,10 +75,10 @@ Typical usage to inspect a crashed program is::
6775
-> print(spam)
6876
(Pdb)
6977

78+
7079
The module defines the following functions; each enters the debugger in a
7180
slightly different way:
7281

73-
7482
.. function:: run(statement[, globals[, locals]])
7583

7684
Execute the *statement* (given as a string) under debugger control. The
@@ -113,7 +121,38 @@ slightly different way:
113121

114122
.. function:: pm()
115123

116-
Enter post-mortem debugging of the traceback found in ``sys.last_traceback``.
124+
Enter post-mortem debugging of the traceback found in
125+
:data:`sys.last_traceback`.
126+
127+
128+
The ``run_*`` functions and :func:`set_trace` are aliases for instantiating the
129+
:class:`Pdb` class and calling the method of the same name. If you want to
130+
access further features, you have to do this yourself:
131+
132+
.. class:: Pdb(completekey='tab', stdin=None, stdout=None, skip=None)
133+
134+
:class:`Pdb` is the debugger class.
135+
136+
The *completekey*, *stdin* and *stdout* arguments are passed to the
137+
underlying :class:`cmd.Cmd` class; see the description there.
138+
139+
The *skip* argument, if given, must be an iterable of glob-style module name
140+
patterns. The debugger will not step into frames that originate in a module
141+
that matches one of these patterns. [1]_
142+
143+
Example call to enable tracing with *skip*::
144+
145+
import pdb; pdb.Pdb(skip=['django.*']).set_trace()
146+
147+
.. versionadded:: 2.7
148+
The *skip* argument.
149+
150+
.. method:: run(statement[, globals[, locals]])
151+
runeval(expression[, globals[, locals]])
152+
runcall(function[, argument, ...])
153+
set_trace()
154+
155+
See the documentation for the functions explained above.
117156

118157

119158
.. _debugger-commands:
@@ -336,3 +375,9 @@ run [*args* ...]
336375

337376
q(uit)
338377
Quit from the debugger. The program being executed is aborted.
378+
379+
380+
.. rubric:: Footnotes
381+
382+
.. [1] Whether a frame is considered to originate in a certain module
383+
is determined by the ``__name__`` in the frame globals.

Lib/bdb.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Debugger basics"""
22

3+
import fnmatch
34
import sys
45
import os
56
import types
@@ -19,7 +20,8 @@ class Bdb:
1920
The standard debugger class (pdb.Pdb) is an example.
2021
"""
2122

22-
def __init__(self):
23+
def __init__(self, skip=None):
24+
self.skip = set(skip) if skip else None
2325
self.breaks = {}
2426
self.fncache = {}
2527

@@ -94,9 +96,18 @@ def dispatch_exception(self, frame, arg):
9496
# methods, but they may if they want to redefine the
9597
# definition of stopping and breakpoints.
9698

99+
def is_skipped_module(self, module_name):
100+
for pattern in self.skip:
101+
if fnmatch.fnmatch(module_name, pattern):
102+
return True
103+
return False
104+
97105
def stop_here(self, frame):
98106
# (CT) stopframe may now also be None, see dispatch_call.
99107
# (CT) the former test for None is therefore removed from here.
108+
if self.skip and \
109+
self.is_skipped_module(frame.f_globals.get('__name__')):
110+
return False
100111
if frame is self.stopframe:
101112
return frame.f_lineno >= self.stoplineno
102113
while frame is not None and frame is not self.stopframe:

Lib/pdb.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ def find_function(funcname, filename):
5858

5959
class Pdb(bdb.Bdb, cmd.Cmd):
6060

61-
def __init__(self, completekey='tab', stdin=None, stdout=None):
62-
bdb.Bdb.__init__(self)
61+
def __init__(self, completekey='tab', stdin=None, stdout=None, skip=None):
62+
bdb.Bdb.__init__(self, skip=skip)
6363
cmd.Cmd.__init__(self, completekey, stdin, stdout)
6464
if stdout:
6565
self.use_rawinput = 0

Lib/test/test_pdb.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# A test suite for pdb; at the moment, this only validates skipping of
2+
# specified test modules (RFE #5142).
3+
4+
import imp
5+
import os
6+
import sys
7+
import doctest
8+
import tempfile
9+
10+
from test import support
11+
# This little helper class is essential for testing pdb under doctest.
12+
from test.test_doctest import _FakeInput
13+
14+
15+
def test_pdb_skip_modules():
16+
"""This illustrates the simple case of module skipping.
17+
18+
>>> def skip_module():
19+
... import string
20+
... import pdb; pdb.Pdb(skip=['stri*']).set_trace()
21+
... string.capwords('FOO')
22+
>>> real_stdin = sys.stdin
23+
>>> sys.stdin = _FakeInput([
24+
... 'step',
25+
... 'continue',
26+
... ])
27+
28+
>>> try:
29+
... skip_module()
30+
... finally:
31+
... sys.stdin = real_stdin
32+
> <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()
33+
-> string.capwords('FOO')
34+
(Pdb) step
35+
--Return--
36+
> <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()->None
37+
-> string.capwords('FOO')
38+
(Pdb) continue
39+
"""
40+
41+
42+
# Module for testing skipping of module that makes a callback
43+
mod = imp.new_module('module_to_skip')
44+
exec('def foo_pony(callback): x = 1; callback(); return None', mod.__dict__)
45+
46+
47+
def test_pdb_skip_modules_with_callback():
48+
"""This illustrates skipping of modules that call into other code.
49+
50+
>>> def skip_module():
51+
... def callback():
52+
... return None
53+
... import pdb;pdb.Pdb(skip=['module_to_skip*']).set_trace()
54+
... mod.foo_pony(callback)
55+
>>> real_stdin = sys.stdin
56+
>>> sys.stdin = _FakeInput([
57+
... 'step',
58+
... 'step',
59+
... 'step',
60+
... 'step',
61+
... 'step',
62+
... 'continue',
63+
... ])
64+
65+
>>> try:
66+
... skip_module()
67+
... finally:
68+
... sys.stdin = real_stdin
69+
> <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()
70+
-> mod.foo_pony(callback)
71+
(Pdb) step
72+
--Call--
73+
> <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(2)callback()
74+
-> def callback():
75+
(Pdb) step
76+
> <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()
77+
-> return None
78+
(Pdb) step
79+
--Return--
80+
> <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()->None
81+
-> return None
82+
(Pdb) step
83+
--Return--
84+
> <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()->None
85+
-> mod.foo_pony(callback)
86+
(Pdb) step
87+
> <doctest test.test_pdb.test_pdb_skip_modules_with_callback[3]>(4)<module>()
88+
-> sys.stdin = real_stdin
89+
(Pdb) continue
90+
"""
91+
92+
93+
def test_main():
94+
from test import test_pdb
95+
support.run_doctest(test_pdb, verbosity=True)
96+
97+
98+
if __name__ == '__main__':
99+
test_main()

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ Installation
118118
Library
119119
-------
120120

121+
- Issue #5142: Add the ability to skip modules while stepping to pdb.
122+
121123
- Issue #1309567: Fix linecache behavior of stripping subdirectories when
122124
looking for files given by a relative filename.
123125

0 commit comments

Comments
 (0)