msg38717 - (view) |
Author: Quinn Dunkan (quinn_dunkan) |
Date: 2002-01-17 02:52 |
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. |
|
|
msg38718 - (view) |
Author: Just van Rossum (jvr) *  |
Date: 2002-03-16 08:24 |
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. |
|
|
msg38719 - (view) |
Author: Quinn Dunkan (quinn_dunkan) |
Date: 2002-03-16 08:55 |
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 :) |
|
|
msg38720 - (view) |
Author: Armin Rigo (arigo) *  |
Date: 2002-11-12 13:27 |
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. |
|
|
msg38721 - (view) |
Author: Tim Peters (tim.peters) *  |
Date: 2002-11-12 22:38 |
Logged In: YES user_id=31435 Sorry, I'm rejecting this, at Guido's request. I agree: far too much code would break if this were to change now; if you want hasattr()-like functionality that passes on "unexpected" exceptions, you can get it now via doing getattr(obj, attr, None); and, most fundamentally, it's part of hasattr's contract (design) that it never raises an exception. |
|
|
msg165640 - (view) |
Author: Karl Chen (quarl) |
Date: 2012-07-16 17:16 |
For the record, this was eventually fixed for Python 3.2. See http://bugs.python.org/issue9666. Adding this here because comes up earlier than in many web searches. |
|
|