Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error tracebacks so that it shows the cell prompts #13043

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

@martinRenou
Copy link
Contributor

@martinRenou martinRenou commented Jul 2, 2021

This PR improves the error traceback so that it shows the prompt In [x] in front of code coming from cells in Jupyter Notebook and IPython, instead of the filename hash (used for compilation).

The result is the following:

traceback

Versus the old version:

vs

@martinRenou martinRenou force-pushed the martinRenou:improve_traceback branch from 580b323 to cbca2da Jul 2, 2021
@martinRenou martinRenou force-pushed the martinRenou:improve_traceback branch from cbca2da to fe890c7 Jul 5, 2021
@Carreau Carreau added this to the 8.0 milestone Jul 6, 2021
@Carreau
Copy link
Member

@Carreau Carreau commented Jul 6, 2021

Seem reasonable to me.

@MrMino MrMino self-requested a review Jul 6, 2021
# Caching a dictionary { filename: execution_count } for nicely
# rendered tracebacks
self.filename_map = {}

This comment has been minimized.

@MrMino

MrMino Jul 6, 2021
Collaborator

These filenames are the result of the code_name function above. They already contain the number (AKA the execution_count in your comment).

You could get rid of this dictionary altogether, by creating a code_name_to_cell_id func (next to code_name), that would parse out the number out of the:

    return '<ipython-input-{0}-{1}>'.format(number, hash_digest[:12])

Then add a test for the code_name_to_cell_id(code_name(code, number)) == number assumption.
That way you could omit the get_ipython call, and therefore make it all a bit less coupled.

Copy link
Collaborator

@MrMino MrMino left a comment

Apart from the one suggestion above - looks cool.

@@ -92,6 +92,9 @@ def __init__(self):
# (otherwise we'd lose our tracebacks).
linecache.checkcache = check_linecache_ipython

# Caching a dictionary { filename: execution_count } for nicely
# rendered tracebacks
self.filename_map = {}

This comment has been minimized.

@MrMino

MrMino Jul 12, 2021
Collaborator

Let's just take steps to ensure this dict won't get used somewhere else. Also, filename_map is a bit misleading here - these aren't filenames after all ;).

Suggested change
self.filename_map = {}
self._co_filename_map = {}

This comment has been minimized.

@martinRenou

martinRenou Jul 12, 2021
Author Contributor

Filename here refers to the filename argument passed to the compile function used for AST compilation.

I agree, let's make it more private.

This comment has been minimized.

@MrMino

MrMino Jul 12, 2021
Collaborator

If I saw this after a year or two, it would be easier to connect the dots if it told me that it's related to co_filename, but it's probably just me having done one too many frame hacks in python.

The co_filename is the name of the attribute in the code objects that holds this info.

Up to you :).

@MrMino
Copy link
Collaborator

@MrMino MrMino commented Jul 12, 2021

Oh - one more thing - it would be nice to have a short what's new entry for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

3 participants