Issue1501932

classification
Title: hasattr invokes __getattr__
Type: Severity: normal
Components: Core Versions:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: ayeshaiqbal, jeffnorton, juneau001, leouserz, pjenvey
Priority: normal Keywords:

Created on 2006-06-06.23:29:06 by jeffnorton, last changed 2008-04-09.07:03:12 by pjenvey.

Messages
msg1146 (view) Author: Jeff Norton (jeffnorton) Date: 2006-06-06.23:29:06
In CPython hasattr(obj, "foo") does not invoke 
obj.__getattr__ even if "foo" not in obj.__dict__.  In 
Jython it does.
msg1147 (view) Author: Juneau001 (juneau001) Date: 2006-06-08.20:35:59
Logged In: YES 
user_id=1536464

It looks to me like both hasattr and __getattr__ actually 
invoke __findattr__.  I think hasattr may need to be 
reworked so that it does not use __findattr__.
msg1148 (view) Author: Deleted User leouserz (leouserz) Date: 2006-12-20.19:06:07
Im trying to figure out the bug here.  From:
http://docs.python.org/lib/built-in-funcs.html

"hasattr(  	object, name)
    The arguments are an object and a string. The result is True if the string is the name of one of the object's attributes, False if not. (This is implemented by calling getattr(object, name) and seeing whether it raises an exception or not.)"

Running this code:
>>> class x:
...    def __getattr__(self, name):
...       print name

and invoking
z = x()
hasattr(z, "something")

produces the same effect on Python as well as Jython.  Im working with a Jython3005.

leouser

msg1149 (view) Author: ayesha (ayeshaiqbal) Date: 2007-08-31.05:58:23
leouser's code sample indeed shows that jython and python behave identically with respect to the issue of hasattr invoking getattr protocol.Not sure whether this is a bug after all.Perhaps we can get a more detailed description of the bug from jeff ,maybe with a code sample that shows clearly the discrepancy between jython and python behaviour with respect to this bug

Also a point of note :if hasattr is reworked so that it does not invoke findattr then patch  1775263(still open) will be rendered invalid
msg3143 (view) Author: Philip Jenvey (pjenvey) Date: 2008-04-09.05:30:39
Right, in theory the 2 don't differ in beavhior. I did find one 
difference, though:

Python 2.5.1 (r251:54863, Aug 19 2007, 21:02:30) 
[GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> class Foo(object):
...     def __getattr__(self, name):
...             raise TypeError()
... 
>>> hasattr(Foo(), 'bar')
False

Jython 2.3a0 on java1.5.0_13
Type "copyright", "credits" or "license" for more information.
>>> class Foo(object):
...     def __getattr__(self, name):
...             raise TypeError()
... 
>>> hasattr(Foo(), 'bar')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in __getattr__
TypeError: 

I wonder if Jeff ran into something like this? Anyway, that anomaly is 
fixed in r4324, so I'm closing this out
msg3146 (view) Author: Philip Jenvey (pjenvey) Date: 2008-04-09.07:03:11
really fixed this time in r4327
History
Date User Action Args
2008-04-09 07:03:13pjenveysetmessages: + msg3146
2008-04-09 05:30:42pjenveysetstatus: open -> closed
nosy: + pjenvey
resolution: fixed
messages: + msg3143
2006-06-06 23:29:06jeffnortoncreate