This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Improve IndexError messages with actual values
Type: enhancement Stage:
Components: Interpreter Core Versions: Python 3.2
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: rhettinger, terry.reedy
Priority: normal Keywords:

Created on 2009-02-23 18:38 by terry.reedy, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (6)
msg82634 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2009-02-23 18:38
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.
msg83679 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2009-03-17 13:47
This issue was discussed two years ago in this thread:

http://mail.python.org/pipermail/python-list/2006-December/588224.html

Most of the messages under that subject are noise about politeness or
lack thereof (including a post by the submitter of this issue trying to
get people to be reasonable!), but an actual patch was posted as well:

http://mail.python.org/pipermail/python-list/2006-December/588377.html

The patch addresses only one object type, but does demonstrate a pattern
that might be usable in general.  A concern was raised about the
generality of the function proposed, as well as the performance
implications of calling it.

I have not tried to apply the patch to the current trunk, but from a
quick look it doesn't seem like the source has changed much.  Since the
problem turns out to be a lot less trivial than it seems like it should
be, the patch is a good reference in any case.
msg87800 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2009-05-15 08:56
See also issue 1182143.
msg87826 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2009-05-15 19:00
This issue is about formatting a value into an error message.
#1182143 is about adding values that have already been formatted into an
error message to the resulting exception object.  If this suggestion
were implemented, then IndexError would be added to the exceptions
covered by #1182143

Patches to mailing lists tend to get lost.  Thank you for finding
Bjourne's. I have invited him (at the email then given) to update it and
attach it here.
msg87827 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2009-05-15 19:17
I've seen his patch and am working on my own variant that also attaches
the index value as a separate field in the args tuple.  The problem may
be that it slows down the code -- at first I thought that didn't matter,
but someone made a good effort to make the existing code fast (possibly
to support cases where IndexError is used for control flow instead of
for error conditions).
msg122011 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2010-11-21 22:52
I'm abandoning this one since I couldn't find a way to do it that didn't impair performance.  Unlike C++, it is not uncommon in Python to use exceptions such as IndexError for control flow.  There was too little added value in building-out this message to warrant the change (the current message is very clear about what the problem is, it just doesn't dump all the variables involved -- this is not uncommon for python error messages).
History
Date User Action Args
2022-04-11 14:56:46adminsetgithub: 49603
2010-11-21 22:52:50rhettingersetstatus: open -> closed
resolution: wont fix
messages: + msg122011
2010-08-21 13:45:05BreamoreBoysetversions: + Python 3.2, - Python 3.1, Python 2.7
2009-05-28 01:11:40r.david.murraysetnosy: - r.david.murray
2009-05-15 19:17:30rhettingersetmessages: + msg87827
2009-05-15 19:01:00terry.reedysetmessages: + msg87826
2009-05-15 14:46:13rhettingersetassignee: rhettinger

nosy: + rhettinger
2009-05-15 08:56:50r.david.murraysetmessages: + msg87800
2009-04-01 02:40:25r.david.murraysetnosy: + r.david.murray, - r.david.murray-old
2009-04-01 01:46:15r.david.murray-oldsetnosy: + r.david.murray-old, - r.david.murray
2009-03-17 13:47:28r.david.murraysetnosy: + r.david.murray
messages: + msg83679
2009-02-23 18:38:28terry.reedycreate