From 9fa2e02fe2c4c01fc725370a1711411bc48e30c4 Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Wed, 16 Sep 2009 16:40:45 +0000 Subject: [PATCH] Merged revisions 74838-74839 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r74838 | georg.brandl | 2009-09-16 18:22:12 +0200 (Mi, 16 Sep 2009) | 1 line Remove some more boilerplate from the actual tests in test_pdb. ........ r74839 | georg.brandl | 2009-09-16 18:36:39 +0200 (Mi, 16 Sep 2009) | 1 line Make the pdb displayhook compatible with the standard displayhook: do not print Nones. Add a test for that. ........ --- Lib/pdb.py | 4 ++- Lib/test/test_pdb.py | 86 +++++++++++++++++++++++++++++++------------- 2 files changed, 64 insertions(+), 26 deletions(-) diff --git a/Lib/pdb.py b/Lib/pdb.py index 627cd29ba7b..22dcff738a1 100755 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -208,7 +208,9 @@ def displayhook(self, obj): """Custom displayhook for the exec in default(), which prevents assignment of the _ variable in the builtins. """ - print(repr(obj)) + # reproduce the behavior of the standard displayhook, not printing None + if obj is not None: + print(repr(obj)) def default(self, line): if line[:1] == '!': line = line[1:] diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index bcd4853d632..5d52707f408 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -12,6 +12,49 @@ from test.test_doctest import _FakeInput +class PdbTestInput(object): + """Context manager that makes testing Pdb in doctests easier.""" + + def __init__(self, input): + self.input = input + + def __enter__(self): + self.real_stdin = sys.stdin + sys.stdin = _FakeInput(self.input) + + def __exit__(self, *exc): + sys.stdin = self.real_stdin + + +def test_pdb_displayhook(): + """This tests the custom displayhook for pdb. + + >>> def test_function(foo, bar): + ... import pdb; pdb.Pdb().set_trace() + ... pass + + >>> with PdbTestInput([ + ... 'foo', + ... 'bar', + ... 'for i in range(5): print(i)', + ... 'continue', + ... ]): + ... test_function(1, None) + > (3)test_function() + -> pass + (Pdb) foo + 1 + (Pdb) bar + (Pdb) for i in range(5): print(i) + 0 + 1 + 2 + 3 + 4 + (Pdb) continue + """ + + def test_pdb_skip_modules(): """This illustrates the simple case of module skipping. @@ -19,16 +62,12 @@ def test_pdb_skip_modules(): ... import string ... import pdb; pdb.Pdb(skip=['stri*']).set_trace() ... string.capwords('FOO') - >>> real_stdin = sys.stdin - >>> sys.stdin = _FakeInput([ - ... 'step', - ... 'continue', - ... ]) - >>> try: + >>> with PdbTestInput([ + ... 'step', + ... 'continue', + ... ]): ... skip_module() - ... finally: - ... sys.stdin = real_stdin > (4)skip_module() -> string.capwords('FOO') (Pdb) step @@ -36,7 +75,7 @@ def test_pdb_skip_modules(): > (4)skip_module()->None -> string.capwords('FOO') (Pdb) continue -""" + """ # Module for testing skipping of module that makes a callback @@ -50,22 +89,19 @@ def test_pdb_skip_modules_with_callback(): >>> def skip_module(): ... def callback(): ... return None - ... import pdb;pdb.Pdb(skip=['module_to_skip*']).set_trace() + ... import pdb; pdb.Pdb(skip=['module_to_skip*']).set_trace() ... mod.foo_pony(callback) - >>> real_stdin = sys.stdin - >>> sys.stdin = _FakeInput([ - ... 'step', - ... 'step', - ... 'step', - ... 'step', - ... 'step', - ... 'continue', - ... ]) - >>> try: + >>> with PdbTestInput([ + ... 'step', + ... 'step', + ... 'step', + ... 'step', + ... 'step', + ... 'continue', + ... ]): ... skip_module() - ... finally: - ... sys.stdin = real_stdin + ... pass # provides something to "step" to > (5)skip_module() -> mod.foo_pony(callback) (Pdb) step @@ -84,10 +120,10 @@ def test_pdb_skip_modules_with_callback(): > (5)skip_module()->None -> mod.foo_pony(callback) (Pdb) step - > (4)() - -> sys.stdin = real_stdin + > (10)() + -> pass # provides something to "step" to (Pdb) continue -""" + """ def test_main():