如果语句未命中,则继续断点 [英] Breakpoint on continue in if statement not hit
问题描述
在下面的代码中,a
和 b
都是生成器函数的输出,可以计算为 None
或有一个值.
In the following code, both a
and b
are outputs of generator functions, and could evaluate to None
or have a value.
def testBehaviour(self):
a = None
b = 5
while True:
if not a or not b:
continue
print('blaat')
如果我在带有 continue
语句的行和带有 print
语句的行上放置断点(使用 Visual Studio Code),则两者都不会命中.print
语句没有被调用,循环只是按预期无限期地运行,但我希望断点被命中.
If I put breakpoints (using Visual Studio Code) on the line with the continue
statement, and the line with the print
statement, neither is hit. The print
statement is not called, and the loop just keeps running indefinitely as expected, but I would expect that breakpoints are hit.
如果我将代码更改为以下之一:
If I change the code to one of the following:
def testBehaviour(self):
a = None
b = 5
while True:
if not a:
continue
print('blaat')
或:
def testBehaviour(self):
a = None
b = 5
while True:
if not a or not b:
c = 'dummy'
continue
print('blaat')
再次在带有 continue
和 print
语句的行上放置断点,断点被命中.
And again place breakpoints on the lines with the continue
and print
statements, the breakpoints are hit.
谁能告诉我为什么没有命中断点?这似乎不仅仅发生在 Visual Studio Code 中,因为我们的代码覆盖工具还指出未调用 continue
语句.
Can anyone tell me why the breakpoints are not hit? It seems to not happen only in Visual Studio Code, because our code-coverage tool also states that the continue
statement was not called.
这是在 32 位 Windows 7 上的 python 2.7 上.
This is on python 2.7 on Windows 7, 32 bit.
推荐答案
操作码中有优化,编译器识别if expr: continue
.
There is an optimization in the opcodes, the compiler recognizes the if expr: continue
.
当使用if not a or not b:
11 12 SETUP_LOOP 35 (to 50)
>> 15 LOAD_GLOBAL 1 (True)
18 POP_JUMP_IF_FALSE 49
12 21 LOAD_FAST 1 (a)
24 UNARY_NOT
25 POP_JUMP_IF_TRUE 15
28 LOAD_FAST 2 (b)
31 UNARY_NOT
32 POP_JUMP_IF_FALSE 41
13 35 JUMP_ABSOLUTE 15
38 JUMP_FORWARD 0 (to 41)
14 >> 41 LOAD_CONST 2 ('blaat')
44 PRINT_ITEM
45 PRINT_NEWLINE
46 JUMP_ABSOLUTE 15
>> 49 POP_BLOCK
>> 50 LOAD_CONST 0 (None)
53 RETURN_VALUE
如果 not a
为真,25 POP_JUMP_IF_TRUE 15
行将跳回到 while 循环的开头(第 15 行).它永远不会到达 continue 语句的行 35 JUMP_ABSOLUTE 15
The line 25 POP_JUMP_IF_TRUE 15
jumps back to the start of the while loop (line 15) if not a
is true. It never reaches the line of the continue statement 35 JUMP_ABSOLUTE 15
如果您切换 a
和 b
的值,此测试用例有效.
If you switch the values of a
and b
this test case works.
如果您将测试表达式重写为 if not ( a 和 b ):
您会得到以下操作码
If you rewrite the test expression to if not ( a and b ):
you get the following opcodes
11 12 SETUP_LOOP 33 (to 48)
>> 15 LOAD_GLOBAL 1 (True)
18 POP_JUMP_IF_FALSE 47
12 21 LOAD_FAST 1 (a)
24 JUMP_IF_FALSE_OR_POP 30
27 LOAD_FAST 2 (b)
>> 30 POP_JUMP_IF_TRUE 39
13 33 JUMP_ABSOLUTE 15
36 JUMP_FORWARD 0 (to 39)
14 >> 39 LOAD_CONST 2 ('blaat')
42 PRINT_ITEM
43 PRINT_NEWLINE
44 JUMP_ABSOLUTE 15
>> 47 POP_BLOCK
>> 48 LOAD_CONST 0 (None)
51 RETURN_VALUE
这总是有效并且速度更快.not
操作优化为跳转测试语句
And that always works and is faster. The not
operations are optimized into the jump test statements
这篇关于如果语句未命中,则继续断点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!