[Zope] using getProperty and catching Unauthorized

John K. Hohm jhohm@acm.org
Sat, 26 Oct 2002 11:40:05 -0500


Quoting Casey Duncan <casey@zope.com>:
> Yes, but as of right now, Python strings that do not contain spaces are
> interned as the same object. So as long as the name does not have a space in
> it, it should work.

Thanks all, for the responses.  Perhaps it should work, but it doesn't.  I get
the Unauthorized exception in my browser even though I'm doing this:

    try:
        menu_order = sibling.getProperty('menu_order', None)
    except 'Unauthorized': continue

I somehow got the impression from reading the Python manual that raising an
exception with a string that is the name of a class will raise the class
instead, so I tried doing this:

from AccessControl import Unauthorized
#...
    try:
        menu_order = sibling.getProperty('menu_order', None)
    except Unauthorized: continue

But then I get this incredibly ironic error in the browser:
Error Type: ImportError
Error Value: import of "Unauthorized" from "AccessControl" is unauthorized. You
are not allowed to access Unauthorized in this context

So apparently I'm allowed to call things that throw Unauthorized, but I'm not
allowed to import Unauthorized in order to catch it?  Harsh.  Is there some
other way of going about this?  Perhaps some function that simulates the access
control check that happens inside the acquisition wrapper, and returns a
booleanish result?

> That said, string exceptions suck rocks. But string exceptions with spaces
> suck gravel ;^)
> 
> -Casey
> 
> On Fri, 25 Oct 2002 21:37:53 +0200
> Dieter Maurer <dieter@handshake.de> wrote:
> 
> > Casey Duncan writes:
> >  > I think the following should do what you want:
> >  > 
> >  > for foo in bar.objectvalues()
> >  >     try:
> >  >         mo = foo.getProperty('menu_order', None)
> >  >     except 'Unauthorized':
> >  >         continue
> >  >     ..do stuff..
> > It may not work, because Python uses "is" to check against string
> > exceptions and not "==".
> > 
> > 
> > Dieter