[Pyrex] Please support C++
Lenard Lindstrom
len-l at telus.net
Wed Oct 19 08:21:55 CEST 2005
On 19 Oct 2005 at 12:04, Greg Ewing wrote:
> David M. Cooke wrote:
> > On Oct 18, 2005, at 17:59, Greg Ewing wrote:
> >
> >> object PyArray_ContiguousFromObject(object op, ...)
> >
> > True enough, but that doesn't help: I can't cast it to ArrayType with
> > <ArrayType>, because PyObject * is used instead.
>
> You shouldn't need to cast it at all; Pyrex should
> accept assignment to something of type ArrayType
> as-is.
For the following lines of the Pyrex example:
cdef ArrayType array1, array2
...
array1 = PyArray_ContiguousFromObject(o, PyArray_DOUBLE, 1, 1)
the following C code was generated (spacing added for legibility):
PyArrayObject *__pyx_v_array1;
....
PyObject *__pyx_1 = 0;
....
/* "C:\user\projects\python23\pyrex_test\array\assign.pyx":32 */
__pyx_1 = ((PyObject
*)PyArray_ContiguousFromObject(__pyx_v_o,PyArray_DOUBLE,1,1)); if
(!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 32; goto
__pyx_L1;}
Py_DECREF(((PyObject *)__pyx_v_array1));
__pyx_v_array1 = (PyArrayObject *)__pyx_1;
__pyx_1 = 0;
The necessary C cast for __pyx_v_array1 was added automatically.
No Pyrex cast was needed. But it only works because extension type
rvalues are always cast and treated as (PyObject *). So the
PyArray_ContiguousFromObject return value will never be assigned
directly to a PyArrayObject * variable. There currently is no conflict between
the .h and Pyrex declarations of the function.
>
> The downside is that it will insert a runtime type
> check that isn't really necessary. I'll have to think
> some more about how to address that. Perhaps there
> needs to be some way to declare that "this function
> is declared in C as returning PyObject *, but it really
> return something of type whatever".
>
Maybe the Pyrex compiler can generate a warning for an unnecessary
Pyrex cast. It still works, even if it adds some runtime overhead.
Lenard Lindstrom
<len-l at telus.net>
More information about the Pyrex
mailing list