Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion can pointers be stored in Python's dict or list?
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Mark Summerfield  
View profile  
 More options Sep 14 2012, 3:55 pm
From: Mark Summerfield <l...@qtrac.plus.com>
Date: Fri, 14 Sep 2012 20:54:58 +0100
Local: Fri, Sep 14 2012 3:54 pm
Subject: Re: [cython-users] can pointers be stored in Python's dict or list?
Hi Robert,

On Fri, 14 Sep 2012 10:45:06 -0700

Robert Bradshaw <rober...@gmail.com> wrote:
> On Fri, Sep 14, 2012 at 8:11 AM, Mark Summerfield <l...@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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.