Re: [cython-users] beginner question: passing std::vector by reference

75 views
Skip to first unread message

Robert Bradshaw

unread,
Oct 3, 2012, 12:55:31 AM10/3/12
to cython...@googlegroups.com
On Tue, Oct 2, 2012 at 12:38 AM, peter aberline
<peter.a...@gmail.com> wrote:
> Hi all,
>
> I'm new to Cython, it looks to be an incredible technology - congratulations
> to all the devs.
>
> Please excuse my basic question. I'm trying to pass a reference to a
> std::vector to a function and I'm getting a cpp compilaton error. Here's a
> minimum example of what I'm doing:
>
>
> test.py:
>
> import sys
>
> def vectortest(a, idx):
> print ('a[idx] is: ' + str(a[idx]))
> a[idx] += 1
> print ('a[idx] is: ' + str(a[idx]))
> return a
>
> def run():
> lst = range(1, 5)
> lstNew = vectortest(lst, 1)
>
> print ('lstNew[1] is :' + str(lstNew[1]))
>
>
>
> test.pxd
> import cython
> from libcpp.vector cimport vector
>
> import sys
>
> if sys.version_info < (3,):
> range = xrange
>
> cpdef vector[int] vectortest(vector[int] a, int idx) # <-- This works fine
> ('a' is pass by value?)
>
> #cpdef vector[int] vectortest(vector[int]& a, int idx) #<-- This generates
> an 'uninitialised reference' cpp compilation error ('a' is pass by
> reference?')
>
> @cython.locals(lst = vector[int])
> cpdef run()
>
>
> Compilation error:
>
> $/test.cpp: In function ‘PyObject* __pyx_pw_4perf_1vectortest(PyObject*,
> PyObject*, PyObject*)’:
> $/test.cpp:640:21: error: ‘__pyx_v_a’ declared as reference but not
> initialized
>
>
> Can anyone suggest what I'm going wrong?

Looks like this was a bug introduced In 0.17.1 and already fixed in master.

- Robert

Stefan Behnel

unread,
Oct 7, 2012, 2:06:30 AM10/7/12
to cython...@googlegroups.com
peter aberline, 02.10.2012 09:38:
> test.pxd
> import cython
> from libcpp.vector cimport vector
>
> import sys
>
> if sys.version_info < (3,):
> range = xrange

Are you really using this in a .pxd file? I think it would be nice if
Cython gave you a compile error for this - it's dead code.

Stefan

Reply all
Reply to author
Forward
0 new messages