Ask Your Question
0

How to recreate traceback?

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

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:

try:
    sin(t)
except:
    _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>
      sin(t)
   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
2

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

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

Comments

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 -0600 )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

Stats

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

Seen: 49 times

Last updated: May 18