Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debug where method returns None

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

like image 539
guettli Avatar asked Dec 13 '17 09:12

guettli


1 Answers

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.

like image 70
georgexsh Avatar answered Sep 20 '22 08:09

georgexsh