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: Add index attribute to IndexError
Type: enhancement Stage: test needed
Components: Interpreter Core Versions: Python 3.5
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: barry, blackfawn, brett.cannon, cool-RR, cvrebert, eric.araujo, ezio.melotti, r.david.murray, rhettinger, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2013-06-07 20:50 by brett.cannon, last changed 2022-04-11 14:57 by admin.

Files
File name Uploaded Description Edit
indexerror.patch blackfawn, 2015-04-15 22:16 patch review
indexerror.tests blackfawn, 2015-04-15 22:16 tests review
indexerror_all.patch blackfawn, 2015-04-15 23:41 Full patch including code+tests+docs review
Messages (18)
msg190777 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2013-06-07 20:50
Give IndexError an index attribute to store the index it was raised for. Since it is typically an integer there is no reason to worry about GC and thus using a regex.
msg190840 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2013-06-09 03:45
regex?  do you mean weakref?
msg190879 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2013-06-10 00:45
Yes, I mean weakref.
msg191234 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2013-06-15 21:05
Is there a meta-issue for these changes?
I remember a similar discussion a couple of years ago, but I don't remember if it was on python-dev/ideas or on the bug tracker.
I agree that exceptions could be improved, but I would like to get the big picture of the changes you are planning to do instead of many individual issues.  A simple PEP might also be good, depending on how far you want to go :)
msg191245 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2013-06-15 22:55
Nope, no meta-issue. I literally just realized one evening that the handful of exceptions that I filed bugs for could use an attribute for why the exception was raised.
msg191256 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2013-06-16 03:01
I would also like to see a PEP or good python-dev discussion before embarking on all these API expansions.  There should be an evaluation of whether any existing code would benefit from it, whether the current "args" attribute is sufficient, whether the code bloat is worth it, etc.

Also, I'm unclear on whether you expect users to have to provide the index value in their own code (i.e. is "raise IndexError" to still be permitted or will all pure python code be required to use "raise IndexError(i)" and change existing code if they already use "raise IndexError("Out of range")).

If you don't change requirements for pure python code, then how can be even rely on the new attribute being there (i.e.  is "except IndexError as e:  print(e.index)" guaranteed to work?).
msg191258 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2013-06-16 04:29
Obviously it can't be required that the index be provided as that would break way too much code. There are already exceptions in the stdlib that have optional attributes you can choose to (not) set.

As for relying upon it, it would be just like any other object that gets a new attribute: either you rely on it because you know the code you are using always provides it (e.g. using core/stdlib code directly) or you use some way to deal with cases where it was not set (EAFP or LBYL). Subclassing IndexError to provide a inheritance guarantee that an attribute exists seems like overkill (e.g. IndexError2 so you can do ``except IndexError2 as exc:``).
msg191386 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2013-06-18 02:00
-1 on expanding the API for an attribute that is sometimes there and sometimes not.  This doesn't add any value, but it does add complication.
msg191403 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2013-06-18 12:38
Do you mean "sometimes there sometimes not" because old code won't set it (yet) or because you don't think it will always be appropriate to set the attribute and thus people won't set it when available?
msg191408 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-06-18 13:47
I am in favor of adding meaningful attributes to stdlib exceptions.  I've always considered the lack of such an API a wart in Python, though an understandable one (since exceptions started out as simple strings).  But yeah, while I hate to say it, this is probably mini-PEP material :(.
msg202327 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-11-07 12:06
See also issue1534607.
msg222353 - (view) Author: Ram Rachum (cool-RR) * Date: 2014-07-05 13:17
Since #21911 has been merged into this issue, I'd like to add: Please also include the length of the sequence in the exception message. It can help with debugging.
msg222354 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2014-07-05 13:23
Knowing the len of the sequence would also be useful.
Brett, were you also planning to use these attributes in the error message (when they are available), or do you prefer to keep the two issues separate and reopen #21911?

> Is there a meta-issue for these changes?

FTR the other relevant issues are:
  #18156: Add an 'attr' attribute to AttributeError
  #18163: Add a 'key' attribute to KeyError
  #18165: Add 'unexpected_type' to TypeError
  #18166: 'value' attribute for ValueError
msg222355 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2014-07-05 13:27
Part of the point of these various attributes I proposed was so that a good default message could be provided when only the new attributes are given. So I'm fine with that being part of this issue.
msg241149 - (view) Author: Ofer Schwarz (blackfawn) * Date: 2015-04-15 19:47
I'm working on this now (patch hopefully coming soon), and I've hit an interesting issue:
PySequence methods take care of negative indexes by adding +len before calling the subclass method (s.t. -1 becomes len-1 etc.). This means that if the subclass raises an exception with the index, it might be the wrong index.

This case only happens when index < -len, so when I talked it out with Larry he suggested just passing the original index to the subclass in this case. So for index >= 0 or index < -len the subclass will receive the actual index, and for -len <= index < 0 the subclass will get index+len.
That's a change in behavior, but I actually think it's a good one. The negative-index use case in PySequence is exactly intended for this [-len, 0) use case, and if some subclass wants to do something weird with negative indexes it'd probably want the actual argument anyway.

I'm writing my patch with this in it, but it might be worth discussing here if someone disagrees.
msg241153 - (view) Author: Ram Rachum (cool-RR) * Date: 2015-04-15 20:18
Looking forward to your patch Ofer! Thanks for working on this!
msg257269 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2016-01-01 03:47
See also #1182143.
msg257270 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2016-01-01 04:19
And PEP 473.
History
Date User Action Args
2022-04-11 14:57:46adminsetgithub: 62362
2019-03-05 17:38:00SilentGhostlinkissue36200 superseder
2016-01-01 04:19:11ezio.melottisetmessages: + msg257270
2016-01-01 03:47:25ezio.melottisetmessages: + msg257269
2015-08-08 15:21:19r.david.murraylinkissue24830 superseder
2015-04-15 23:41:01blackfawnsetfiles: + indexerror_all.patch
2015-04-15 22:16:35blackfawnsetfiles: + indexerror.tests
2015-04-15 22:16:18blackfawnsetfiles: + indexerror.patch
keywords: + patch
2015-04-15 20:18:07cool-RRsetmessages: + msg241153
2015-04-15 19:47:16blackfawnsetnosy: + blackfawn
messages: + msg241149
2014-07-05 13:27:41brett.cannonsetmessages: + msg222355
2014-07-05 13:23:49ezio.melottisetmessages: + msg222354
2014-07-05 13:17:26cool-RRsetnosy: + cool-RR
messages: + msg222353
2014-07-05 13:14:49ezio.melottilinkissue21911 superseder
2014-02-01 00:13:59yselivanovsetversions: + Python 3.5, - Python 3.4
2013-11-07 18:22:48brett.cannonlinkissue1534607 superseder
2013-11-07 12:06:10serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg202327
2013-06-18 13:47:20r.david.murraysetnosy: + r.david.murray
messages: + msg191408
2013-06-18 12:38:02brett.cannonsetmessages: + msg191403
2013-06-18 02:00:32rhettingersetmessages: + msg191386
2013-06-16 04:29:05brett.cannonsetmessages: + msg191258
2013-06-16 03:01:29rhettingersetnosy: + rhettinger
messages: + msg191256
2013-06-15 22:55:03brett.cannonsetmessages: + msg191245
2013-06-15 21:05:39ezio.melottisetnosy: + ezio.melotti
messages: + msg191234
2013-06-14 16:25:40cvrebertsetnosy: + cvrebert
2013-06-10 00:45:28brett.cannonsetmessages: + msg190879
2013-06-09 03:45:12eric.araujosetnosy: + eric.araujo
messages: + msg190840
2013-06-07 21:23:35barrysetnosy: + barry
2013-06-07 20:50:59brett.cannoncreate