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, brett.cannon, cool-RR, cvrebert, eric.araujo, ezio.melotti, r.david.murray, rhettinger, serhiy.storchaka
Priority: normal Keywords:

Created on 2013-06-07 20:50 by brett.cannon, last changed 2014-07-05 13:27 by brett.cannon.

Messages (14)
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.
History
Date User Action Args
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