Visual Studio代码中的条件断点

问题描述

我在同一目录中有以下两个python文件:

main.py

from module import f1
f1()

module.py

import traceback

def f1():
    print('f1')
    print(traceback.extract_stack()[-1].filename)
    print(traceback.extract_stack()[-2].filename)
    f2()

def f2():
    print('f2')
    print(traceback.extract_stack()[-1].filename)
    print(traceback.extract_stack()[-2].filename)

我在目录中启动VSCode并使用以下表达式设置条件断点:

traceback.extract_stack()[-2].filename != traceback.extract_stack()[-1].filename

printf1中的第一个f2语句上。

main.py的运行打印出来:

f1
c:\Users\...\tmp\pythonTest-breakpoint\module.py
c:\Users\...\tmp\pythonTest-breakpoint\main.py
f2
c:\Users\...\tmp\pythonTest-breakpoint\module.py
c:\Users\...\tmp\pythonTest-breakpoint\module.py

两个断点都被触发。

为什么在不满足条件的情况下触发f2中的断点?

解决方法

在教程debug in vscode中,它说:

表达条件:只要表达式计算为 true,就会命中断点。

表达式的值决定在当前断点调试期间是暂停还是跳过代码,而对代码输出没有影响。

关于您的问题,您认为在print("f2")处expression的值是假的,但事实是断点机制将其视为true,因此触发了断点:

enter image description here

我还将表达式值更改为 False ,并且还触发了f2()中的断点,因此vscode中的条件断点可能会引起这种情况。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...