为什么 Python 用 vscode debug, step into 会跳转到意想之外的地方? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
ActualAvocado
V2EX    Python

为什么 Python 用 vscode debug, step into 会跳转到意想之外的地方?

  •  1
     
  •   ActualAvocado 2022-04-17 15:21:03 +08:00 3110 次点击
    这是一个创建于 1328 天前的主题,其中的信息可能已经有所发展或是发生改变。

    源码 python 协程 Futures 样例

    import asyncio async def set_after(fut, delay, value): # Sleep for *delay* seconds. await asyncio.sleep(delay) # Set *value* as a result of *fut* Future. print(fut._state) fut.set_result(value) print(fut._state) async def main(): # Get the current event loop. loop = asyncio.get_running_loop() # Create a new Future object. # 我在这里设置了断点 想看看 future 对象的状态什么时候 哪个代码把他置成 pending 了 fut = loop.create_future() print(fut._state) # Run "set_after()" coroutine in a parallel Task. # We are using the low-level "loop.create_task()" API here because # we already have a reference to the event loop at hand. # Otherwise we could have just used "asyncio.create_task()". loop.create_task( set_after(fut, 1, '... world')) print(fut._state) print('hello ...') # Wait until *fut* has a result (1 second) and print it. print(await fut) asyncio.run(main()) 

    断点如上面源码注释那样。 我已经实现把 vscode launch.json justMyCode 设置成 false 了。 即便如此,我在一步步 step into 的时候。

    # 从上面一步步 step into 下去遇到在 base_events.py 成员函数 # 这个成员函数没有用装饰器 def create_future(self): """Create a Future object attached to the loop.""" return futures.Future(loop=self) # 跳转到同一个文件下的 def get_debug(self): return self._debug # 再次 step into 跳转回来 def create_future(self): """Create a Future object attached to the loop.""" return futures.Future(loop=self) # 再次 step into 回到最开始的断点处 fut = loop.create_future() 

    我是在 windows ,下面用 vscode 调试的,python3.9

    之前也遇到类似的情况,step into 会跳转一个我完全不理解的地方,看不出来是谁调用的。。 请教一下 这个原因是什么呢?还有我怎么样才能看到真正的一步步执行的流程。(我现在还是不知道 future 什么时候被设置了 pending 状态)

    第 1 条附言    2022-04-17 16:50:56 +08:00

    编辑下: 我说的意想不到指的是我在 return futures.Future(loop=self) 这一步 step into 后,我预计会跳转 Future 对象的 init 方法过去,但是没有,跳转到了 get_debug 这个函数上了。后续也没有跳转到 Future 对象的 init 方法 而是直接返回到一开始设置断点的地方 fut = loop.create_future()

    7 条回复    2022-04-17 16:59:39 +08:00
    ActualAvocado
        1
    ActualAvocado  
    OP
       2022-04-17 15:24:29 +08:00
    补充一下第一次跳到 getdebug 函数的时候的调用堆栈
    ActualAvocado
        2
    ActualAvocado  
    OP
       2022-04-17 15:26:36 +08:00
    执行完 getdebug 后的调用堆栈
    u823tg
        3
    u823tg  
       2022-04-17 16:08:44 +08:00
    这不是意想不到地方这是 asyncio 库里。 那几个调试按钮 or 快捷键,你 Google 下理解了就能看到一步步执行的流程
    ActualAvocado
        4
    ActualAvocado  
    OP
       2022-04-17 16:16:04 +08:00
    @u823tg #3 我知道是 asyncio 库,我目的是看里面是怎么把 future 对象设置成 pending 状态。
    抱歉我这里没说清楚,我说的意想不到指的是我在
    return futures.Future(loop=self)
    这一步 step into 后,我预计会跳转 Future 对象的 init 方法过去,但是没有,跳转到了 get_debug 这个函数上了。后续也没有跳转到 Future 对象的 init 方法 而是直接返回到一开始设置断点的地方 fut = loop.create_future()
    u823tg
        5
    u823tg  
       2022-04-17 16:53:15 +08:00
    额,你看看下 future 类就知道了啊。 这个你还调试
    u823tg
        6
    u823tg  
       2022-04-17 16:55:14 +08:00
    再说真要调试也不是在这打断点。 你跟踪的应该是 决定 future 对象状态 的变量
    u823tg
        7
    u823tg  
       2022-04-17 16:59:39 +08:00
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     902 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 24ms UTC 22:58 PVG 06:58 LAX 14:58 JFK 17:58
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86