简体   繁体   中英

Conditional breakpoints in Visual Studio Code

I have following two python files in the same directory:

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)

I started VSCode in the directory and set conditional breakpoints with following expression:

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

on the first print statement in f1 and f2 .

The running of main.py printed out:

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

Both breakpoints are triggerred.

Why the breakpoint in f2 get triggerred whereas the condition is not met?

From tutorial debug in vscode , it says:

Expression condition: The breakpoint will be hit whenever the expression evaluates to true.

The value of the expression decides if the code will be paused or skip during debugging at current breakpoint and has no impact on the code output.

As to your question, you thought the value of expression was false at print("f2") , but the fact was breakpoint mechanism treat it as true and so breakpoint was triggered:

在此处输入图像描述

I also change the expression value to False and the breakpoint in f2() was also triggered, So there may be an issue with conditional breakpoint in vscode that casues this phenomenon.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM