[PEAK] Permissions for class with __getitem__

Radek Kanovsky rk at dat.cz
Mon Feb 14 07:46:04 EST 2005


I have class that implements __getitem__ via metaclass:

    >>> class XMeta (type) :
    ...     def __getitem__ (klass, k) :
    ...         return k

    >>> class X :
    ...     __metaclass__ = XMeta
   
    >>> X['foo'] # invokes XMeta.__getitem__(X, 'foo')

Permissions declaration for class X fails:

    >>> binding.declareMetadata(X, security.Anybody)

The failure depends on __getitem__ implementation. Problem is in how
generic function parses signatures, especialy 'x in y' expression. It
generates two forms of criterions:

    1. 'x in y' -> 'isinstance(x,y)'  or 'adapt(x,y)'
    2. 'x in y' -> 'x == y[0] or x == y[1] ....'

The form depends on 'y'. If it supports iteration, form 2 is choosen.



Permission metadata are registered via generic function 

    permissionFor.when('... subj in classobj ...')

If 'classobj' supports iteration, parser tries to generate form 2
and declaration eats all memory and swap :-) Remedy is to construct
signature "by hand" or use 'isinstance' directly.

I know it is minor problem but anyway 'x in y' expression is slightly
ambiguous. Wouldn't be better to deprecate form 1 when we can now use
unambiguous and more clear ``isinstance(subj, classobj)'' expression?

RadekK



More information about the PEAK mailing list