[Python-ideas] Enhance exceptions by attaching some more information to them
Sebastian Kreft
skreft at gmail.com
Sat Feb 15 16:40:34 CET 2014
More than once I've been in a situation where I wish that some of the
stdlib exceptions had a better message or some more information to help me
diagnose the problem.
For example:
a = [1, 2, 3, 4, 5]
a[5]
IndexError: list index out of range
In this case there's no reference to neither the length of the array nor to
the offending index.
I'm of the idea that we could extend the exceptions by adding some more
information to them, so 3rd party libraries could use them for
debugging/logging.
For example, one such use case would be to have a modified test runner,
that in case of exceptions automatically prints some debug information.
Another would be a logger system that in case of an exception also logs
some debug info that could be relevant to understand and solve the issue.
I propose extending (at least) the following exceptions with the following
attributes:
KeyError: key, object
IndexError: index, object
AttributeError: attribute, object
NameError: name
Of course that populating these attributes could be controlled by a flag.
I know that some of this information is already present in some exceptions,
depending on the context. However, I propose adding these attributes, as in
this way a tool could easily and reliably extract the information and work
with it, as opposed to have to parse the huge variety of different messages
there are.
For the first use case mentioned above I have a working prototype, although
it does not use this proposal, but a series of hacks (I'm modifying the
bytecode to save a reference to the key and object :() and parsing of the
exception messages. But I want to share what the output of such a tool
could be.
======================================================================
ERROR: test_attribute (example.ExampleTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/skreft/test/debug_exception/example.py.py", line 18, in
test_attribute
AttributeError: 'str' object has no attribute 'Lower'. Did you mean
'islower', 'lower'?
Debug info:
Object: ''
Type: <type 'str'>
======================================================================
ERROR: test_index (example.ExampleTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/skreft/test/debug_exception/example.py.py", line 6, in
test_index
IndexError: list index out of range
Debug info:
Object: [1, 2]
Object len: 2
Index: 2
======================================================================
ERROR: test_key (example.ExampleTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/skreft/test/debug_exception/example.py.py", line 10, in
test_key
KeyError_: 'fooo', did you mean 'foo'?
Debug info:
Object: {'foo': 1}
Key: 'fooo'
======================================================================
ERROR: test_name (example.ExampleTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/skreft/test/debug_exception/example.py.py", line 14, in
test_name
NameError: global name 'fooo' is not defined. Did you mean 'foo'?
----------------------------------------------------------------------
Ran 4 tests in 0.005s
--
Sebastian Kreft
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140215/4a42b4df/attachment-0001.html>
More information about the Python-ideas
mailing list