Skip to content

I would like to hook traceback printing to use better_exceptions #10422

Open

Description

@Omnifarious

The better-exceptions package makes very pretty tracebacks with a lot of useful information in them. It would be nice if ipython could be hooked so that it used that package to print out exception tracebacks.

It doesn't look like the set_custom_exc is quite the right thing, partly because it expects that you will want to only handle a small subset of exception types. Also, it has a tb_offset argument that isn't terribly well documented.

Activity

  1. takluyver commented on Mar 31, 2017

    @takluyver
    Member

    I agree that it would be good to have an official hook for this, but if you want to experiment with it in the meantime, try replacing get_ipython()._showtraceback.

  2. Omnifarious commented on Mar 31, 2017

    @Omnifarious
    Author

    That doesn't work because by the time the data gets to _showtraceback, ipython has already eaten the traceback and made strings out of it.

    The better_exceptions package goes through the traceback and does variable lookups and a whole bunch of stuff. So it needs the raw stack data, not the strings that result from processing that data.

  3. takluyver commented on Mar 31, 2017

    @takluyver
    Member

    Right. Maybe override showtraceback?

  4. Omnifarious commented on Mar 31, 2017

    @Omnifarious
    Author

    Yes, that appears to work. Though that function takes a lot of extra arguments that I don't know what mean.

  5. Omnifarious commented on Mar 31, 2017

    @Omnifarious
    Author

    Here's an ipython startup file that does the trick:

    from __future__ import print_function
    import better_exceptions, sys
    
    ip = get_ipython()
    old_show = ip.showtraceback
    
    def exception_thunk(exc_tuple=None, filename=None,
                        tb_offset=None, exception_only=False):
        print("Thunk: %r %r %r %r" % (exc_tuple, filename, tb_offset, exception_only),
              file=sys.stderr)
        notuple = False
        if exc_tuple is None:
            notuple = True
            exc_tuple = sys.exc_info()
        use_better = True
        use_better = use_better and (filename is None)
        use_better = use_better and (tb_offset is None)
        use_better = use_better and (not exception_only)
        use_better = use_better and (not isinstance(exc_tuple[0], SyntaxError))
        if use_better:
            return better_exceptions.excepthook(*exc_tuple)
        else:
            return old_show(None if notuple else exc_tuple,
                            filename, tb_offset, exception_only)
    
    ip.showtraceback = exception_thunk
  6. Carreau commented on Apr 3, 2017

    @Carreau
    Member

    @Omnifarious if you are motivated the exception handling could need some love, we already have multiple mode (using xmode). If you want to make it more pluggable to allow better switching that would be great. There is also a long standing issue (somewhere), to make tracebacks more structured so that we could provide richer representations in particular in the notebook.

  7. modified the milestone: wishlist on Apr 3, 2017
  8. Omnifarious commented on Apr 11, 2017

    @Omnifarious
    Author

    @Carreau - I don't think that I'm motivated enough to do that at this time. :-)

    But, I realized that the above snippet was sub-optimal, and I wanted to add a better version, and per your suggestion I've put it in a gist:

    https://gist.github.com/Omnifarious/bfe19f07fd21e582591ee632ddca47ee

  9. carlsmith commented on Apr 12, 2017

    @carlsmith
    Contributor

    Just for future reference, it's generally better to put scripts (really, anything more than example code) on GitHub Gist, then just link to the gist here. It's makes the code easier to maintain and discover, and helps to keep the comments in these conversations short and tidy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions