Skip to content

Commit 641279e

Browse files
bpo-41609: Fix output of pdb's whatis command for instance methods (pythonGH-21935) (python#21976)
(cherry picked from commit 022bc75) Co-authored-by: Irit Katriel <iritkatriel@yahoo.com>
1 parent 7475aa2 commit 641279e

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

Lib/pdb.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -1312,21 +1312,21 @@ def do_whatis(self, arg):
13121312
# _getval() already printed the error
13131313
return
13141314
code = None
1315-
# Is it a function?
1315+
# Is it an instance method?
13161316
try:
1317-
code = value.__code__
1317+
code = value.__func__.__code__
13181318
except Exception:
13191319
pass
13201320
if code:
1321-
self.message('Function %s' % code.co_name)
1321+
self.message('Method %s' % code.co_name)
13221322
return
1323-
# Is it an instance method?
1323+
# Is it a function?
13241324
try:
1325-
code = value.__func__.__code__
1325+
code = value.__code__
13261326
except Exception:
13271327
pass
13281328
if code:
1329-
self.message('Method %s' % code.co_name)
1329+
self.message('Function %s' % code.co_name)
13301330
return
13311331
# Is it a class?
13321332
if value.__class__ is type:

Lib/test/test_pdb.py

+41
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,47 @@ def test_list_commands():
425425
(Pdb) continue
426426
"""
427427

428+
def test_pdb_whatis_command():
429+
"""Test the whatis command
430+
431+
>>> myvar = (1,2)
432+
>>> def myfunc():
433+
... pass
434+
435+
>>> class MyClass:
436+
... def mymethod(self):
437+
... pass
438+
439+
>>> def test_function():
440+
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
441+
442+
>>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
443+
... 'whatis myvar',
444+
... 'whatis myfunc',
445+
... 'whatis MyClass',
446+
... 'whatis MyClass()',
447+
... 'whatis MyClass.mymethod',
448+
... 'whatis MyClass().mymethod',
449+
... 'continue',
450+
... ]):
451+
... test_function()
452+
--Return--
453+
> <doctest test.test_pdb.test_pdb_whatis_command[3]>(2)test_function()->None
454+
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
455+
(Pdb) whatis myvar
456+
<class 'tuple'>
457+
(Pdb) whatis myfunc
458+
Function myfunc
459+
(Pdb) whatis MyClass
460+
Class test.test_pdb.MyClass
461+
(Pdb) whatis MyClass()
462+
<class 'test.test_pdb.MyClass'>
463+
(Pdb) whatis MyClass.mymethod
464+
Function mymethod
465+
(Pdb) whatis MyClass().mymethod
466+
Method mymethod
467+
(Pdb) continue
468+
"""
428469

429470
def test_post_mortem():
430471
"""Test post mortem traceback debugging.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The pdb whatis command correctly reports instance methods as 'Method' rather than 'Function'.

0 commit comments

Comments
 (0)