Improve error tracebacks so that it shows the cell prompts #13043
Conversation
580b323
to
cbca2da
cbca2da
to
fe890c7
|
Seem reasonable to me. |
| # Caching a dictionary { filename: execution_count } for nicely | ||
| # rendered tracebacks | ||
| self.filename_map = {} |
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.
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.
|
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 = {} | |||
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 = {}
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 ;).
| self.filename_map = {} | |
| self._co_filename_map = {} |
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.
Filename here refers to the filename argument passed to the compile function used for AST compilation.
I agree, let's make it more private.
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 :).
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 :).
|
Oh - one more thing - it would be nice to have a short what's new entry for this. |
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:
Versus the old version: