cdef declaration inside conditional fails

3,013 views
Skip to first unread message

seventhunders

unread,
Jun 18, 2010, 3:57:22 PM6/18/10
to cython-users
I am using a development version of cython after the 0.12.1 series
and I'm trying to wrap some C++ code. I have a templated class that
can have several instantiations based on the underlying type of the
array. I want the instantiation to be dependent on some input flags.
Unfortunately cython is not letting me put a cdef inside an if block.

---------------------------------code
snippet---------------------------
cpdef binwrite(char *file, np.ndarray arr, int iscomplex,int
isdouble) :
""" Python equivalent of bin write, but take an ndarray as an
input.
iscomplex=1 for complex 0 for real, isdouble=1 for double 0 for
single precision
floating point """
cdef np.ndarray narr
cdef int nr, nc
nr, nc = arr.shape
# unfortunately we will copy this array twice
# copy the array to a right size and shaped nd array attach it to
a dynmatrix and then write
if (iscomplex == 0) & (isdouble == 0) :
narr = array(arr, dtype=np.float32, order='F')
cdef dynmatrix[float] *a = new dynmatrix(<float *> narr.data,
nr, nc, nr, Realmatrix)
a.write(file)
del a
elif (iscomplex == 0) & (isdouble == 1) :
narr = array(arr, dtype=np.float64, order='F')
cdef dynmatrix[double] *a = new dynmatrix(<double *>
narr.data, nr, nc, nr, Realmatrix)
....
---------------------------------------------------------------------------------

Cython error
-----------------------------------------------------
...
nr, nc = arr.shape
# unfortunately we will copy this array twice
# copy the array to a right size and shaped nd array attach it to
a dynmatrix and then write
if (iscomplex == 0) & (isdouble == 0) :
narr = array(arr, dtype=np.float32, order='F')
cdef dynmatrix[float] *a = new dynmatrix(<float *> narr.data,
nr, nc, nr, Realmatrix)
^
------------------------------------------------------------

pygpumat.pyx:454:13: cdef statement not allowed here

Since this is supposed to compile to C++ why can't I put a variable
declaration wherever I darn well please?

Dag Sverre Seljebotn

unread,
Jun 19, 2010, 3:40:08 AM6/19/10
to cython...@googlegroups.com

Python and C scoping rules differs. Please tell us how this is supposed to
behave:

if a:
cdef int x = 4
else:
cdef double x = 5.3
print x

In Python, x would always be available also after the if-test. It's hard
to create rules that would be consistent and work well with what you
propose.

Dag Sverre

Robert Bradshaw

unread,
Jun 21, 2010, 2:39:20 PM6/21/10
to cython...@googlegroups.com
On Jun 18, 2010, at 12:57 PM, seventhunders wrote:

> I am using a development version of cython after the 0.12.1 series
> and I'm trying to wrap some C++ code. I have a templated class that
> can have several instantiations based on the underlying type of the
> array. I want the instantiation to be dependent on some input flags.
> Unfortunately cython is not letting me put a cdef inside an if block.
>

> Since this is supposed to compile to C++ why can't I put a variable
> declaration wherever I darn well please?

As Dag Sverre mentioned, there are issues with scoping rules (among
other things). On a higher level, rather than thinking about writing C+
+ code with Python syntax, it's often more correct to just think of
writing Python code that can create/manipulate/call C++ objects and
functions.

- Robert

seventhunders

unread,
Jun 22, 2010, 2:07:40 PM6/22/10
to cython-users
OK I can see the problem with that. Fortunately there is a
straightforward workaround in part because I have to
declare all C++ objects as pointers. Thus there isn't too much
overhead associated with predeclaring the pointers prior to
any if then else statements.

I guess also the C++ scoping rules are way different from Python's so
I suppose it wouldn't make much sense.

On Jun 21, 2:39 pm, Robert Bradshaw <rober...@math.washington.edu>
wrote:

Dag Sverre Seljebotn

unread,
Jun 22, 2010, 3:10:22 PM6/22/10
to cython...@googlegroups.com
seventhunders wrote:
> OK I can see the problem with that. Fortunately there is a
> straightforward workaround in part because I have to
> declare all C++ objects as pointers. Thus there isn't too much

In fact, the scoping issues is the reason you are limited to only use
C++ objects through pointers in the first place. The C++ object
construction/destruction rules clashes very much with a Python feel in
this respect.

Dag Sverre

> overhead associated with predeclaring the pointers prior to
> any if then else statements.
>
> I guess also the C++ scoping rules are way different from Python's so
> I suppose it wouldn't make much sense.
>
> On Jun 21, 2:39 pm, Robert Bradshaw <rober...@math.washington.edu>
> wrote:
>> On Jun 18, 2010, at 12:57 PM, seventhunders wrote:
>>
>>> I am using a development version of cython after the 0.12.1 series
>>> and I'm trying to wrap some C++ code. I have a templated class that
>>> can have several instantiations based on the underlying type of the
>>> array. I want the instantiation to be dependent on some input flags.
>>> Unfortunately cython is not letting me put a cdef inside an if block.
>>> Since this is supposed to compile to C++ why can't I put a variable
>>> declaration wherever I darn well please?
>> As Dag Sverre mentioned, there are issues with scoping rules (among
>> other things). On a higher level, rather than thinking about writing C+
>> + code with Python syntax, it's often more correct to just think of
>> writing Python code that can create/manipulate/call C++ objects and
>> functions.
>>
>> - Robert


--
Dag Sverre

Reply all
Reply to author
Forward
0 new messages