修改 Python 以在交互模式下接受 continue 语句是个好主意吗?

发布于 2024-10-09 23:30:40 字数 385 浏览 0 评论 0原文

作为使用 pdb 的替代方法,我可以在交互模式下使用 Python continue 语句,在使用 python -i 进行脚本调用期间在 control-C 之后。这样,在脚本中出现 raw_input('continue->') 提示时,我就可以中断、检查/修改内容,然后直接返回到 raw_input 使用继续命令提示(或任何导致异常的代码)。循环之外的 break 命令也可以重新用于对称性,但我对此的使用较少。在为此提交 PEP 之前,我希望获得 Python 社区的一些反馈。

仅使用 PYTHONSTARTUP 脚本和 inspect 模块就可以做类似的事情,但如果是这样,我还没有弄清楚。

as an alternative to using pdb, I would have a use for the Python continue statement in interactive mode, after control-C during a script invocation with python -i. that way, say at a raw_input('continue->') prompt in my script, I could break out, inspect/modify things, and go right back to the raw_input prompt (or whatever code caused an exception) with a continue command. the break command outside of a loop could also be repurposed for symmetry, but I'd have less use for that. before submitting a PEP for this, I'd like some feedback from the Python community.

it might be possible to do something similar just using a PYTHONSTARTUP script and the inspect module, but if so I haven't figured it out yet.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

卷耳 2024-10-16 23:30:40

ctrl-C 在脚本中引发了 KeyboardInterrupt 异常。由于您没有捕获该异常,因此程序终止。只有这样才会出现交互式提示。

您无法继续,因为您的程序已经结束。您按下 Ctrl-C 的事实只是引发了一个异常,程序并没有在那个确切的位置暂停。它继续执行,直到最后一行,然后完成。

没有办法知道你想继续到哪里。为此,您需要一个真正的调试器。

ctrl-C raised a KeyboardInterrupt exception in your script. Since you didn't catch that exception, the program terminated. Only then the interactive prompt appears.

You can't continue because your program is already over. The fact that you pressed Ctrl-C just raised an exception, the program didn't pause at that exact place. It continued execution, up to the last line, and finished.

There's no way to know where you want to continue to. For that you need a real debugger.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文