在python中获取pdb风格的调用者信息

发布于 2024-07-19 08:05:20 字数 279 浏览 2 评论 0原文

假设我有以下方法(在类或模块中,我认为这并不重要):

def someMethod():
    pass

我想在调用此方法时访问调用者的状态。

traceback.extract_stack 只是给我一些有关调用堆栈的字符串。

我想要类似 pdb 的东西,在其中我可以在 someMethod() 中设置断点,然后输入“u”进入调用堆栈,然后检查那么系统。

Let's say I have the following method (in a class or a module, I don't think it matters):

def someMethod():
    pass

I'd like to access the caller's state at the time this method is called.

traceback.extract_stack just gives me some strings about the call stack.

I'd like something like pdb in which I can set a breakpoint in someMethod() and then type 'u' to go up the call stack and then examine the state of the system then.

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

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

发布评论

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

评论(1

ヅ她的身影、若隐若现 2024-07-26 08:05:20

我想到了:

import inspect

def callMe():
    tag = ''
    frame = inspect.currentframe()
    try:
        tag = frame.f_back.f_locals['self']._tag
    finally:
        del frame

    return tag

I figured it out:

import inspect

def callMe():
    tag = ''
    frame = inspect.currentframe()
    try:
        tag = frame.f_back.f_locals['self']._tag
    finally:
        del frame

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