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, objectIndexError: index, objectAttributeError: attribute, objectNameError: 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_attributeAttributeError: '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_indexIndexError: list index out of rangeDebug info:Object: [1, 2]Object len: 2Index: 2
======================================================================ERROR: test_key (example.ExampleTest)----------------------------------------------------------------------Traceback (most recent call last):File "/home/skreft/test/debug_exception/example.py.py", line 10, in test_keyKeyError_: '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_nameNameError: global name 'fooo' is not defined. Did you mean 'foo'?
----------------------------------------------------------------------Ran 4 tests in 0.005s
--
Sebastian Kreft
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
No virus found in this message.
Checked by AVG - www.avg.com
Version: 2012.0.2247 / Virus Database: 3705/6597 - Release Date: 02/16/14