[issue5353] Improve IndexError messages with actual values

Terry J. Reedy report at bugs.python.org
Mon Feb 23 19:38:32 CET 2009


New submission from Terry J. Reedy <tjreedy at udel.edu>:

Currently: >>> l=[]; l[1] # 3.0.1
...
IndexError: list index out of range

Assuming that negative indexes are converted before the range test is
made, I would like to see
"IndexError: list index 1 not in range(0)", ie,
"IndexError: list index {0} not in range({1})".format(<index>,
len(<sequence>)).

The 'in' operator still works with Py3 range objects, so the suggested
Python-level check is still valid.

I did not add 2.6/3.0 only because I don't know if improved error
messages are acceptable in bug-fix releases.

Same request for other sequence IndexError messages:
>>> t=(); t[0]
IndexError: tuple index out of range

>>> s=''; s[0] # 3.0
IndexError: string index out of range

>>> b=b''; b[0]
IndexError: index out of range # *** 'bytes' uniquely missing here ***

>>> ba=bytearray(b); ba[0]
IndexError: bytearray index out of range

Is the check factored out as a macro (or function) so one change would
change all of these?

Similar errors with dict (tersely) give the bad key already:
>>> d={}; d[1]
...
KeyError: 1

-------------------------
The may be superficially similar to request #654558, but that was vague
and was closed as a duplicate of #569574, which is definitely about a
different issue.  I did not see anything else in the search results.

----------
components: Interpreter Core
messages: 82634
nosy: tjreedy
severity: normal
status: open
title: Improve IndexError messages with actual values
type: feature request
versions: Python 2.7, Python 3.1

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue5353>
_______________________________________


More information about the Python-bugs-list mailing list