[Python-ideas] Enhance exceptions by attaching some more information to them

Sebastian Kreft skreft at gmail.com
Sat Mar 8 17:57:45 CET 2014


Thanks for the comments, I will start working on a PEP for Step 1.


On Mon, Feb 17, 2014 at 2:38 PM, Rob Cliffe <rob.cliffe at btinternet.com>wrote:

>  +1 on the idea (I can't comment on the implementation).  One of Python's
> great strengths is its excellent error messages.  But if they can be made
> still better, that would be fantastic.
> Rob Cliffe
>
>
> On 15/02/2014 15:40, Sebastian Kreft wrote:
>
> 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
>
>
> _______________________________________________
> Python-ideas mailing listPython-ideas at python.orghttps://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
>
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>



-- 
Sebastian Kreft
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140308/ab1431dd/attachment-0001.html>


More information about the Python-ideas mailing list