ยปCore Development>Code coverage>Lib/test/test_pdb.py

Python code coverage for Lib/test/test_pdb.py

#countcontent
1n/a# A test suite for pdb; not very comprehensive at the moment.
2n/a
3n/aimport doctest
4n/aimport os
5n/aimport pdb
6n/aimport sys
7n/aimport types
8n/aimport unittest
9n/aimport subprocess
10n/aimport textwrap
11n/a
12n/afrom test import support
13n/a# This little helper class is essential for testing pdb under doctest.
14n/afrom test.test_doctest import _FakeInput
15n/a
16n/a
17n/aclass PdbTestInput(object):
18n/a """Context manager that makes testing Pdb in doctests easier."""
19n/a
20n/a def __init__(self, input):
21n/a self.input = input
22n/a
23n/a def __enter__(self):
24n/a self.real_stdin = sys.stdin
25n/a sys.stdin = _FakeInput(self.input)
26n/a self.orig_trace = sys.gettrace() if hasattr(sys, 'gettrace') else None
27n/a
28n/a def __exit__(self, *exc):
29n/a sys.stdin = self.real_stdin
30n/a if self.orig_trace:
31n/a sys.settrace(self.orig_trace)
32n/a
33n/a
34n/adef test_pdb_displayhook():
35n/a """This tests the custom displayhook for pdb.
36n/a
37n/a >>> def test_function(foo, bar):
38n/a ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
39n/a ... pass
40n/a
41n/a >>> with PdbTestInput([
42n/a ... 'foo',
43n/a ... 'bar',
44n/a ... 'for i in range(5): print(i)',
45n/a ... 'continue',
46n/a ... ]):
47n/a ... test_function(1, None)
48n/a > <doctest test.test_pdb.test_pdb_displayhook[0]>(3)test_function()
49n/a -> pass
50n/a (Pdb) foo
51n/a 1
52n/a (Pdb) bar
53n/a (Pdb) for i in range(5): print(i)
54n/a 0
55n/a 1
56n/a 2
57n/a 3
58n/a 4
59n/a (Pdb) continue
60n/a """
61n/a
62n/a
63n/adef test_pdb_basic_commands():
64n/a """Test the basic commands of pdb.
65n/a
66n/a >>> def test_function_2(foo, bar='default'):
67n/a ... print(foo)
68n/a ... for i in range(5):
69n/a ... print(i)
70n/a ... print(bar)
71n/a ... for i in range(10):
72n/a ... never_executed
73n/a ... print('after for')
74n/a ... print('...')
75n/a ... return foo.upper()
76n/a
77n/a >>> def test_function():
78n/a ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
79n/a ... ret = test_function_2('baz')
80n/a ... print(ret)
81n/a
82n/a >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
83n/a ... 'step', # entering the function call
84n/a ... 'args', # display function args
85n/a ... 'list', # list function source
86n/a ... 'bt', # display backtrace
87n/a ... 'up', # step up to test_function()
88n/a ... 'down', # step down to test_function_2() again
89n/a ... 'next', # stepping to print(foo)
90n/a ... 'next', # stepping to the for loop
91n/a ... 'step', # stepping into the for loop
92n/a ... 'until', # continuing until out of the for loop
93n/a ... 'next', # executing the print(bar)
94n/a ... 'jump 8', # jump over second for loop
95n/a ... 'return', # return out of function
96n/a ... 'retval', # display return value
97n/a ... 'continue',
98n/a ... ]):
99n/a ... test_function()
100n/a > <doctest test.test_pdb.test_pdb_basic_commands[1]>(3)test_function()
101n/a -> ret = test_function_2('baz')
102n/a (Pdb) step
103n/a --Call--
104n/a > <doctest test.test_pdb.test_pdb_basic_commands[0]>(1)test_function_2()
105n/a -> def test_function_2(foo, bar='default'):
106n/a (Pdb) args
107n/a foo = 'baz'
108n/a bar = 'default'
109n/a (Pdb) list
110n/a 1 -> def test_function_2(foo, bar='default'):
111n/a 2 print(foo)
112n/a 3 for i in range(5):
113n/a 4 print(i)
114n/a 5 print(bar)
115n/a 6 for i in range(10):
116n/a 7 never_executed
117n/a 8 print('after for')
118n/a 9 print('...')
119n/a 10 return foo.upper()
120n/a [EOF]
121n/a (Pdb) bt
122n/a ...
123n/a <doctest test.test_pdb.test_pdb_basic_commands[2]>(18)<module>()
124n/a -> test_function()
125n/a <doctest test.test_pdb.test_pdb_basic_commands[1]>(3)test_function()
126n/a -> ret = test_function_2('baz')
127n/a > <doctest test.test_pdb.test_pdb_basic_commands[0]>(1)test_function_2()
128n/a -> def test_function_2(foo, bar='default'):
129n/a (Pdb) up
130n/a > <doctest test.test_pdb.test_pdb_basic_commands[1]>(3)test_function()
131n/a -> ret = test_function_2('baz')
132n/a (Pdb) down
133n/a > <doctest test.test_pdb.test_pdb_basic_commands[0]>(1)test_function_2()
134n/a -> def test_function_2(foo, bar='default'):
135n/a (Pdb) next
136n/a > <doctest test.test_pdb.test_pdb_basic_commands[0]>(2)test_function_2()
137n/a -> print(foo)
138n/a (Pdb) next
139n/a baz
140n/a > <doctest test.test_pdb.test_pdb_basic_commands[0]>(3)test_function_2()
141n/a -> for i in range(5):
142n/a (Pdb) step
143n/a > <doctest test.test_pdb.test_pdb_basic_commands[0]>(4)test_function_2()
144n/a -> print(i)
145n/a (Pdb) until
146n/a 0
147n/a 1
148n/a 2
149n/a 3
150n/a 4
151n/a > <doctest test.test_pdb.test_pdb_basic_commands[0]>(5)test_function_2()
152n/a -> print(bar)
153n/a (Pdb) next
154n/a default
155n/a > <doctest test.test_pdb.test_pdb_basic_commands[0]>(6)test_function_2()
156n/a -> for i in range(10):
157n/a (Pdb) jump 8
158n/a > <doctest test.test_pdb.test_pdb_basic_commands[0]>(8)test_function_2()
159n/a -> print('after for')
160n/a (Pdb) return
161n/a after for
162n/a ...
163n/a --Return--
164n/a > <doctest test.test_pdb.test_pdb_basic_commands[0]>(10)test_function_2()->'BAZ'
165n/a -> return foo.upper()
166n/a (Pdb) retval
167n/a 'BAZ'
168n/a (Pdb) continue
169n/a BAZ
170n/a """
171n/a
172n/a
173n/adef test_pdb_breakpoint_commands():
174n/a """Test basic commands related to breakpoints.
175n/a
176n/a >>> def test_function():
177n/a ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
178n/a ... print(1)
179n/a ... print(2)
180n/a ... print(3)
181n/a ... print(4)
182n/a
183n/a First, need to clear bdb state that might be left over from previous tests.
184n/a Otherwise, the new breakpoints might get assigned different numbers.
185n/a
186n/a >>> from bdb import Breakpoint
187n/a >>> Breakpoint.next = 1
188n/a >>> Breakpoint.bplist = {}
189n/a >>> Breakpoint.bpbynumber = [None]
190n/a
191n/a Now test the breakpoint commands. NORMALIZE_WHITESPACE is needed because
192n/a the breakpoint list outputs a tab for the "stop only" and "ignore next"
193n/a lines, which we don't want to put in here.
194n/a
195n/a >>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE
196n/a ... 'break 3',
197n/a ... 'disable 1',
198n/a ... 'ignore 1 10',
199n/a ... 'condition 1 1 < 2',
200n/a ... 'break 4',
201n/a ... 'break 4',
202n/a ... 'break',
203n/a ... 'clear 3',
204n/a ... 'break',
205n/a ... 'condition 1',
206n/a ... 'enable 1',
207n/a ... 'clear 1',
208n/a ... 'commands 2',
209n/a ... 'p "42"',
210n/a ... 'print("42", 7*6)', # Issue 18764 (not about breakpoints)
211n/a ... 'end',
212n/a ... 'continue', # will stop at breakpoint 2 (line 4)
213n/a ... 'clear', # clear all!
214n/a ... 'y',
215n/a ... 'tbreak 5',
216n/a ... 'continue', # will stop at temporary breakpoint
217n/a ... 'break', # make sure breakpoint is gone
218n/a ... 'continue',
219n/a ... ]):
220n/a ... test_function()
221n/a > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(3)test_function()
222n/a -> print(1)
223n/a (Pdb) break 3
224n/a Breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
225n/a (Pdb) disable 1
226n/a Disabled breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
227n/a (Pdb) ignore 1 10
228n/a Will ignore next 10 crossings of breakpoint 1.
229n/a (Pdb) condition 1 1 < 2
230n/a New condition set for breakpoint 1.
231n/a (Pdb) break 4
232n/a Breakpoint 2 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
233n/a (Pdb) break 4
234n/a Breakpoint 3 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
235n/a (Pdb) break
236n/a Num Type Disp Enb Where
237n/a 1 breakpoint keep no at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
238n/a stop only if 1 < 2
239n/a ignore next 10 hits
240n/a 2 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
241n/a 3 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
242n/a (Pdb) clear 3
243n/a Deleted breakpoint 3 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
244n/a (Pdb) break
245n/a Num Type Disp Enb Where
246n/a 1 breakpoint keep no at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
247n/a stop only if 1 < 2
248n/a ignore next 10 hits
249n/a 2 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
250n/a (Pdb) condition 1
251n/a Breakpoint 1 is now unconditional.
252n/a (Pdb) enable 1
253n/a Enabled breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
254n/a (Pdb) clear 1
255n/a Deleted breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
256n/a (Pdb) commands 2
257n/a (com) p "42"
258n/a (com) print("42", 7*6)
259n/a (com) end
260n/a (Pdb) continue
261n/a 1
262n/a '42'
263n/a 42 42
264n/a > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(4)test_function()
265n/a -> print(2)
266n/a (Pdb) clear
267n/a Clear all breaks? y
268n/a Deleted breakpoint 2 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
269n/a (Pdb) tbreak 5
270n/a Breakpoint 4 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:5
271n/a (Pdb) continue
272n/a 2
273n/a Deleted breakpoint 4 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:5
274n/a > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(5)test_function()
275n/a -> print(3)
276n/a (Pdb) break
277n/a (Pdb) continue
278n/a 3
279n/a 4
280n/a """
281n/a
282n/a
283n/adef do_nothing():
284n/a pass
285n/a
286n/adef do_something():
287n/a print(42)
288n/a
289n/adef test_list_commands():
290n/a """Test the list and source commands of pdb.
291n/a
292n/a >>> def test_function_2(foo):
293n/a ... import test.test_pdb
294n/a ... test.test_pdb.do_nothing()
295n/a ... 'some...'
296n/a ... 'more...'
297n/a ... 'code...'
298n/a ... 'to...'
299n/a ... 'make...'
300n/a ... 'a...'
301n/a ... 'long...'
302n/a ... 'listing...'
303n/a ... 'useful...'
304n/a ... '...'
305n/a ... '...'
306n/a ... return foo
307n/a
308n/a >>> def test_function():
309n/a ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
310n/a ... ret = test_function_2('baz')
311n/a
312n/a >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
313n/a ... 'list', # list first function
314n/a ... 'step', # step into second function
315n/a ... 'list', # list second function
316n/a ... 'list', # continue listing to EOF
317n/a ... 'list 1,3', # list specific lines
318n/a ... 'list x', # invalid argument
319n/a ... 'next', # step to import
320n/a ... 'next', # step over import
321n/a ... 'step', # step into do_nothing
322n/a ... 'longlist', # list all lines
323n/a ... 'source do_something', # list all lines of function
324n/a ... 'source fooxxx', # something that doesn't exit
325n/a ... 'continue',
326n/a ... ]):
327n/a ... test_function()
328n/a > <doctest test.test_pdb.test_list_commands[1]>(3)test_function()
329n/a -> ret = test_function_2('baz')
330n/a (Pdb) list
331n/a 1 def test_function():
332n/a 2 import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
333n/a 3 -> ret = test_function_2('baz')
334n/a [EOF]
335n/a (Pdb) step
336n/a --Call--
337n/a > <doctest test.test_pdb.test_list_commands[0]>(1)test_function_2()
338n/a -> def test_function_2(foo):
339n/a (Pdb) list
340n/a 1 -> def test_function_2(foo):
341n/a 2 import test.test_pdb
342n/a 3 test.test_pdb.do_nothing()
343n/a 4 'some...'
344n/a 5 'more...'
345n/a 6 'code...'
346n/a 7 'to...'
347n/a 8 'make...'
348n/a 9 'a...'
349n/a 10 'long...'
350n/a 11 'listing...'
351n/a (Pdb) list
352n/a 12 'useful...'
353n/a 13 '...'
354n/a 14 '...'
355n/a 15 return foo
356n/a [EOF]
357n/a (Pdb) list 1,3
358n/a 1 -> def test_function_2(foo):
359n/a 2 import test.test_pdb
360n/a 3 test.test_pdb.do_nothing()
361n/a (Pdb) list x
362n/a *** ...
363n/a (Pdb) next
364n/a > <doctest test.test_pdb.test_list_commands[0]>(2)test_function_2()
365n/a -> import test.test_pdb
366n/a (Pdb) next
367n/a > <doctest test.test_pdb.test_list_commands[0]>(3)test_function_2()
368n/a -> test.test_pdb.do_nothing()
369n/a (Pdb) step
370n/a --Call--
371n/a > ...test_pdb.py(...)do_nothing()
372n/a -> def do_nothing():
373n/a (Pdb) longlist
374n/a ... -> def do_nothing():
375n/a ... pass
376n/a (Pdb) source do_something
377n/a ... def do_something():
378n/a ... print(42)
379n/a (Pdb) source fooxxx
380n/a *** ...
381n/a (Pdb) continue
382n/a """
383n/a
384n/a
385n/adef test_post_mortem():
386n/a """Test post mortem traceback debugging.
387n/a
388n/a >>> def test_function_2():
389n/a ... try:
390n/a ... 1/0
391n/a ... finally:
392n/a ... print('Exception!')
393n/a
394n/a >>> def test_function():
395n/a ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
396n/a ... test_function_2()
397n/a ... print('Not reached.')
398n/a
399n/a >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
400n/a ... 'next', # step over exception-raising call
401n/a ... 'bt', # get a backtrace
402n/a ... 'list', # list code of test_function()
403n/a ... 'down', # step into test_function_2()
404n/a ... 'list', # list code of test_function_2()
405n/a ... 'continue',
406n/a ... ]):
407n/a ... try:
408n/a ... test_function()
409n/a ... except ZeroDivisionError:
410n/a ... print('Correctly reraised.')
411n/a > <doctest test.test_pdb.test_post_mortem[1]>(3)test_function()
412n/a -> test_function_2()
413n/a (Pdb) next
414n/a Exception!
415n/a ZeroDivisionError: division by zero
416n/a > <doctest test.test_pdb.test_post_mortem[1]>(3)test_function()
417n/a -> test_function_2()
418n/a (Pdb) bt
419n/a ...
420n/a <doctest test.test_pdb.test_post_mortem[2]>(10)<module>()
421n/a -> test_function()
422n/a > <doctest test.test_pdb.test_post_mortem[1]>(3)test_function()
423n/a -> test_function_2()
424n/a <doctest test.test_pdb.test_post_mortem[0]>(3)test_function_2()
425n/a -> 1/0
426n/a (Pdb) list
427n/a 1 def test_function():
428n/a 2 import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
429n/a 3 -> test_function_2()
430n/a 4 print('Not reached.')
431n/a [EOF]
432n/a (Pdb) down
433n/a > <doctest test.test_pdb.test_post_mortem[0]>(3)test_function_2()
434n/a -> 1/0
435n/a (Pdb) list
436n/a 1 def test_function_2():
437n/a 2 try:
438n/a 3 >> 1/0
439n/a 4 finally:
440n/a 5 -> print('Exception!')
441n/a [EOF]
442n/a (Pdb) continue
443n/a Correctly reraised.
444n/a """
445n/a
446n/a
447n/adef test_pdb_skip_modules():
448n/a """This illustrates the simple case of module skipping.
449n/a
450n/a >>> def skip_module():
451n/a ... import string
452n/a ... import pdb; pdb.Pdb(skip=['stri*'], nosigint=True, readrc=False).set_trace()
453n/a ... string.capwords('FOO')
454n/a
455n/a >>> with PdbTestInput([
456n/a ... 'step',
457n/a ... 'continue',
458n/a ... ]):
459n/a ... skip_module()
460n/a > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()
461n/a -> string.capwords('FOO')
462n/a (Pdb) step
463n/a --Return--
464n/a > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()->None
465n/a -> string.capwords('FOO')
466n/a (Pdb) continue
467n/a """
468n/a
469n/a
470n/a# Module for testing skipping of module that makes a callback
471n/amod = types.ModuleType('module_to_skip')
472n/aexec('def foo_pony(callback): x = 1; callback(); return None', mod.__dict__)
473n/a
474n/a
475n/adef test_pdb_skip_modules_with_callback():
476n/a """This illustrates skipping of modules that call into other code.
477n/a
478n/a >>> def skip_module():
479n/a ... def callback():
480n/a ... return None
481n/a ... import pdb; pdb.Pdb(skip=['module_to_skip*'], nosigint=True, readrc=False).set_trace()
482n/a ... mod.foo_pony(callback)
483n/a
484n/a >>> with PdbTestInput([
485n/a ... 'step',
486n/a ... 'step',
487n/a ... 'step',
488n/a ... 'step',
489n/a ... 'step',
490n/a ... 'continue',
491n/a ... ]):
492n/a ... skip_module()
493n/a ... pass # provides something to "step" to
494n/a > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()
495n/a -> mod.foo_pony(callback)
496n/a (Pdb) step
497n/a --Call--
498n/a > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(2)callback()
499n/a -> def callback():
500n/a (Pdb) step
501n/a > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()
502n/a -> return None
503n/a (Pdb) step
504n/a --Return--
505n/a > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()->None
506n/a -> return None
507n/a (Pdb) step
508n/a --Return--
509n/a > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()->None
510n/a -> mod.foo_pony(callback)
511n/a (Pdb) step
512n/a > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[1]>(10)<module>()
513n/a -> pass # provides something to "step" to
514n/a (Pdb) continue
515n/a """
516n/a
517n/a
518n/adef test_pdb_continue_in_bottomframe():
519n/a """Test that "continue" and "next" work properly in bottom frame (issue #5294).
520n/a
521n/a >>> def test_function():
522n/a ... import pdb, sys; inst = pdb.Pdb(nosigint=True, readrc=False)
523n/a ... inst.set_trace()
524n/a ... inst.botframe = sys._getframe() # hackery to get the right botframe
525n/a ... print(1)
526n/a ... print(2)
527n/a ... print(3)
528n/a ... print(4)
529n/a
530n/a >>> with PdbTestInput([ # doctest: +ELLIPSIS
531n/a ... 'next',
532n/a ... 'break 7',
533n/a ... 'continue',
534n/a ... 'next',
535n/a ... 'continue',
536n/a ... 'continue',
537n/a ... ]):
538n/a ... test_function()
539n/a > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(4)test_function()
540n/a -> inst.botframe = sys._getframe() # hackery to get the right botframe
541n/a (Pdb) next
542n/a > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(5)test_function()
543n/a -> print(1)
544n/a (Pdb) break 7
545n/a Breakpoint ... at <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>:7
546n/a (Pdb) continue
547n/a 1
548n/a 2
549n/a > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(7)test_function()
550n/a -> print(3)
551n/a (Pdb) next
552n/a 3
553n/a > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(8)test_function()
554n/a -> print(4)
555n/a (Pdb) continue
556n/a 4
557n/a """
558n/a
559n/a
560n/adef pdb_invoke(method, arg):
561n/a """Run pdb.method(arg)."""
562n/a getattr(pdb.Pdb(nosigint=True, readrc=False), method)(arg)
563n/a
564n/a
565n/adef test_pdb_run_with_incorrect_argument():
566n/a """Testing run and runeval with incorrect first argument.
567n/a
568n/a >>> pti = PdbTestInput(['continue',])
569n/a >>> with pti:
570n/a ... pdb_invoke('run', lambda x: x)
571n/a Traceback (most recent call last):
572n/a TypeError: exec() arg 1 must be a string, bytes or code object
573n/a
574n/a >>> with pti:
575n/a ... pdb_invoke('runeval', lambda x: x)
576n/a Traceback (most recent call last):
577n/a TypeError: eval() arg 1 must be a string, bytes or code object
578n/a """
579n/a
580n/a
581n/adef test_pdb_run_with_code_object():
582n/a """Testing run and runeval with code object as a first argument.
583n/a
584n/a >>> with PdbTestInput(['step','x', 'continue']): # doctest: +ELLIPSIS
585n/a ... pdb_invoke('run', compile('x=1', '<string>', 'exec'))
586n/a > <string>(1)<module>()...
587n/a (Pdb) step
588n/a --Return--
589n/a > <string>(1)<module>()->None
590n/a (Pdb) x
591n/a 1
592n/a (Pdb) continue
593n/a
594n/a >>> with PdbTestInput(['x', 'continue']):
595n/a ... x=0
596n/a ... pdb_invoke('runeval', compile('x+1', '<string>', 'eval'))
597n/a > <string>(1)<module>()->None
598n/a (Pdb) x
599n/a 1
600n/a (Pdb) continue
601n/a """
602n/a
603n/adef test_next_until_return_at_return_event():
604n/a """Test that pdb stops after a next/until/return issued at a return debug event.
605n/a
606n/a >>> def test_function_2():
607n/a ... x = 1
608n/a ... x = 2
609n/a
610n/a >>> def test_function():
611n/a ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
612n/a ... test_function_2()
613n/a ... test_function_2()
614n/a ... test_function_2()
615n/a ... end = 1
616n/a
617n/a >>> from bdb import Breakpoint
618n/a >>> Breakpoint.next = 1
619n/a >>> with PdbTestInput(['break test_function_2',
620n/a ... 'continue',
621n/a ... 'return',
622n/a ... 'next',
623n/a ... 'continue',
624n/a ... 'return',
625n/a ... 'until',
626n/a ... 'continue',
627n/a ... 'return',
628n/a ... 'return',
629n/a ... 'continue']):
630n/a ... test_function()
631n/a > <doctest test.test_pdb.test_next_until_return_at_return_event[1]>(3)test_function()
632n/a -> test_function_2()
633n/a (Pdb) break test_function_2
634n/a Breakpoint 1 at <doctest test.test_pdb.test_next_until_return_at_return_event[0]>:1
635n/a (Pdb) continue
636n/a > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(2)test_function_2()
637n/a -> x = 1
638n/a (Pdb) return
639n/a --Return--
640n/a > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(3)test_function_2()->None
641n/a -> x = 2
642n/a (Pdb) next
643n/a > <doctest test.test_pdb.test_next_until_return_at_return_event[1]>(4)test_function()
644n/a -> test_function_2()
645n/a (Pdb) continue
646n/a > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(2)test_function_2()
647n/a -> x = 1
648n/a (Pdb) return
649n/a --Return--
650n/a > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(3)test_function_2()->None
651n/a -> x = 2
652n/a (Pdb) until
653n/a > <doctest test.test_pdb.test_next_until_return_at_return_event[1]>(5)test_function()
654n/a -> test_function_2()
655n/a (Pdb) continue
656n/a > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(2)test_function_2()
657n/a -> x = 1
658n/a (Pdb) return
659n/a --Return--
660n/a > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(3)test_function_2()->None
661n/a -> x = 2
662n/a (Pdb) return
663n/a > <doctest test.test_pdb.test_next_until_return_at_return_event[1]>(6)test_function()
664n/a -> end = 1
665n/a (Pdb) continue
666n/a """
667n/a
668n/adef test_pdb_next_command_for_generator():
669n/a """Testing skip unwindng stack on yield for generators for "next" command
670n/a
671n/a >>> def test_gen():
672n/a ... yield 0
673n/a ... return 1
674n/a ... yield 2
675n/a
676n/a >>> def test_function():
677n/a ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
678n/a ... it = test_gen()
679n/a ... try:
680n/a ... if next(it) != 0:
681n/a ... raise AssertionError
682n/a ... next(it)
683n/a ... except StopIteration as ex:
684n/a ... if ex.value != 1:
685n/a ... raise AssertionError
686n/a ... print("finished")
687n/a
688n/a >>> with PdbTestInput(['step',
689n/a ... 'step',
690n/a ... 'step',
691n/a ... 'next',
692n/a ... 'next',
693n/a ... 'step',
694n/a ... 'step',
695n/a ... 'continue']):
696n/a ... test_function()
697n/a > <doctest test.test_pdb.test_pdb_next_command_for_generator[1]>(3)test_function()
698n/a -> it = test_gen()
699n/a (Pdb) step
700n/a > <doctest test.test_pdb.test_pdb_next_command_for_generator[1]>(4)test_function()
701n/a -> try:
702n/a (Pdb) step
703n/a > <doctest test.test_pdb.test_pdb_next_command_for_generator[1]>(5)test_function()
704n/a -> if next(it) != 0:
705n/a (Pdb) step
706n/a --Call--
707n/a > <doctest test.test_pdb.test_pdb_next_command_for_generator[0]>(1)test_gen()
708n/a -> def test_gen():
709n/a (Pdb) next
710n/a > <doctest test.test_pdb.test_pdb_next_command_for_generator[0]>(2)test_gen()
711n/a -> yield 0
712n/a (Pdb) next
713n/a > <doctest test.test_pdb.test_pdb_next_command_for_generator[0]>(3)test_gen()
714n/a -> return 1
715n/a (Pdb) step
716n/a --Return--
717n/a > <doctest test.test_pdb.test_pdb_next_command_for_generator[0]>(3)test_gen()->1
718n/a -> return 1
719n/a (Pdb) step
720n/a StopIteration: 1
721n/a > <doctest test.test_pdb.test_pdb_next_command_for_generator[1]>(7)test_function()
722n/a -> next(it)
723n/a (Pdb) continue
724n/a finished
725n/a """
726n/a
727n/adef test_pdb_return_command_for_generator():
728n/a """Testing no unwindng stack on yield for generators
729n/a for "return" command
730n/a
731n/a >>> def test_gen():
732n/a ... yield 0
733n/a ... return 1
734n/a ... yield 2
735n/a
736n/a >>> def test_function():
737n/a ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
738n/a ... it = test_gen()
739n/a ... try:
740n/a ... if next(it) != 0:
741n/a ... raise AssertionError
742n/a ... next(it)
743n/a ... except StopIteration as ex:
744n/a ... if ex.value != 1:
745n/a ... raise AssertionError
746n/a ... print("finished")
747n/a
748n/a >>> with PdbTestInput(['step',
749n/a ... 'step',
750n/a ... 'step',
751n/a ... 'return',
752n/a ... 'step',
753n/a ... 'step',
754n/a ... 'continue']):
755n/a ... test_function()
756n/a > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(3)test_function()
757n/a -> it = test_gen()
758n/a (Pdb) step
759n/a > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(4)test_function()
760n/a -> try:
761n/a (Pdb) step
762n/a > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(5)test_function()
763n/a -> if next(it) != 0:
764n/a (Pdb) step
765n/a --Call--
766n/a > <doctest test.test_pdb.test_pdb_return_command_for_generator[0]>(1)test_gen()
767n/a -> def test_gen():
768n/a (Pdb) return
769n/a StopIteration: 1
770n/a > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(7)test_function()
771n/a -> next(it)
772n/a (Pdb) step
773n/a > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(8)test_function()
774n/a -> except StopIteration as ex:
775n/a (Pdb) step
776n/a > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(9)test_function()
777n/a -> if ex.value != 1:
778n/a (Pdb) continue
779n/a finished
780n/a """
781n/a
782n/adef test_pdb_until_command_for_generator():
783n/a """Testing no unwindng stack on yield for generators
784n/a for "until" command if target breakpoing is not reached
785n/a
786n/a >>> def test_gen():
787n/a ... yield 0
788n/a ... yield 1
789n/a ... yield 2
790n/a
791n/a >>> def test_function():
792n/a ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
793n/a ... for i in test_gen():
794n/a ... print(i)
795n/a ... print("finished")
796n/a
797n/a >>> with PdbTestInput(['step',
798n/a ... 'until 4',
799n/a ... 'step',
800n/a ... 'step',
801n/a ... 'continue']):
802n/a ... test_function()
803n/a > <doctest test.test_pdb.test_pdb_until_command_for_generator[1]>(3)test_function()
804n/a -> for i in test_gen():
805n/a (Pdb) step
806n/a --Call--
807n/a > <doctest test.test_pdb.test_pdb_until_command_for_generator[0]>(1)test_gen()
808n/a -> def test_gen():
809n/a (Pdb) until 4
810n/a 0
811n/a 1
812n/a > <doctest test.test_pdb.test_pdb_until_command_for_generator[0]>(4)test_gen()
813n/a -> yield 2
814n/a (Pdb) step
815n/a --Return--
816n/a > <doctest test.test_pdb.test_pdb_until_command_for_generator[0]>(4)test_gen()->2
817n/a -> yield 2
818n/a (Pdb) step
819n/a > <doctest test.test_pdb.test_pdb_until_command_for_generator[1]>(4)test_function()
820n/a -> print(i)
821n/a (Pdb) continue
822n/a 2
823n/a finished
824n/a """
825n/a
826n/adef test_pdb_next_command_in_generator_for_loop():
827n/a """The next command on returning from a generator controlled by a for loop.
828n/a
829n/a >>> def test_gen():
830n/a ... yield 0
831n/a ... return 1
832n/a
833n/a >>> def test_function():
834n/a ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
835n/a ... for i in test_gen():
836n/a ... print('value', i)
837n/a ... x = 123
838n/a
839n/a >>> with PdbTestInput(['break test_gen',
840n/a ... 'continue',
841n/a ... 'next',
842n/a ... 'next',
843n/a ... 'next',
844n/a ... 'continue']):
845n/a ... test_function()
846n/a > <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[1]>(3)test_function()
847n/a -> for i in test_gen():
848n/a (Pdb) break test_gen
849n/a Breakpoint 6 at <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[0]>:1
850n/a (Pdb) continue
851n/a > <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[0]>(2)test_gen()
852n/a -> yield 0
853n/a (Pdb) next
854n/a value 0
855n/a > <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[0]>(3)test_gen()
856n/a -> return 1
857n/a (Pdb) next
858n/a Internal StopIteration: 1
859n/a > <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[1]>(3)test_function()
860n/a -> for i in test_gen():
861n/a (Pdb) next
862n/a > <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[1]>(5)test_function()
863n/a -> x = 123
864n/a (Pdb) continue
865n/a """
866n/a
867n/adef test_pdb_next_command_subiterator():
868n/a """The next command in a generator with a subiterator.
869n/a
870n/a >>> def test_subgenerator():
871n/a ... yield 0
872n/a ... return 1
873n/a
874n/a >>> def test_gen():
875n/a ... x = yield from test_subgenerator()
876n/a ... return x
877n/a
878n/a >>> def test_function():
879n/a ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
880n/a ... for i in test_gen():
881n/a ... print('value', i)
882n/a ... x = 123
883n/a
884n/a >>> with PdbTestInput(['step',
885n/a ... 'step',
886n/a ... 'next',
887n/a ... 'next',
888n/a ... 'next',
889n/a ... 'continue']):
890n/a ... test_function()
891n/a > <doctest test.test_pdb.test_pdb_next_command_subiterator[2]>(3)test_function()
892n/a -> for i in test_gen():
893n/a (Pdb) step
894n/a --Call--
895n/a > <doctest test.test_pdb.test_pdb_next_command_subiterator[1]>(1)test_gen()
896n/a -> def test_gen():
897n/a (Pdb) step
898n/a > <doctest test.test_pdb.test_pdb_next_command_subiterator[1]>(2)test_gen()
899n/a -> x = yield from test_subgenerator()
900n/a (Pdb) next
901n/a value 0
902n/a > <doctest test.test_pdb.test_pdb_next_command_subiterator[1]>(3)test_gen()
903n/a -> return x
904n/a (Pdb) next
905n/a Internal StopIteration: 1
906n/a > <doctest test.test_pdb.test_pdb_next_command_subiterator[2]>(3)test_function()
907n/a -> for i in test_gen():
908n/a (Pdb) next
909n/a > <doctest test.test_pdb.test_pdb_next_command_subiterator[2]>(5)test_function()
910n/a -> x = 123
911n/a (Pdb) continue
912n/a """
913n/a
914n/adef test_pdb_issue_20766():
915n/a """Test for reference leaks when the SIGINT handler is set.
916n/a
917n/a >>> def test_function():
918n/a ... i = 1
919n/a ... while i <= 2:
920n/a ... sess = pdb.Pdb()
921n/a ... sess.set_trace(sys._getframe())
922n/a ... print('pdb %d: %s' % (i, sess._previous_sigint_handler))
923n/a ... i += 1
924n/a
925n/a >>> with PdbTestInput(['continue',
926n/a ... 'continue']):
927n/a ... test_function()
928n/a > <doctest test.test_pdb.test_pdb_issue_20766[0]>(6)test_function()
929n/a -> print('pdb %d: %s' % (i, sess._previous_sigint_handler))
930n/a (Pdb) continue
931n/a pdb 1: <built-in function default_int_handler>
932n/a > <doctest test.test_pdb.test_pdb_issue_20766[0]>(5)test_function()
933n/a -> sess.set_trace(sys._getframe())
934n/a (Pdb) continue
935n/a pdb 2: <built-in function default_int_handler>
936n/a """
937n/a
938n/aclass PdbTestCase(unittest.TestCase):
939n/a
940n/a def run_pdb(self, script, commands):
941n/a """Run 'script' lines with pdb and the pdb 'commands'."""
942n/a filename = 'main.py'
943n/a with open(filename, 'w') as f:
944n/a f.write(textwrap.dedent(script))
945n/a self.addCleanup(support.unlink, filename)
946n/a self.addCleanup(support.rmtree, '__pycache__')
947n/a cmd = [sys.executable, '-m', 'pdb', filename]
948n/a stdout = stderr = None
949n/a with subprocess.Popen(cmd, stdout=subprocess.PIPE,
950n/a stdin=subprocess.PIPE,
951n/a stderr=subprocess.STDOUT,
952n/a ) as proc:
953n/a stdout, stderr = proc.communicate(str.encode(commands))
954n/a stdout = stdout and bytes.decode(stdout)
955n/a stderr = stderr and bytes.decode(stderr)
956n/a return stdout, stderr
957n/a
958n/a def _assert_find_function(self, file_content, func_name, expected):
959n/a file_content = textwrap.dedent(file_content)
960n/a
961n/a with open(support.TESTFN, 'w') as f:
962n/a f.write(file_content)
963n/a
964n/a expected = None if not expected else (
965n/a expected[0], support.TESTFN, expected[1])
966n/a self.assertEqual(
967n/a expected, pdb.find_function(func_name, support.TESTFN))
968n/a
969n/a def test_find_function_empty_file(self):
970n/a self._assert_find_function('', 'foo', None)
971n/a
972n/a def test_find_function_found(self):
973n/a self._assert_find_function(
974n/a """\
975n/a def foo():
976n/a pass
977n/a
978n/a def bar():
979n/a pass
980n/a
981n/a def quux():
982n/a pass
983n/a """,
984n/a 'bar',
985n/a ('bar', 4),
986n/a )
987n/a
988n/a def test_issue7964(self):
989n/a # open the file as binary so we can force \r\n newline
990n/a with open(support.TESTFN, 'wb') as f:
991n/a f.write(b'print("testing my pdb")\r\n')
992n/a cmd = [sys.executable, '-m', 'pdb', support.TESTFN]
993n/a proc = subprocess.Popen(cmd,
994n/a stdout=subprocess.PIPE,
995n/a stdin=subprocess.PIPE,
996n/a stderr=subprocess.STDOUT,
997n/a )
998n/a self.addCleanup(proc.stdout.close)
999n/a stdout, stderr = proc.communicate(b'quit\n')
1000n/a self.assertNotIn(b'SyntaxError', stdout,
1001n/a "Got a syntax error running test script under PDB")
1002n/a
1003n/a def test_issue13183(self):
1004n/a script = """
1005n/a from bar import bar
1006n/a
1007n/a def foo():
1008n/a bar()
1009n/a
1010n/a def nope():
1011n/a pass
1012n/a
1013n/a def foobar():
1014n/a foo()
1015n/a nope()
1016n/a
1017n/a foobar()
1018n/a """
1019n/a commands = """
1020n/a from bar import bar
1021n/a break bar
1022n/a continue
1023n/a step
1024n/a step
1025n/a quit
1026n/a """
1027n/a bar = """
1028n/a def bar():
1029n/a pass
1030n/a """
1031n/a with open('bar.py', 'w') as f:
1032n/a f.write(textwrap.dedent(bar))
1033n/a self.addCleanup(support.unlink, 'bar.py')
1034n/a stdout, stderr = self.run_pdb(script, commands)
1035n/a self.assertTrue(
1036n/a any('main.py(5)foo()->None' in l for l in stdout.splitlines()),
1037n/a 'Fail to step into the caller after a return')
1038n/a
1039n/a def test_issue13210(self):
1040n/a # invoking "continue" on a non-main thread triggered an exception
1041n/a # inside signal.signal
1042n/a
1043n/a # raises SkipTest if python was built without threads
1044n/a support.import_module('threading')
1045n/a
1046n/a with open(support.TESTFN, 'wb') as f:
1047n/a f.write(textwrap.dedent("""
1048n/a import threading
1049n/a import pdb
1050n/a
1051n/a def start_pdb():
1052n/a pdb.Pdb(readrc=False).set_trace()
1053n/a x = 1
1054n/a y = 1
1055n/a
1056n/a t = threading.Thread(target=start_pdb)
1057n/a t.start()""").encode('ascii'))
1058n/a cmd = [sys.executable, '-u', support.TESTFN]
1059n/a proc = subprocess.Popen(cmd,
1060n/a stdout=subprocess.PIPE,
1061n/a stdin=subprocess.PIPE,
1062n/a stderr=subprocess.STDOUT,
1063n/a )
1064n/a self.addCleanup(proc.stdout.close)
1065n/a stdout, stderr = proc.communicate(b'cont\n')
1066n/a self.assertNotIn('Error', stdout.decode(),
1067n/a "Got an error running test script under PDB")
1068n/a
1069n/a def test_issue16180(self):
1070n/a # A syntax error in the debuggee.
1071n/a script = "def f: pass\n"
1072n/a commands = ''
1073n/a expected = "SyntaxError:"
1074n/a stdout, stderr = self.run_pdb(script, commands)
1075n/a self.assertIn(expected, stdout,
1076n/a '\n\nExpected:\n{}\nGot:\n{}\n'
1077n/a 'Fail to handle a syntax error in the debuggee.'
1078n/a .format(expected, stdout))
1079n/a
1080n/a
1081n/a def test_readrc_kwarg(self):
1082n/a script = textwrap.dedent("""
1083n/a import pdb; pdb.Pdb(readrc=False).set_trace()
1084n/a
1085n/a print('hello')
1086n/a """)
1087n/a
1088n/a save_home = os.environ.pop('HOME', None)
1089n/a try:
1090n/a with support.temp_cwd():
1091n/a with open('.pdbrc', 'w') as f:
1092n/a f.write("invalid\n")
1093n/a
1094n/a with open('main.py', 'w') as f:
1095n/a f.write(script)
1096n/a
1097n/a cmd = [sys.executable, 'main.py']
1098n/a proc = subprocess.Popen(
1099n/a cmd,
1100n/a stdout=subprocess.PIPE,
1101n/a stdin=subprocess.PIPE,
1102n/a stderr=subprocess.PIPE,
1103n/a )
1104n/a with proc:
1105n/a stdout, stderr = proc.communicate(b'q\n')
1106n/a self.assertNotIn("NameError: name 'invalid' is not defined",
1107n/a stdout.decode())
1108n/a
1109n/a finally:
1110n/a if save_home is not None:
1111n/a os.environ['HOME'] = save_home
1112n/a
1113n/a def tearDown(self):
1114n/a support.unlink(support.TESTFN)
1115n/a
1116n/a
1117n/adef load_tests(*args):
1118n/a from test import test_pdb
1119n/a suites = [unittest.makeSuite(PdbTestCase), doctest.DocTestSuite(test_pdb)]
1120n/a return unittest.TestSuite(suites)
1121n/a
1122n/a
1123n/aif __name__ == '__main__':
1124n/a unittest.main()