|
List Info
Thread: Use of hasattr in attribute lookup
|
|
| Use of hasattr in attribute lookup |
  United States |
2007-06-07 03:42:31 |
Hi all,
I've been using Genshi for a while with Pylons, and it's
been quite
fun, but now I have hit a small problem. I have created an
auth
object, which permissions can be registered on. So to do a
permissions check one can do
if auth.perm2():
which seems to be what I want (though I might have just
reinvented the
wheel and made it square).
The problem I have is that Genshi's attribute lookup on auth
in
genshi.template.eval.LookupBase.lookup_attr() checks for
attributes
using hasattr().
hasattr() catches all exceptions and returns False, which
means that
if an exception occurs in auth.__getattr__() I never see it
(but I get
another exception due to missing attribute).
Is there a reason not to do:
try:
return getattr(obj, key)
except AttributeError:
pass
instead of the
if hasattr(): getattr()
pattern?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Genshi" group.
To post to this group, send email to genshi googlegroups.com
To unsubscribe from this group, send email to
genshi-unsubscribe googlegroups.com
For more options, visit this group at http://gr
oups.google.com/group/genshi?hl=en
-~----------~----~----~----~------~----~------~--~---
|
|
| Re: Use of hasattr in attribute lookup |

|
2007-06-07 03:59:28 |
On 6/7/07, Graham Stratton <google ramsey.uk.com> wrote:
> I've been using Genshi for a while with Pylons, and
it's been quite
> fun, but now I have hit a small problem. I have
created an auth
> object, which permissions can be registered on. So to
do a
> permissions check one can do
>
> if auth.perm2():
>
> which seems to be what I want (though I might have just
reinvented the
> wheel and made it square).
>
> The problem I have is that Genshi's attribute lookup on
auth in
> genshi.template.eval.LookupBase.lookup_attr() checks
for attributes
> using hasattr().
> hasattr() catches all exceptions and returns False,
which means that
> if an exception occurs in auth.__getattr__() I never
see it (but I get
> another exception due to missing attribute).
I don't know Pylons at all, but why would the attribute
lookup raise
an error you were interested in? I would expect the function
call
after the attribute lookup to be doing the actual
permissions check?
Schiavo
Simon
--
I don't see why people are picky about it when the
Banach-Tarski
paradox is clearly a Biblical principle - look at Mark
6:38-44. What,
you have a different interpretation of the loaves and fishes
thing?
-- Daniel Martin, snowplow.org
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Genshi" group.
To post to this group, send email to genshi googlegroups.com
To unsubscribe from this group, send email to
genshi-unsubscribe googlegroups.com
For more options, visit this group at http://gr
oups.google.com/group/genshi?hl=en
-~----------~----~----~----~------~----~------~--~---
|
|
| Re: Use of hasattr in attribute lookup |
  United States |
2007-06-07 16:13:22 |
On 7 Jun., 10:42, Graham Stratton <goo... ramsey.uk.com> wrote:
> The problem I have is that Genshi's attribute lookup on
auth in
> genshi.template.eval.LookupBase.lookup_attr() checks
for attributes
> using hasattr().
> hasattr() catches all exceptions and returns False,
which means that
> if an exception occurs in auth.__getattr__() I never
see it (but I get
> another exception due to missing attribute).
>
> Is there a reason not to do:
>
> try:
> return getattr(obj, key)
> except AttributeError:
> pass
I'm not sure, but I suspect using hasattr() may have shown
better
performance in my tests. But I should really measure that
again to be
sure.
Note that catching AttributeError will also swallow any
AttributeError
raised from inside the property getter, so while it's better
than
hasattr() in this respect, it still isn't perfect.
Cheers,
Chris
--
http://www.cmlenz.net/
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Genshi" group.
To post to this group, send email to genshi googlegroups.com
To unsubscribe from this group, send email to
genshi-unsubscribe googlegroups.com
For more options, visit this group at http://gr
oups.google.com/group/genshi?hl=en
-~----------~----~----~----~------~----~------~--~---
|
|
| Re: Use of hasattr in attribute lookup |
  United States |
2007-06-22 05:15:05 |
Hi Chris, thanks for your reply.
On Jun 7, 10:13 pm, Christopher Lenz <cml... gmx.de> wrote:
> On 7 Jun., 10:42, Graham Stratton <goo... ramsey.uk.com> wrote:
> > The problem I have is that Genshi's attribute
lookup on auth in
> > genshi.template.eval.LookupBase.lookup_attr()
checks for attributes
> > using hasattr().
> > hasattr() catches all exceptions and returns
False, which means that
> > if an exception occurs in auth.__getattr__() I
never see it (but I get
> > another exception due to missing attribute).
> > Is there a reason not to do:
>
> > try:
> > return getattr(obj, key)
> > except AttributeError:
> > pass
>
> I'm not sure, but I suspect using hasattr() may have
shown better
> performance in my tests. But I should really measure
that again to be
> sure.
Any news on this?
> Note that catching AttributeError will also swallow any
AttributeError
> raised from inside the property getter, so while it's
better than
> hasattr() in this respect, it still isn't perfect.
That's true, but there is the option of catching
AttributeErrors in
the code and raising a different exception, so at least
there is
_some_ way to get at the exception; currently there is
none.
Thanks,
Graham
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Genshi" group.
To post to this group, send email to genshi googlegroups.com
To unsubscribe from this group, send email to
genshi-unsubscribe googlegroups.com
For more options, visit this group at http://gr
oups.google.com/group/genshi?hl=en
-~----------~----~----~----~------~----~------~--~---
|
|
| Re: Use of hasattr in attribute lookup |
  Germany |
2007-07-17 06:35:44 |
Am 22.06.2007 um 12:15 schrieb Graham Stratton:
> Hi Chris, thanks for your reply.
>
>> I'm not sure, but I suspect using hasattr() may
have shown better
>> performance in my tests. But I should really
measure that again to be
>> sure.
>
> Any news on this?
The benchmarks show no difference at all, so I must have
been
misremembering.
>> Note that catching AttributeError will also swallow
any
>> AttributeError
>> raised from inside the property getter, so while
it's better than
>> hasattr() in this respect, it still isn't perfect.
>
> That's true, but there is the option of catching
AttributeErrors in
> the code and raising a different exception, so at least
there is
> _some_ way to get at the exception; currently there is
none.
Right. This change has made it into the 0.4.3 release.
Thanks for the suggestion!
Cheers,
Chris
--
Christopher Lenz
cmlenz at gmx.de
http://www.cmlenz.net/
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Genshi" group.
To post to this group, send email to genshi googlegroups.com
To unsubscribe from this group, send email to
genshi-unsubscribe googlegroups.com
For more options, visit this group at http://gr
oups.google.com/group/genshi?hl=en
-~----------~----~----~----~------~----~------~--~---
|
|
[1-5]
|
|