[Cython] Bug in inplace operators?

Hagen Fürstenau hagen at zhuliguan.net
Tue Sep 13 20:22:26 CEST 2011


I'm new to Cython, so I don't know if this is a bug, a known limitation, 
or a misunderstanding on my part, but the following simple example 
produces C code that fails to compile:

import numpy
cimport numpy

def f(numpy.ndarray[numpy.int32_t, ndim=1] a not None, i):
     cdef numpy.ndarray[numpy.int32_t, ndim=1] b

     b = numpy.ones(10, dtype=numpy.int32)
     b[0] *= a[i]

a = numpy.array([1, 2, 3], dtype=numpy.int32)
f(a, 1)

Running this on Cython 0.15 and Python 3.2.2 with

$ python -c 'import pyximport; pyximport.install(); import x'

I get a compiler error, the essential part of which seems to be

/export/home/hagenf/.pyxbld/temp.linux-x86_64-3.2/pyrex/x.c:1105: error: 
invalid operands to binary * (have ‘__pyx_t_5numpy_int32_t’ and ‘struct 
PyObject *’)

The problem is in the line "b[0] *= a[i]". Replacing this with "b[0] = 
b[0] * a[i]" solves it. As does typing i as an int (which of course is a 
good idea anyway).


More information about the cython-devel mailing list