Laanan
Laanan

Reputation: 9

Understanding pdb output after pdb.set_trace()

Trying to debug my python based roguelike with pdb. What I am trying to figure out is, is the function handle_keys() returning 0 like I am asking. I have another function that is not behaving as expected, and so I am trying to figure out where the problem is happening. What should happen is: handle_keys() returns 0, then frag_grenade() does something.

I inserted a traceback for pdb, but I am not sure if that will show me if handle_keys() is returning 0 or not:

elif key.vk == libtcod.KEY_BACKSPACE:
        game_state = 'playing'
        pdb.set_trace()
        return 0

When I hit backspace in the game, I get this output from pdb:

--> return 0

I am not sure if this is showing the return value or just showing the next line of code....

Thank you very much!

Upvotes: 1

Views: 464

Answers (1)

ivan_pozdeev
ivan_pozdeev

Reputation: 36028

26.2. pdb — The Python Debugger — Python 2.7.15 documentation:

The typical usage to break into the debugger from a running program is to insert

import pdb; pdb.set_trace()

at the location you want to break into the debugger. You can then step through the code following this statement, and continue running without the debugger using the c command.

The arrow is pointing to the current line, that is about to be executed.

A return value is printed completely differently:

In [11]: def answer():
    ...:     return 42

In [13]: pdb.runeval("answer()")
> <string>(1)<module>()->None
(Pdb) s
--Call--
> <ipython-input-11-22e067ec9c24>(1)answer()
-> def answer():
(Pdb) n
> <ipython-input-11-22e067ec9c24>(2)answer()
-> return 42
(Pdb)
--Return--
> <ipython-input-11-22e067ec9c24>(2)answer()->42
-> return 42
(Pdb)

As you can see, the returning line is printed twice -- first before being executed, then as the function returns. The second time, it's accompanied by --Return-- and the returned value in the location line.

Upvotes: 1

Related Questions