Daniel Nouri <[EMAIL PROTECTED]>
writes:

> Martin Aspeli wrote:
>
>> Unfortunately, acquisition doesn't work properly inside Python property
>> __get__ accessors. I've seen this pattern before, but I've seen people have
>> various problems with it (that I can't remember the details of right now).
>
> Hmm, I've been using @property now for quite a long time for handling
> context without any problems.  It would be good to have a minimal failing
> example.
>
> I know that formlib actions, which are applied via a decorator, have
> problems with acquisition.  But then, they do much more than @property.

Here's a doctest showing what I think Martin was talking about:

;-*-Doctest-*-
==========================
Properties and Acquisition
==========================

Inside a descriptor method, including properties, self loses
acquisition.

We start with some descriptors and properties used in a class to
access an attribute of self and to check for the acquisition
wrappers::

    >>> class BarDescriptor(object):
    ...     def __get__(self, instance, owner):
    ...         return instance.bar

    >>> class HasAqDescriptor(object):
    ...     def __get__(self, instance, owner):
    ...         return hasattr(instance, 'aq_parent')

    >>> from OFS.SimpleItem import SimpleItem
    >>> class Foo(SimpleItem):
    ...     bar = 'bar'
    ...     @property
    ...     def bar_property(self):
    ...         return self.bar
    ...     bar_descr = BarDescriptor()
    ...     @property
    ...     def has_aq(self):
    ...         return hasattr(self, 'aq_parent')
    ...     has_aq_descr = HasAqDescriptor()

Now we instantiate our object, add it to the application and retrieve
it with acquisition wrappers::

    >>> self.app._setObject('foo', Foo('foo'))
    'foo'
    >>> foo = self.app._getOb('foo')
    >>> foo
    <Foo at />

Everything works as it should for instance attributes::

    >>> foo.bar
    'bar'
    >>> foo.bar_property
    'bar'
    >>> foo.bar_descr
    'bar'

But even through the object is acquisition wrapper, the descriptors,
including properties, don't have access to the wrapper::

    >>> foo.aq_parent
    <Application at >
    >>> foo.has_aq
    True
    >>> foo.has_aq_descr
    True
Those last two "True"s come out "False" if you actually run this test.

It's a problem in all descriptors as far as I can tell.  I'd be very
interested in any work arounds for this as I run into it all the time.

Also, is there someplace this test might be included?  Or is this
considered not to be a bug?

Also attached is a tarball of a minimal product providing this doctest.

Ross

Attachment: aqdescriptors.tar.bz2
Description: Binary data

_______________________________________________
Product-Developers mailing list
[email protected]
http://lists.plone.org/mailman/listinfo/product-developers

Reply via email to