User
User

Reputation: 14873

Custom python traceback or debug output

I have a traceback print and want to customize the last part of it.

Similar to this code:

>>> def f():
    g()

>>> def g():
    raise Exception, Exception(), None ## my traceback here

>>> f()

Traceback (most recent call last):
  File "<pyshell#14>", line 1, in <module>
    f()
  File "<pyshell#8>", line 2, in f
    g()
  File "<pyshell#11>", line 2, in g
    raise Exception, Exception(), None ## my traceback starts here
my traceback appears here
my traceback appears here

Exception

Impossible "Solutions": subclass and mock-object

>>> from types import *
>>> class CostomTB(TracebackType):
    pass


Traceback (most recent call last):
  File "<pyshell#125>", line 1, in <module>
    class CostomTB(TracebackType):
TypeError: Error when calling the metaclass bases
    type 'traceback' is not an acceptable base type
>>> class CostomTB(object):
    pass

>>> try: zzzzzzzzz
except NameError:
    import sys
    ty, err, tb = sys.exc_info()
    raise ty, err, CostomTB()


Traceback (most recent call last):
  File "<pyshell#133>", line 5, in <module>
    raise ty, err, CostomTB()
TypeError: raise: arg 3 must be a traceback or None

I am using python 2.7.

Upvotes: 1

Views: 3201

Answers (2)

Stefano Masini
Stefano Masini

Reputation: 462

You mentioned a separate process: if your problem is to capture the traceback in process A and show it in process B, as if the exception was actually raised in the latter, then I'm afraid there is no clean way to do it.

I would suggest to serialize the traceback in process A, send it to process B and from there raise a new exception that includes the former in its description. The result is a somewhat longer output, but it carries information about both processes stacks.

In the following example there aren't really two separate processes, but I hope it makes my point clearer:

import traceback, StringI

def functionInProcessA():
    raise Exception('Something happened in A')

class RemoteException(Exception):
    def __init__(self, tb):
        Exception.__init__(self, "Remote traceback:\n\n%s" % tb)

def controlProcessB():
    try:
        functionInProcessA()
    except:
        fd = StringIO.StringIO()
        traceback.print_exc(file=fd)
        tb = fd.getvalue()
        raise RemoteException(tb)

if __name__ == '__main__':
    controlProcessB()

Output:

Traceback (most recent call last):
  File "a.py", line 20, in <module>
    controlProcessB()
  File "a.py", line 17, in controlProcessB
    raise RemoteException(tb)
__main__.RemoteException: Remote traceback:

Traceback (most recent call last):
  File "a.py", line 12, in controlProcessB
    functionInProcessA()
  File "a.py", line 4, in functionInProcessA
    raise Exception('Something happened in A')
Exception: Something happened in A

Upvotes: 0

NIlesh Sharma
NIlesh Sharma

Reputation: 5655

I guess you want the full traceback stack. See this which is having very good examples python logging module.
If some confusion comes See the logging documentation.

Upvotes: 1

Related Questions