当肯定存在调用堆栈时,为什么traceback.extract_stack()会返回[]?

发布于 2024-07-30 03:20:17 字数 509 浏览 4 评论 0原文

我有一个调用其 __init__() 的类

traceback.extract_stack()

,但每当我这样做时,traceback.extract_stack() 的值都是 []

造成这种情况的原因有哪些? 是否有另一种方法来获得更可靠的回溯?

我认为问题在于代码是在 Pylons 中运行的。 这是控制器操作的一些代码:

def test_tb(self):
    import traceback
    return a.lib.htmlencode(traceback.extract_stack())

它生成一个网页,所以

[] 

,我认为它与对象的构造函数或类似的东西没有任何关系。 它是否与某些类型的线程和回溯模块或类似的东西之间的不兼容有关?

I have a class that calls

traceback.extract_stack()

in its __init__(), but whenever I do that, the value of traceback.extract_stack() is [].

What are some reasons that this could be the case?
Is there another way to get a traceback that will be more reliable?

I think the problem is that the code is running in Pylons. Here is some code for a controller action:

def test_tb(self):
    import traceback
    return a.lib.htmlencode(traceback.extract_stack())

It generates a webpage that is just

[] 

So, I don't think it has anything to do with being in the constructor of an object or anything like that. Could it have to do with an incompatibility between some kinds of threading and the traceback module or something like that?

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

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

发布评论

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

评论(3

嗼ふ静 2024-08-06 03:20:17

下面显示了从类的 __init__ 方法调用时traceback.extract_stack() 的工作情况。 请发布您的代码,表明它不起作用。 包括 Python 版本。 不要凭记忆打字; 像我一样使用复制/粘贴。

Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import traceback as tb
>>> tb.extract_stack()
[('<stdin>', 1, '<module>', None)]
>>> def func():
...     print tb.extract_stack()
...
>>> func()
[('<stdin>', 1, '<module>', None), ('<stdin>', 2, 'func', None)]
>>> class Klass(object):
...     def __init__(self):
...         print tb.extract_stack()
...
>>> k = Klass()
[('<stdin>', 1, '<module>', None), ('<stdin>', 3, '__init__', None)]
>>>

更新不要查看return a.lib.htmlencode(traceback.extract_stack())并想知道,而是进入管道:

(1) do tb_stack = repr ((traceback.extract_stack()) 并将结果写入日志文件以进行检查

(2) 执行 return a.lib.htmlencode(some_known_constant_data) 并检查已知数据是否正确显示您期望它出现的地方。

Following shows traceback.extract_stack() working when called from a class's __init__ method. Please post your code showing that it doesn't work. Include the Python version. Don't type from memory; use copy/paste as I have done.

Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import traceback as tb
>>> tb.extract_stack()
[('<stdin>', 1, '<module>', None)]
>>> def func():
...     print tb.extract_stack()
...
>>> func()
[('<stdin>', 1, '<module>', None), ('<stdin>', 2, 'func', None)]
>>> class Klass(object):
...     def __init__(self):
...         print tb.extract_stack()
...
>>> k = Klass()
[('<stdin>', 1, '<module>', None), ('<stdin>', 3, '__init__', None)]
>>>

UPDATE Instead of looking at return a.lib.htmlencode(traceback.extract_stack()) and wondering, tap into the pipeline:

(1) do tb_stack = repr((traceback.extract_stack()) and write the result to your logfile for checking

(2) do return a.lib.htmlencode(some_known_constant_data) and check that the known data shows up correctly where you expect it to show up.

如歌彻婉言 2024-08-06 03:20:17

查看回溯模块的代码,一种可能性是您已将 sys.tracebacklimit 设置为零,尽管这看起来不太可能......

Looking at the code for the traceback module, one possibility is that you've got sys.tracebacklimit set to zero, though that seems like a longshot...

離人涙 2024-08-06 03:20:17

原因原来是有人在项目上打开了 Pysco,而 Psyco 与回溯模块配合得不好。

The reason turned out to be that someone turned on Pysco on the project, and Psyco doesn't play nice with the traceback module.

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