classification
Title: Add an 'attr' attribute to AttributeError
Type: enhancement Stage: test needed
Components: Interpreter Core Versions: Python 3.4
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: ag6502, alex, benjamin.peterson, brett.cannon, eric.araujo, ezio.melotti, kermit666, serhiy.storchaka
Priority: normal Keywords: easy, patch

Created on 2013-06-07 15:31 by brett.cannon, last changed 2014-10-24 15:10 by serhiy.storchaka.

Files
File name Uploaded Description Edit
attributeerror-attr.patch kermit666, 2013-07-06 16:56 review
Repositories containing patches
https://bitbucket.org/kermit666/cpython/src/0fa84b73a4e5f09846327d90041ba9c645cf11a3?at=attr
Messages (10)
msg190754 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2013-06-07 15:31
Much like ImportError now has 'name' and 'path', AttributeError should get an 'attr' attribute that can only be set through a keyword argument or after creating an instance. That would make the common ``try/except AttributeError`` uses much more robust by not accidentally swallowing an AttributeError that has nothing to do with the attribute in question::

 try:
   cls.meth()
 except AttributeError as exc:
   if exc.attr != 'meth':
     raise
msg190783 - (view) Author: Alex Gaynor (alex) * (Python committer) Date: 2013-06-08 00:03
+1 on this, but it's worth noting that that fix is not 100% correct (though it's obviously better than most existing equivilants), it's potentially wrong with custom __getattr__, __getattribute__, descriptors.
msg190787 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2013-06-08 01:47
Such custom implementations should be updated to support this wonderful new attr. :)
msg190788 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2013-06-08 01:54
What Benjamin said.

Adding something like this is mostly about a nicer constructor (``AttributeError(attr='meth')``) and automatically creating the message for the exception (although that would require another argument like 'object' or something to be able to do e.g. "'dict' object has no attribute 'asdfdsff'" or "type object 'dict' has no attribute 'asdfdsf'").
msg190789 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2013-06-08 01:54
And standardizing on an attribute name, of course. =)
msg192466 - (view) Author: Dražen Lučanin (kermit666) * Date: 2013-07-06 16:56
I've been working on this at the EuroPython sprint today and it seems the change requires editing >20 files that call PyExc_AttributeError. This means it would be quite a big and dangerous change, so for now I just attach the optional argument addition - without it actually being used by the rest of the codebase.

What is your oppinion on it?
msg192468 - (view) Author: Andrea Griffini (ag6502) * Date: 2013-07-06 17:08
Even porting to the new wonderful 'attr' field is not going to make the code correct... (the exception could be bubbling up from code down ten frames about a different unrelated attribute that happens to have the same name in a different object). BTW cpython has a lot of those "except AttributeError" fragments coded in C with PyErr_ExceptionMatches.
msg192470 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2013-07-06 17:22
Dražen: didn't do a deep review, but a cursory look suggests the patch is fine.

As for having to change a ton of files to start using the attribute, that's part of the effect of changing something as basic as an exception. If you would rather break it up into separate patches that's fine. But I would caution you from doing more than this patch as I have gotten some push back from other core devs on my proposed new attributes on exceptions so I don't want you spending your time on something that might get sunk (although if you are enjoying it then go for it since if it gets accepted this work will be needed).

Also please sign the contributor agreement form (http://python.org/psf/contrib/contrib-form/) so that we can actually use your code.

Andrea: While the attribute might coincidentally name an attribute that is the same as some other attribute which is not the actual trigger, the traceback on the exception provides the proper context to know what attribute in what code did the actual triggering. The point is that the exception can be considered better than nothing and is still an improvement over not having the information at all.
msg192555 - (view) Author: Dražen Lučanin (kermit666) * Date: 2013-07-07 13:34
OK, thanks for the feedback. I signed the CLA.

I'll then wait with the remaining work, until a final decision has been made. We have a rough idea of how it could be implemented if it comes to this - adding a wrapper function in Python/errors.c:

    PyErr_SetAttributeError(PyObject *attr, const char *format, ...)

that would replace all the PyErr_SetObject, PyErr_SetString and PyErr_Format calls (in around 50 files), create the kwargs object, format the message (if provided) and call PyErr_SetObject or PyErr_SetFormat.

I put the last patch as a commit in the attr bookmark on BitBucket (took me quite some time to figure out that's the alternative to git branches), so that subsequent changes go more easily.
msg229928 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-10-24 15:10
In issue22716 proposed to add a reference to the object missing an attribute.
History
Date User Action Args
2014-10-25 15:26:33r.david.murrayunlinkissue22716 superseder
2014-10-24 15:10:31serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg229928
2014-10-24 15:07:45serhiy.storchakalinkissue22716 superseder
2013-07-07 13:34:34kermit666sethgrepos: + hgrepo203
messages: + msg192555
2013-07-06 17:22:44brett.cannonsetmessages: + msg192470
2013-07-06 17:08:18ag6502setmessages: + msg192468
2013-07-06 16:56:12kermit666setfiles: + attributeerror-attr.patch

nosy: + ag6502, kermit666
messages: + msg192466

keywords: + patch
2013-06-08 17:51:30ezio.melottisetkeywords: + easy
nosy: + ezio.melotti
2013-06-08 01:54:37brett.cannonsetmessages: + msg190789
2013-06-08 01:54:05brett.cannonsetmessages: + msg190788
2013-06-08 01:47:09benjamin.petersonsetnosy: + benjamin.peterson
messages: + msg190787
2013-06-08 00:03:34alexsetnosy: + alex
messages: + msg190783
2013-06-07 18:27:56eric.araujosetnosy: + eric.araujo
2013-06-07 15:31:50brett.cannoncreate