You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
>>> list(IFoo)
['x']
>>> list(ProxyFactory(IFoo))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
zope.security.interfaces.ForbiddenAttribute: ('__len__', <InterfaceClass __main__.IFoo>)
AFAICT the reason is that Python's list(an_object) tries to compute len(an_object) as an optimization (to preallocate all the list entries in one go). Python 2 ignores exceptions raised by an_object.__class__.__len__(an_object) when they're an AttributeError subclass and merely skips the optimization. Python 3 does not suppress the exception but instead fails the entire operation.
It's a bit more subtle than that, e.g. if a class doesn't have a __len__, then list() on instances of that class succeeds, but if the class has a __len__ that does raise AttributeError, then list() on instances of that class fails (on Python 3).
I'm not sure if this is a problem in zope.interface or in zope.security. The simplest solution would be to allow __len__ on Interface objects.
The text was updated successfully, but these errors were encountered:
I suspect the fix belongs in zope.security. There have been other zope.interface-on-Python3 fixes necessary there, specifically around iteration and length hints (and also for BTrees) (e.g., zopefoundation/zope.security@6f8de03)
jamadden
added a commit
to zopefoundation/zope.security
that referenced
this issue
Feb 6, 2020
This code works on Python 2.7:
It breaks on Python 3:
AFAICT the reason is that Python's
list(an_object)
tries to computelen(an_object)
as an optimization (to preallocate all the list entries in one go). Python 2 ignores exceptions raised byan_object.__class__.__len__(an_object)
when they're an AttributeError subclass and merely skips the optimization. Python 3 does not suppress the exception but instead fails the entire operation.It's a bit more subtle than that, e.g. if a class doesn't have a
__len__
, then list() on instances of that class succeeds, but if the class has a__len__
that doesraise AttributeError
, then list() on instances of that class fails (on Python 3).I'm not sure if this is a problem in zope.interface or in zope.security. The simplest solution would be to allow
__len__
on Interface objects.The text was updated successfully, but these errors were encountered: