[Cython] Expected errors of tests/errors/cdef_members_T517.pxd

Robert Bradshaw robertwb at math.washington.edu
Sun Feb 27 04:16:37 CET 2011


On Sat, Feb 26, 2011 at 6:34 PM, W. Trevor King <wking at drexel.edu> wrote:
> On Sat, Feb 26, 2011 at 10:15:38AM -0800, Robert Bradshaw wrote:
>> On Sat, Feb 26, 2011 at 5:08 AM, W. Trevor King <wking at drexel.edu> wrote:
>> > The relevant lines from tests/errors/cdef_members_T517.pxd are:
>> >
>> >    $ grep -n ^ tests/errors/cdef_members_T517.pyx
>> >    ...
>> >    8:ctypedef struct Foo:
>> >    9:    int i
>> >    10:
>> >    11:cdef class Bar:
>> >    12:    cdef          Foo foo0
>> >    13:    cdef readonly Foo foo2
>> >    14:    cdef public   Foo foo1
>> >    15:    pass
>> >    ...
>>
>> ...
>>
>> What's less clear is the behavior of
>>
>>    b = Bar()
>>    b.foo1 = Foo(i=1)
>>    b.foo1.i = 100
>>    print b.foo1
>>    saved = b.foo1
>>    del b
>>    print saved
>
> Is the problem choosing between:
>
> 1) Copy struct data into its own PyObject and let Python manage the
>   memory.
> 2) Pass around a pointer to Cython-managed struct data.

Yes, exactly.

> I suppose I don't really know enough about how Cython handles this yet
> to really see the problem or be able to help with a solution ;).

Cython doesn't handle this yet--that's what we get to decide. Another
option would be to use weakrefs to implement
copy-on-owner-destruction, but that has even subtler corner semantics.

    b = Bar()
    b.foo1 = Foo(i=1)
    first = b.foo1
    second = b.foo1
    first.i = 100
    print second
    del b
    first.i = 200
    print second

I'm leaning towards (1) for ease and efficiency of implementation and
it's easier to explain in all cases.

- Robert


More information about the cython-devel mailing list