How can I debug manually entered expressions and expressions in pdb?

In pdb (or ipdb), we can execute instructions and evaluate expressions using or commands : !p

p expression
    Rate the expression in the current context and print its value.

[!] Statement

    Execute a statement (single line) in the context of the current stack frame. An exclamation mark may be omitted if the first statement word is not like a debugger command. To set a global variable, you can prefix the destination command with the global command on the same line

So, for example, I can type p reddit.get_subreddits()during debugging in ipdb, and the code will be executed in the current context, and I will see the return value.

Is there a way to debug the execution of such expressions "manually"?

Basically I would like to do this s reddit.get_subreddits(), but it just executes the command stepand ignores the expression.

EDIT: A Trivial Example

Take this simple function:

import random

def get_value_for_weekday(weekday_index=None):
    values = [10, 20, 20, 10, 30, 30, 30]
    if not weekday_index:
        # If no weekday provided, return the average of all weekdays
        return sum(values) / 7
    return averages[weekday_index]

if __name__ == '__main__':
    while True:
        import ipdb; ipdb.set_trace()  # enter ipbd for debug
        get_value_for_weekday(random.randint(0, 7))

What is being tapped because of if not weekday_index(he needs to check weekday_index is not None.)

Suppose I notice that I am getting 10half the number of times I expect. So I added import ipdb; ipdb.set_trace()before calling the function to try and debug the code.

So, I am in the ipdb console, and I suddenly realized that maybe the problem is that I am missing 0 as weekday_index. I can test my hypothesis directly in ipdb:

ipdb> p get_value_for_weekday(0)
22

, , - , weekday_index=0.
get_value_for_weekday(0), , if.

, ipdb, script, , 0, script ipdb, ipdb step (s).
, s get_value_for_weekday(0) , p get_value_for_weekday(0)?

- ?

+4
2

, (d)ebug, - Debugger Commands. pdb ( , help ). debug:

(Pdb) help debug
debug code
        Enter a recursive debugger that steps through the code
        argument (which is an arbitrary expression or statement to be
        executed in the current environment).

, , , . script :

python -m pdb pdbscript.py

n , ( , pdb). debug get_value_for_weekday(0) :

(Pdb) debug get_value_for_weekday(0)
ENTERING RECURSIVE DEBUGGER
> <string>(1)<module>()
((Pdb)) s
--Call--
> /home/jim/Desktop/pdbscript.py(3)get_value_for_weekday()
-> def get_value_for_weekday(weekday_index=None):
((Pdb)) n
> /home/jim/Desktop/pdbscript.py(4)get_value_for_weekday()
-> values = [10, 20, 20, 10, 30, 30, 30]
((Pdb)) n 
> /home/jim/Desktop/pdbscript.py(5)get_value_for_weekday()
-> if not weekday_index:
((Pdb)) p weekday_index
0
((Pdb)) n
> /home/jim/Desktop/pdbscript.py(7)get_value_for_weekday()
-> return sum(values) / 7

, -, , , , .

+4

, pdb . ( 's') weekday_index = 0 .

:

  • "j # "
  • 's'
  • .

, , , 3 2 - .

+1

Source: https://habr.com/ru/post/1624130/


All Articles