diff --git a/Lib/pdb.py b/Lib/pdb.py index d7d957159458be..a9e7d983ea0383 100755 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -1149,11 +1149,19 @@ def do_args(self, arg): do_a = do_args def do_retval(self, arg): - """retval - Print the return value for the last return of a function. + """retval [variable] + Print or store the return value for the last return of a function. + + When used without argument, prints the return value + + When used with an argument, store the return value into a local variable + """ if '__return__' in self.curframe_locals: - self.message(repr(self.curframe_locals['__return__'])) + if arg: + self.curframe_locals[arg] = self.curframe_locals['__return__'] + else: + self.message(repr(self.curframe_locals['__return__'])) else: self.error('Not yet returned!') do_rv = do_retval diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index 4bb574fc5b7bff..176f5a500c713f 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -106,6 +106,8 @@ def test_pdb_basic_commands(): ... 'jump 8', # jump over second for loop ... 'return', # return out of function ... 'retval', # display return value + ... 'retval v1', # stores the return value in "v1" local variable + ... 'v1', # display the "v1" stored variable ... 'next', # step to test_function3() ... 'step', # stepping into test_function3() ... 'args', # display function args @@ -139,7 +141,7 @@ def test_pdb_basic_commands(): [EOF] (Pdb) bt ... - (25)() + (27)() -> test_function() (3)test_function() -> ret = test_function_2('baz') @@ -184,6 +186,9 @@ def test_pdb_basic_commands(): -> return foo.upper() (Pdb) retval 'BAZ' + (Pdb) retval v1 + (Pdb) v1 + 'BAZ' (Pdb) next > (4)test_function() -> test_function3(kwonly=True) diff --git a/Misc/NEWS.d/next/Library/2020-12-01-16-17-13.bpo-42524.lHDAP6.rst b/Misc/NEWS.d/next/Library/2020-12-01-16-17-13.bpo-42524.lHDAP6.rst new file mode 100644 index 00000000000000..574e6a2841471b --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-12-01-16-17-13.bpo-42524.lHDAP6.rst @@ -0,0 +1 @@ +Add `retval [variable]` option to pdb