[Pyrex] Please support C++

David M. Cooke cookedm at physics.mcmaster.ca
Tue Oct 18 14:53:39 CEST 2005


On Oct 17, 2005, at 18:04, David M. Cooke wrote:
> Oooh, this sounds like what I was trying figure out, to track down
> some more compiler warnings. Basically, (PyObject *) was being used to
> cast an extension type, even when there was an explicit cast to  
> that type.
> I'm currently getting around it with a postprocessor on the generated
> .c file.

Greg asked me to post an example:

cdef extern from "Numeric/arrayobject.h":
     enum ArrayTypes "PyArray_TYPES":
         PyArray_CHAR, PyArray_UBYTE, PyArray_SBYTE,
         PyArray_SHORT, PyArray_USHORT,
         PyArray_INT, PyArray_UINT,
         PyArray_LONG,
         PyArray_FLOAT, PyArray_DOUBLE,
         PyArray_CFLOAT, PyArray_CDOUBLE,
         PyArray_OBJECT,
         PyArray_NTYPES, PyArray_NOTYPE

     struct ArrayDescr "PyArray_Descr":
         ArrayTypes type_num
         int elsize
         char type
     ctypedef extern class Numeric.ArrayType [object PyArrayObject]:
         cdef char *data
         cdef int nd
         cdef int *dimensions, *strides
         cdef object base
         cdef ArrayDescr *descr
         cdef int flags
     void import_array()
     ArrayType PyArray_ContiguousFromObject(object op, ArrayTypes  
type, int min_dim, int max_dim)

import_array()

def donothing(o):
     cdef ArrayType array1, array2

     array1 = PyArray_ContiguousFromObject(o, PyArray_DOUBLE, 1, 1)
     array2 = <ArrayType>PyArray_ContiguousFromObject(o,  
PyArray_DOUBLE, 1, 1)

The two calls to PyArray_ContiguousFromObject generate code that set  
array1 and array2 like this:

   __pyx_1 = ((PyObject *)PyArray_ContiguousFromObject 
(__pyx_v_o,PyArray_DOUBLE,1
,1)); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 31;  
goto __pyx_
L1;}
   Py_DECREF(((PyObject *)__pyx_v_array1));
   __pyx_v_array1 = __pyx_1;
   __pyx_1 = 0;

   /* "/home/cookedm/stuff/python/Numeric/pyrex-api/testcast.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_INCREF(((PyObject *)__pyx_1));
   Py_DECREF(((PyObject *)__pyx_v_array2));
   __pyx_v_array2 = ((PyObject *)__pyx_1);
   Py_DECREF(__pyx_1); __pyx_1 = 0;

-- 
|>|\/|<
/------------------------------------------------------------------\
|David M. Cooke              http://arbutus.physics.mcmaster.ca/dmc/
|cookedm at physics.mcmaster.ca




More information about the Pyrex mailing list