[issue18156] Add an 'attr' attribute to AttributeError

2017-08-18 Thread Brett Cannon

Changes by Brett Cannon :


--
keywords:  -easy

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18156] Add an 'attr' attribute to AttributeError

2016-03-08 Thread Xiang Zhang

Xiang Zhang added the comment:

The concerns mentioned by haypo seems to have existed in PEP473.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18156] Add an 'attr' attribute to AttributeError

2016-03-08 Thread STINNER Victor

STINNER Victor added the comment:

> try:
>   cls.meth()
> except AttributeError as exc:
>   if exc.attr != 'meth':
> raise

What if cls.__getattr__('meth') calls a completly unrelated function which also 
raises AttributeError(attr='meth') but on a different object? I would also 
expect an "obj" attribute, a reference to the object which doesn't have 
attribute.

But AttributeError can be raised manually without setting attr and/or obj 
attribute. So the best test would look to something like:

if exc.obj is cls and exc.attr is not None and exc.attr != 'meth':
raise

The test is much more complex than expected :-/ Maybe it's simpler to split the 
code to first get the bounded method?

try:
   meth = cls.meth
except AttributeError:
   ...
meth()

Or check first if cls has the attribute 'meth' with hasattr(cls, 'meth')?

--

About attr/obj attribute not set, an alternative is to add a new 
BetterAttributeError(obj, attr) exception which requires obj and attr to be 
set, it inherits from AttributeError.

class BetterAttributeError(AttributeError):
   def __init__(self, obj, attr):
   super().__init__('%r has no attribute %r' % (obj, attr)
   self.obj = obj
   self.attr = attr

It would allow a smoother transition from "legacy" AttributeError to the new 
BetterAttributeError.

The major issue with keeping a strong reference to the object is that Python 3 
introduced Exception.__traceback__ which can create a reference cycle if an 
exception is stored in a local variable somewhere in the traceback. It's a 
major pain point in asyncio. In asyncio, the problem is more likely since 
exceptions are stored in Future objects to be used later.

--
nosy: +haypo

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18156] Add an 'attr' attribute to AttributeError

2016-03-07 Thread Xiang Zhang

Xiang Zhang added the comment:

I'd like to ping this channel.

I post a question on 
https://groups.google.com/forum/#!topic/comp.lang.python/y8yDAAJJ9Sc to ask if 
it is possible to directly get the attribute from AttributeError, which leads 
me here.

--
nosy: +xiang.zhang

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18156] Add an 'attr' attribute to AttributeError

2015-10-12 Thread Shubham Dash

Changes by Shubham Dash :


Added file: http://bugs.python.org/file40757/client.py

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18156] Add an 'attr' attribute to AttributeError

2014-11-02 Thread Ezio Melotti

Ezio Melotti added the comment:

I closed #22716 in favor of this, since I think both should be tackled together.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18156
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18156] Add an 'attr' attribute to AttributeError

2014-11-02 Thread flying sheep

flying sheep added the comment:

yeah, exactly: my idea was to add a reference to the original object 
(AttributeError.target). together with this bug, that would be the 
AttributeError part of PEP 473, which i really like!

--
nosy: +flying sheep

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18156
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18156] Add an 'attr' attribute to AttributeError

2014-10-24 Thread Serhiy Storchaka

Serhiy Storchaka added the comment:

In issue22716 proposed to add a reference to the object missing an attribute.

--
nosy: +serhiy.storchaka

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18156
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18156] Add an 'attr' attribute to AttributeError

2013-07-07 Thread Dražen Lučanin

Dražen Lučanin added the comment:

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.

--
hgrepos: +203

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18156
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18156] Add an 'attr' attribute to AttributeError

2013-07-06 Thread Dražen Lučanin

Dražen Lučanin added the comment:

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?

--
keywords: +patch
nosy: +ag6502, kermit666
Added file: http://bugs.python.org/file30829/attributeerror-attr.patch

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18156
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18156] Add an 'attr' attribute to AttributeError

2013-07-06 Thread Andrea Griffini

Andrea Griffini added the comment:

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.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18156
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18156] Add an 'attr' attribute to AttributeError

2013-07-06 Thread Brett Cannon

Brett Cannon added the comment:

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.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18156
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18156] Add an 'attr' attribute to AttributeError

2013-06-08 Thread Ezio Melotti

Changes by Ezio Melotti ezio.melo...@gmail.com:


--
keywords: +easy
nosy: +ezio.melotti

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18156
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18156] Add an 'attr' attribute to AttributeError

2013-06-07 Thread Brett Cannon

New submission from Brett Cannon:

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

--
components: Interpreter Core
messages: 190754
nosy: brett.cannon
priority: normal
severity: normal
stage: test needed
status: open
title: Add an 'attr' attribute to AttributeError
type: enhancement
versions: Python 3.4

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18156
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18156] Add an 'attr' attribute to AttributeError

2013-06-07 Thread Éric Araujo

Changes by Éric Araujo mer...@netwok.org:


--
nosy: +eric.araujo

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18156
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18156] Add an 'attr' attribute to AttributeError

2013-06-07 Thread Alex Gaynor

Alex Gaynor added the comment:

+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.

--
nosy: +alex

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18156
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18156] Add an 'attr' attribute to AttributeError

2013-06-07 Thread Benjamin Peterson

Benjamin Peterson added the comment:

Such custom implementations should be updated to support this wonderful new 
attr. :)

--
nosy: +benjamin.peterson

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18156
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18156] Add an 'attr' attribute to AttributeError

2013-06-07 Thread Brett Cannon

Brett Cannon added the comment:

And standardizing on an attribute name, of course. =)

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18156
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18156] Add an 'attr' attribute to AttributeError

2013-06-07 Thread Brett Cannon

Brett Cannon added the comment:

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').

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18156
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com