Hi Robert,
On Fri, 14 Sep 2012 10:45:06 -0700
Robert Bradshaw <
robe...@gmail.com> wrote:
> On Fri, Sep 14, 2012 at 8:11 AM, Mark Summerfield <
li...@qtrac.plus.com>
> wrote:
> > Hi,
> >
> > (1)
> >
> > I am using Cython to provide a Python wrapper to a C library.
> > The C library has a function which gives a handle (a pointer) which is
> > then used to call other functions.
> >
> > I want to be able to store a dictionary of name-pointer pairs so that I
> > can resuse handles.
> >
> > This doesn't seem to work with dict or list because of the
> > Cannot convert 'Thing*' to Python object
> > error.
> >
> > Right now I have a static array and a dictionary:
> >
> > cdef Thing* pointers[30]
> > indexForThingName = {}
> >
> > but I'd really like to be able to store an arbitrary number of
> > pointers, ideally in a dict.
> >
> > Is this possible?
>
> Sounds like you want
>
http://docs.python.org/release/3.1.5/c-api/capsule.html
But that means I have to drop down to C. I want to stay in Python and
Cython. I was hoping that Cython offered some kind of wrapper type,
something that at the Python level would look like:
class Pointer(object):
def __init__(self, void *pointer):
cdef void *self.pointer = pointer # Doesn't work
that could be put in a dict or list. Then I could do things like this:
Pointer p = <void*>c_func_that_returns_a_pointer()
mylist.append(p)
...
Foo *q = <Foo*>mylist.pop()
Oh well, I'll see if I can use libcpp's map...
> > (2)
> >
> > Also, I tried doing
> >
> > from libc.stdio cimport printf
> >
> > and then in my code:
> >
> > printf("%p", pointer)
> >
> > but although it compiled without error, nothing was printed.
>
> Did it perhaps not flush? You also might want to try adding a newline.
I changed it to printf("%p\n", pointer) and now I get
Cannot convert 'void *' to Python object
--
Mark Summerfield, Qtrac Ltd,
www.qtrac.eu
C++, Python, Qt, PyQt - training and consultancy
"Programming in Go" - ISBN 0321774639
http://www.qtrac.eu/gobook.html