<div dir="ltr">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.<div><br></div><div>For example:</div>
<div>a = [1, 2, 3, 4, 5]<br></div><div><div>a[5]<br></div><div>IndexError: list index out of range<br></div><div><br></div><div>In this case there's no reference to neither the length of the array nor to the offending index.</div>
<div><br></div><div>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.</div><div><br></div><div>For example, one such use case would be to have a modified test runner, that in case of exceptions automatically prints some debug information.</div>
<div>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. </div><div><br></div><div>I propose extending (at least) the following exceptions with the following attributes:</div>
<div>KeyError: key, object</div><div>IndexError: index, object</div><div>AttributeError: attribute, object</div><div>NameError: name</div><div><br></div><div>Of course that populating these attributes could be controlled by a flag.</div>
<div><br></div><div>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. </div>
<div><br></div><div>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.</div>
<div><br></div><div><div>======================================================================</div><div>ERROR: test_attribute (example.ExampleTest)</div><div>----------------------------------------------------------------------</div>
<div>Traceback (most recent call last):</div><div> File "/home/skreft/test/debug_exception/<a href="http://example.py.py">example.py.py</a>", line 18, in test_attribute</div><div>AttributeError: 'str' object has no attribute 'Lower'. Did you mean 'islower', 'lower'?</div>
<div>Debug info:</div><div> Object: ''</div><div> Type: <type 'str'></div><div><br></div><div>======================================================================</div><div>ERROR: test_index (example.ExampleTest)</div>
<div>----------------------------------------------------------------------</div><div>Traceback (most recent call last):</div><div> File "/home/skreft/test/debug_exception/<a href="http://example.py.py">example.py.py</a>", line 6, in test_index</div>
<div>IndexError: list index out of range</div><div>Debug info:</div><div> Object: [1, 2]</div><div> Object len: 2</div><div> Index: 2</div><div><br></div><div>======================================================================</div>
<div>ERROR: test_key (example.ExampleTest)</div><div>----------------------------------------------------------------------</div><div>Traceback (most recent call last):</div><div> File "/home/skreft/test/debug_exception/<a href="http://example.py.py">example.py.py</a>", line 10, in test_key</div>
<div>KeyError_: 'fooo', did you mean 'foo'?</div><div>Debug info:</div><div> Object: {'foo': 1}</div><div> Key: 'fooo'</div><div><br></div><div>======================================================================</div>
<div>ERROR: test_name (example.ExampleTest)</div><div>----------------------------------------------------------------------</div><div>Traceback (most recent call last):</div><div> File "/home/skreft/test/debug_exception/<a href="http://example.py.py">example.py.py</a>", line 14, in test_name</div>
<div>NameError: global name 'fooo' is not defined. Did you mean 'foo'?</div><div><br></div><div>----------------------------------------------------------------------</div><div>Ran 4 tests in 0.005s</div>
</div>
<div><br></div><div>-- <br>Sebastian Kreft
</div></div></div>