creating a python list of pointers

195 views
Skip to first unread message

Henry Gomersall

unread,
Mar 9, 2012, 10:11:38 AM3/9/12
to cython...@googlegroups.com
I wish to create a python list of pointers, such that I can do something
like this:

pointer_list = []
cdef int* foo

pointer_list.append(foo)

cdef int* retrieved_pointer = pointer_list[0]

Is this a sensible thing to do? If so, is it possible?

Cheers,

Henry

Chris Colbert

unread,
Mar 9, 2012, 10:26:28 AM3/9/12
to cython...@googlegroups.com
Python lists can only hold pointers to PyObject. But, you can use Cython in c++ mode and use a std::vector<int*>. 

Henry Gomersall

unread,
Mar 9, 2012, 10:30:40 AM3/9/12
to cython...@googlegroups.com
On 09/03/2012 15:26, Chris Colbert wrote:
pointer_list = []
cdef int* foo

pointer_list.append(foo)

cdef int* retrieved_pointer = pointer_list[0]

Is this a sensible thing to do? If so, is it possible?


Python lists can only hold pointers to PyObject. But, you can use Cython in c++ mode and use a std::vector<int*>. 

Yeah, that would work. My poor c++-fu has always led me to avoid such solutions, but I can see it's probably a good way.

Cheers,

Henry

Lars Buitinck

unread,
Mar 9, 2012, 10:43:06 AM3/9/12
to cython...@googlegroups.com
2012/3/9 Henry Gomersall <he...@cantab.net>:

> Yeah, that would work. My poor c++-fu has always led me to avoid such
> solutions, but I can see it's probably a good way.

Thinking out loud: for a Cython/C solution, you could cast those
pointers to uintptr_t (from <inttypes.h>), then store them as Python
integers and later cast them back. That does require a C99 compiler,
so it wouldn't work with MSVC, I believe.

--
Lars Buitinck
Scientific programmer, ILPS
University of Amsterdam

mark florisson

unread,
Mar 9, 2012, 7:48:12 PM3/9/12
to cython...@googlegroups.com

You could use Py_intptr_t, or in Python 2 you can use PyLong_FromVoidPointer.

Reply all
Reply to author
Forward
0 new messages