views:

88

answers:

3

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?

A: 

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...

Ned Batchelder
+1  A: 

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.

John Machin
A: 

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

Josh Gibson