[Numpy-discussion] __array__struct__: about using PyCapsule instead of PyCObject for Python 2.7

Lisandro Dalcin dalcinl@gmail....
Wed Jun 30 16:07:40 CDT 2010

On 30 June 2010 15:08, Charles R Harris <charlesr.harris@gmail.com> wrote:
> On Wed, Jun 30, 2010 at 10:57 AM, Lisandro Dalcin <dalcinl@gmail.com> wrote:
>> On 30 June 2010 02:48, Charles R Harris <charlesr.harris@gmail.com> wrote:
>> >
>> Oh! Sorry! Now I realize that!
> Do I detect a touch of sarcasm?

No, no sarcasm at all! I just realized that PyCObject were
(pending)deprecated in 2.7 ... Anyway. let me say I'm so annoyed and
upset as you.

>> > Grrr... I didn't see the point, myself, I'm tempted to deprecate 2.7
>> > just to
>> > get even. There are some routines in the numpy/core/src includes that
>> > you
>> > might want to copy, they will allow you to use a common interface for
>> > PyCObject and PyCapsule if you need to do that.
>> >
>> I've already fixed my code for PyCapsule. What's not clear to me is
>> how to build (i.e, use the old cobject or the new capsules)
>> __array_struct__ across NumPy and Python versions combinations. Will
>> NumPy 1.x series ever support Python 2.7? In such case, should I use
>> cobjects or capsules?
>> Still, I think NumPy should still accept cobjects from extension types
>> on a Python 2.x runtime, the fix for this support is trival: accept
>> cobjects in NpyCapsule_Check() and NpyCapsule_AsVoidPtr() for
> That might work. The destructors are different between the two, but if you
> just need access to the pointer things should be simpler.

That's my point. I'm fine with numpy creating PyCapsule's in 2.7, as
long as PyCObject's are still accepted. Given a cobject, NumPy only
needs to get the pointer.

> If you test it out
> and put together a patch I don't see why it shouldn't be applied. If it
> works. I'm a bit dubious about changing the check however.

Well, if the check is not modified to accept PyCObject's, the things
will simply not work. NumPy  uses NpyCapsule_Check in order to
typecheck __array_struct__ attributes. That check should succeed for
cobjects, then you know can call NpyCapsule_AsVoidPtr.

>> People can still live with PendingDeprecationWarning (remember, they
>> are shown if explicitly requested with -W), and such warning is
>> triggered when cobjects are created, not when they are accessed.
> Yeah, but it is kind of ugly to have to deal with the warnings.

Yes, I understand. However, I'll insist about accepting PyCObject's,
this will not make warnings show up.

So, in short, if you accept that both NpyCapsule_Check() and
NpyCapsule_AsVoidPtr() should accept PyCObject's for Py < 3.1, I can
provide the patch... I can stress-test this because i'm using
__array_struct__ in order to share memory between numpy arrays and
PETSc vectors.

Lisandro Dalcin
Predio CONICET-Santa Fe
Colectora RN 168 Km 472, Paraje El Pozo
Tel: +54-342-4511594 (ext 1011)
Tel/Fax: +54-342-4511169

More information about the NumPy-Discussion mailing list