Bad idiom to access optional attributes
I just analysed why "AccessControl.Role.RoleManager.get_valid_userids" raised an unexpected "AttributeError". It uses the wrong idiom to access optional attribute: aclu = getattr(aq_base(item), '__allow_groups__', _notfound) This means, that "aclu" is only partially acquisition wrapped and does not behave as usual. Until Tim reported that "hasattr" behaves bad with respect to exceptions, I have been convinved, that the following idiom were correct: if hasattr(aq_base(obj), attrname): attr = getattr(obj, attrname) ... Meanwhile, the best way to access optional attributes seems to be if getattr(aq_base(obj), attrname, notFound) is not notFound: attr = getattr(obj, attrname) ... Please keep this in mind when you push the "Death to 'hasattr'" project. -- Dieter
On Mon, 7 Jun 2004 07:46:41 +0200 Dieter Maurer <dieter@handshake.de> wrote: [..]
Meanwhile, the best way to access optional attributes seems to be
if getattr(aq_base(obj), attrname, notFound) is not notFound: attr = getattr(obj, attrname) ...
Ahhh, the miracle of acqfuscation... -Casey
Casey Duncan wrote at 2004-6-7 09:16 -0400:
On Mon, 7 Jun 2004 07:46:41 +0200 Dieter Maurer <dieter@handshake.de> wrote:
[..]
Meanwhile, the best way to access optional attributes seems to be
if getattr(aq_base(obj), attrname, notFound) is not notFound: attr = getattr(obj, attrname) ...
Ahhh, the miracle of acqfuscation...
The better way would of course be to incapsulate this into a function, similar to my "hasattr_unacquired" (which waits as a feature request in Zope's collector without being touched). The "hasattr_unacquired" still uses "hasattr" but "death to 'hasattr'" would need to replace it with the above "getattr(..., notFound) is not notFound" test. -- Dieter
participants (2)
-
Casey Duncan -
Dieter Maurer