VisualStudio代码中的条件断点

2024-04-23 06:27:08 发布

您现在位置:Python中文网/ 问答频道 /正文

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

main.py

from module import f1
f1()

模块.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

f1f2中的第一个print语句上

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中的断点会触发,而条件不满足


Tags: pystackmainextractfilenameuserstmpf2
1条回答
网友
1楼 · 发布于 2024-04-23 06:27:08

在教程debug in vscode中,它说:

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

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

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

enter image description here

我还将表达式值更改为False,并且f2()中的断点也被触发,因此vscode中的条件断点可能存在问题,导致这种现象

相关问题 更多 >