Enhanced information from common exceptions

Tim Peters tim.one at comcast.net
Sat Mar 16 20:40:08 EST 2002


[John Machin]
> An example:
>
> >>> alist = [4,3,2,1]
> >>> alist[42]
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> IndexError: list index out of range
> >>>
>
> Suppose this was actually in a script somewhere. IMO, it would be much
> more useful for Python developers of all levels were the message to be
> something like:
>
> IndexError: list index 42 not in range(4)
>
> Often this extra info would lead to problem recognition & resolution
> without the need for an extra step (print statement or debugger).

A difficulty with this kind of thing is that producing exception info
doesn't come for free.  Before 2.2, raising IndexError was the only way to
terminate a "for" loop via normal exhaustion, and the list indexing code has
no idea *why* it's being called.  If it produced more info on an
out-of-range index, it would slow down every normal loop by the time it took
to construct a nice exception message doomed to be thrown away unlooked-at.
Note that the integers in your ideal message have to go through binary ->
decimal string conversion, and lots of dynamic string fiddling to paste all
the msg pieces together.  The cycles add up fast.

> Perhaps if people cared to nominate their "favourite" high-frequency-
> low-information exception message(s), together with suggested changes,
> we could put together a nanoPEP on this topic -- or simply just propose
> a few patches.

We're *usually* happy to apply such patches, but get more cautious over
time.  Last year Jim Fulton and I traced a major slowdown in Zope to a "nice
msg" patch that spent lots of time formatting exception info in a context
where Zope routinely provoked that exception in a try/except that ignored
the exception.

So these things can be trickier to get right than you may think.  A good
approach to IndexError would be to create an "exception object" at the C
level that merely stored the offending index and max size, as C longs, and
didn't produce a string until and unless it was actually needed for display.
Unfortunately, that raises the amount of knowledge needed to write such a
patch.





More information about the Python-list mailing list