Ask Your Question

How to recreate traceback?

asked 2019-05-17 14:58:18 -0500

dsejas gravatar image

Hello, Sage Community.

When we write something like sin(t) without previously defining the variable t, we get a traceback like

NameError                                 Traceback (most recent call last)
<ipython-input-138-17829414d33a> in <module>()
----> 1 sin(t)

NameError: name 't' is not defined

I would like to reproduce this text automatically. I have tried the following:

    _typ_, _val_, _tb_ = sys.exc_info()
    traceback.print_exception(_typ_, _val_, _tb_)

However, this produces a less rich output:

Traceback (most recent call last):
   File "<ipython-input-139-55b9ce98509c>", line 2, in <module>
   NameError: name 't' is not defined

How could I reproduce the rich output?

Thanks in advance for your answers!

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2019-05-18 02:28:57 -0500

nbruin gravatar image

You should probably look at IPython.core.ultratb . It looks like that's the part of IPython that produces the colourful tracebacks.

edit flag offensive delete link more


Thank you very much, @nbruin! Indeed, IPython.core.ultratb was the correct way to proceed. I have also found sage.interacts.debugger, which has similar functionality, but is a little restricted in comparison.

dsejas gravatar imagedsejas ( 2019-05-20 11:59:40 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2019-05-17 14:58:18 -0500

Seen: 42 times

Last updated: May 18