How to continue the next loop iteration in Python PDB?

Given this code example:

import pdb for i in range(10): pdb.set_trace() print(str(i)) 

When I receive an invitation from the PDB, how can I skip the loop iteration using the continue loop control statement, when it is also used by the PDB to continue executing the code?

+6
source share
2 answers

It sounds strange what you want to do. However, you must use the jump command . You probably need to add a pass statement at the end of the for loop so you can jump to the end of the loop. If you are not sure about the line numbers of your code, you can use ll to find out the line numbers of your loop.

 > c:\run.py(5)<module>() -> print(i) (Pdb) ll 1 import pdb 2 3 for i in range(10): 4 pdb.set_trace() 5 -> print(i) 6 pass (Pdb) j 6 > c:\run.py(6)<module>() -> pass (Pdb) c > c:\python\run.py(4)<module>() -> pdb.set_trace() (Pdb) c 1 > c:\python\run.py(5)<module>() -> print(i) 

It is worth noting that going to the for line will restart the loop.

+7
source

You cannot use continue because the new statements in the debugger must be complete and valid without any other context; continue must be specified inside the compilation loop structure. Thus, using !continue (c ! To prevent pdb from interpreting the command) cannot be used even if the debugger is processing the loop construct.

You can use the j[ump] if you have a statement later to jump. If your cycle is empty after the statements you wanted to jump over, you can only "rewind":

 $ bin/python test.py > /.../test.py(5)<module>() -> print(str(i)) (Pdb) l 1 import pdb 2 3 for i in range(10): 4 pdb.set_trace() 5 -> print(str(i)) 6 [EOF] (Pdb) j 3 > /.../test.py(3)<module>() -> for i in range(10): 

j 3 jumped to line 3 without missing anything; line 3 will be overwritten, including the range() setting. You can go to line 4, but then the for loop does not advance.

You need to add another statement at the end of the loop in order to move on to using Python. This statement can be print() or pass or something else that should not change your state. You can even use continue as the last statement. I used i :

 for i in range(10): pdb.set_trace() print(str(i)) i # only here to jump to. 

Demo:

 $ bin/python test.py > /.../test.py(5)<module>() -> print(str(i)) (Pdb) l 1 import pdb 2 3 for i in range(10): 4 pdb.set_trace() 5 -> print(str(i)) 6 i # only here to jump to. 7 [EOF] (Pdb) j 6 > /.../test.py(6)<module>() -> i # only here to jump to. (Pdb) c > /.../test.py(4)<module>() -> pdb.set_trace() (Pdb) s > /.../test.py(5)<module>() -> print(str(i)) (Pdb) j 6 > /.../test.py(6)<module>() -> i # only here to jump to. (Pdb) i 1 (Pdb) c > /.../test.py(4)<module>() -> pdb.set_trace() (Pdb) s > /.../test.py(5)<module>() -> print(str(i)) (Pdb) j 6 > /.../test.py(6)<module>() -> i # only here to jump to. (Pdb) i 2 

From the Debugger Command :

j (ump) lineno
Set the next line to be executed. Available only in the bottom frame. This allows you to go back again and execute code or go forward to skip code that you do not want to run.

It should be noted that not all jumps are valid - for example, it is impossible to jump to the middle of a for loop or from a finally clause.

+8
source

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


All Articles