Skip to content

Suggestion: use stack_data for better tracebacks #145

@alexmojaki

Description

@alexmojaki

Hi! I've recently created a few libraries that are helpful for extracting information from frames and tracebacks, and I think they could be very useful to rich.

  • executing can identify the exact AST node being executed by a frame in many cases. It's always disappointed me that Python only points to a line in each frame in tracebacks, especially when the expression/statement at fault spans many lines. Sometimes it can be really hard to interpret. Currently executing is used in IPython (in master, unreleased) to highlight the node, here's what it looks like:

ipython

  • executing can also infer a function __qualname__ from a code object, meaning your traceback can say lorem/ipsum.py in MyClass.__init__ at line 123 instead of just __init__, which is much more informative.
  • pure_eval can safely evaluate certain AST nodes without side effects, so that if the source for a frame contains expressions like self.foo and bar[key] their values can often be shown alongside plain variables.
  • stack_data collects data from tracebacks for the purpose of formatting and displaying them. It uses executing and pure_eval and also provides a lot of functionality of its own. I integratedstack_data into IPython which allowed removing a lot of flaky custom introspection code, fixing several bugs and making the code more maintainable.

For rich, I'm thinking you could use stack_data to:

  • Reduce the amount of code in the repo.
  • More intelligently select which lines of code to display, instead of just a fixed number lines before and after.
  • Display the values of local variables and other expressions for each frame in a nice table.
  • Highlight the node being executed.
  • Show the code qualname.
  • Handle repeated frames, especially when the user gets a RecursionError: maximum recursion depth exceeded. Right now it gets stuck for a while before producing a massive traceback.

What do you think? If you want to know more I suggest looking through the stack_data README to get an idea of what it can do and how to use it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions