diff --git a/Lib/pdb.py b/Lib/pdb.py index 081023526c0..d7d95715945 100755 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -1312,14 +1312,6 @@ class Pdb(bdb.Bdb, cmd.Cmd): # _getval() already printed the error return code = None - # Is it a function? - try: - code = value.__code__ - except Exception: - pass - if code: - self.message('Function %s' % code.co_name) - return # Is it an instance method? try: code = value.__func__.__code__ @@ -1328,6 +1320,14 @@ class Pdb(bdb.Bdb, cmd.Cmd): if code: self.message('Method %s' % code.co_name) return + # Is it a function? + try: + code = value.__code__ + except Exception: + pass + if code: + self.message('Function %s' % code.co_name) + return # Is it a class? if value.__class__ is type: self.message('Class %s.%s' % (value.__module__, value.__qualname__)) diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index 65bca291d96..8016f81e5ac 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -425,6 +425,47 @@ def test_list_commands(): (Pdb) continue """ +def test_pdb_whatis_command(): + """Test the whatis command + + >>> myvar = (1,2) + >>> def myfunc(): + ... pass + + >>> class MyClass: + ... def mymethod(self): + ... pass + + >>> def test_function(): + ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() + + >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE + ... 'whatis myvar', + ... 'whatis myfunc', + ... 'whatis MyClass', + ... 'whatis MyClass()', + ... 'whatis MyClass.mymethod', + ... 'whatis MyClass().mymethod', + ... 'continue', + ... ]): + ... test_function() + --Return-- + > (2)test_function()->None + -> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() + (Pdb) whatis myvar + + (Pdb) whatis myfunc + Function myfunc + (Pdb) whatis MyClass + Class test.test_pdb.MyClass + (Pdb) whatis MyClass() + + (Pdb) whatis MyClass.mymethod + Function mymethod + (Pdb) whatis MyClass().mymethod + Method mymethod + (Pdb) continue + """ def test_post_mortem(): """Test post mortem traceback debugging. diff --git a/Misc/NEWS.d/next/Library/2020-08-21-15-51-15.bpo-41609.JmiUKG.rst b/Misc/NEWS.d/next/Library/2020-08-21-15-51-15.bpo-41609.JmiUKG.rst new file mode 100644 index 00000000000..ecaf40eee7b --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-08-21-15-51-15.bpo-41609.JmiUKG.rst @@ -0,0 +1 @@ +The pdb whatis command correctly reports instance methods as 'Method' rather than 'Function'. \ No newline at end of file