[Patches] [ python-Patches-504714 ] hasattr catches only AttributeError

noreply@sourceforge.net noreply@sourceforge.net
Tue, 12 Nov 2002 05:27:10 -0800


Patches item #504714, was opened at 2002-01-17 02:52
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=504714&group_id=5470

Category: Core (C code)
Group: Python 2.1.2
Status: Open
Resolution: None
Priority: 5
Submitted By: Quinn Dunkan (quinn_dunkan)
Assigned to: Nobody/Anonymous (nobody)
Summary: hasattr catches only AttributeError

Initial Comment:
Curse me for a fool.  I reported this exact same thing
in getattr but failed to
look 30 lines down to notice hasattr.

hasattr(foo, 'bar') catches all exceptions.  I think it
should only catch 
AttributeError.  Example:

>>> class Foo:
...     def __getattr__(self, attr):
...         assert 0
... 
>>> f = Foo()
>>> hasattr(f, 'bar')
0               # should have gotten an AssertionError
>>>

This patch makes hasattr only catch AttributeError.  I
changed the 
docstring to reflect that, and also changed the getattr
docstring
to read a little more naturally.


----------------------------------------------------------------------

Comment By: Armin Rigo (arigo)
Date: 2002-11-12 13:27

Message:
Logged In: YES 
user_id=4771

This looks like a possibly worthwhile semantic change.  In
order to help this patch progress I would recommend you to
raise the question in python-dev.

The library documentation should also be patched to reflect
the change.


----------------------------------------------------------------------

Comment By: Quinn Dunkan (quinn_dunkan)
Date: 2002-03-16 08:55

Message:
Logged In: YES 
user_id=429749

That's true, but the current behavior can mask bugs
unexpectedly.  For example, if you ask someone if the
brakes are engaged, and they discover that the brakes have
crumbled to dust and fallen off, you probably want a
different answer than "no". :)

getattr() (now) only catches AttributeErrors, so there's 
a consistency thing too.

Anyway, it's your call :)

----------------------------------------------------------------------

Comment By: Just van Rossum (jvr)
Date: 2002-03-16 08:24

Message:
Logged In: YES 
user_id=92689

(The patch seems to be reversed.)
The patch otherwise looks fine to me, but it will break 
code that depends on the current behavior.
It can be argued that if getattr() raises *any* error, the 
attr doesn't exist, so the current behavior is in fact 
correct.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=504714&group_id=5470