Ask Your Question
0

How to recreate traceback?

asked 5 years ago

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!

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
2

answered 5 years ago

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.

Preview: (hide)
link

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 ( 5 years ago )

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: 5 years ago

Seen: 677 times

Last updated: May 18 '19