10

I have a big legacy Python method which contains roughly twenty return statements.

The method should not return None but it does so. It is repeatable in a simple test case.

Up to now I used a debugger and stepped through the code line by line to find the matching return statement.

But is there an easier way?

Is there a way to raise an Exception as soon as the method returns None?

and Of course I need to see the line containing the return statement.

Example:

def big_method(arg1, some_var):
    #.... many returns
    if arg1:
        return some_var # <------
    #... many returns


assert not big_method(True, None) is None

Above is a simple code snippet. The result:

Traceback (most recent call last):
  File "/home/modwork_vums_d/src/manyreturns.py", line 8, in <module>
    assert not big_method(True, None) is None
AssertionError

Above traceback does not help very much, since I want to see the line inside big_method(). In the example above I want to see which I marked with <------.

I use PyCharm, but a pure python or other solution is welcome.

Just for the records. There is a follow-up question which tries to enable this feature in PyCharm: PyCharm: Debugging: r(eturn) Continue execution until the current function returns

guettli
  • 25,042
  • 81
  • 346
  • 663
  • There is a possible solution: https://stackoverflow.com/a/1156048/5588279. – Sraw Dec 13 '17 at 09:53
  • I wonder this would be possible, the frame to execute big_method has already get reclaimed. – georgexsh Dec 13 '17 at 20:47
  • maybe you could leverage on interpreter's trace function, to record the execution path of the function being called, then print out when outer assertion failed. – georgexsh Dec 13 '17 at 20:50
  • but as you are using a debugger, I guess it should have a functionality like "run until the current function returns", which will stop at the line you need. – georgexsh Dec 13 '17 at 20:54
  • @georgexsh I use PyCharm, but I don't know all features of it. I only now a way to run until big_method() finished. But then the debug-cursor is outside big_method() and I don't see where the interpreter was before. Maybe I am blind, I see the return value, but I don't see which return statement was used. – guettli Dec 14 '17 at 07:55
  • `pdb` has this feature, could you give it a try? – georgexsh Dec 14 '17 at 08:20
  • @georgexsh pdb can do it? Nice. Can you please give me a matching term, I would like to read the docs. – guettli Dec 14 '17 at 08:25
  • 1
    @guettli [link of pdb `r` command doc](https://docs.python.org/3/library/pdb.html#pdbcommand-return) – georgexsh Dec 14 '17 at 08:26

1 Answers1

12

pdb has a r(eturn) command for this need:

r(eturn) Continue execution until the current function returns.

example:

> /Users/georgexsh/wasteland/tmp/app.py(6)<module>()
-> assert not big_method(True, None) is None
(Pdb) s
--Call--
> /Users/georgexsh/wasteland/tmp/app.py(1)big_method()
-> def big_method(arg1, some_var):
(Pdb) r
--Return--
> /Users/georgexsh/wasteland/tmp/app.py(3)big_method()->None
-> return some_var

see more detail in pdb doc.

georgexsh
  • 15,984
  • 2
  • 37
  • 62