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
Passing C pointers to Cython function
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
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
 
Horace Abenga  
View profile  
 More options Sep 27 2012, 5:39 am
From: Horace Abenga <horaceabe...@gmail.com>
Date: Thu, 27 Sep 2012 02:39:03 -0700 (PDT)
Local: Thurs, Sep 27 2012 5:39 am
Subject: Passing C pointers to Cython function

Is it possible to pass pointers to defined types in Cython to constructors,
for example:

cdef extern from "Foo.h":
    cdef cppclass Bar:
        pass

cdef class PyClass:
    cdef Bar *bar

    def __cinit__(self, Bar *b)
        bar = b

When you do this, you get a *Cannot convert Python object to 'Bar *'*.* *

(Copied from
http://stackoverflow.com/questions/12204441/passing-c-pointer-as-argu...,
the top answer there is convoluted and seems to be just wrapping a double,
what I'm trying to do is a bit more complicated)

I am using such a construct to implement a "Has-A" (composition)
relationship between two types that are defined as structs in a C Library.

In my code, the Class *Bar* has a non-trivial constructor, and the
"constructor" for *PyClass* in C should receive a pointer to an initialized
*Bar* object.

Is there a simple way of doing this?


 
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.
Robert Bradshaw  
View profile  
 More options Sep 27 2012, 5:48 am
From: Robert Bradshaw <rober...@gmail.com>
Date: Thu, 27 Sep 2012 02:47:44 -0700
Local: Thurs, Sep 27 2012 5:47 am
Subject: Re: [cython-users] Passing C pointers to Cython function

Python class construction requires a Python call, hence Python object
arguments. The two answers given on stackoverflow (writing a global
method that constructs the object then sets the pointer, or creating a
wrapper class for Bar you an pass around in Python space) are the
typical ways of handling this. There's not really anything "simpler"
(though that's pretty simple).

- Robert


 
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.
Sturla Molden  
View profile  
 More options Sep 27 2012, 5:50 am
From: Sturla Molden <sturlamol...@yahoo.no>
Date: Thu, 27 Sep 2012 11:50:32 +0200
Local: Thurs, Sep 27 2012 5:50 am
Subject: Re: [cython-users] Passing C pointers to Cython function
On 27.09.2012 11:39, Horace Abenga wrote:

> Is it possible to pass pointers to defined types in Cython to
> constructors, for example:

> |cdefextern  from  "Foo.h":
>         cdef cppclassBar:
>                 pass

> cdefclass  PyClass:
>         cdefBar  *bar

>         def  __cinit__(self,  Bar  *b)
>                 bar=  b|

> When you do this, you get a *Cannot convert Python object to 'Bar *'*.**

__cinit__ takes the same arguments as __init__.

Sturla


 
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.
Sturla Molden  
View profile  
 More options Sep 27 2012, 6:03 am
From: Sturla Molden <sturlamol...@yahoo.no>
Date: Thu, 27 Sep 2012 12:03:35 +0200
Local: Thurs, Sep 27 2012 6:03 am
Subject: Re: [cython-users] Passing C pointers to Cython function
On 27.09.2012 11:39, Horace Abenga wrote:

> In my code, the Class *Bar* has a non-trivial constructor, and the
> "constructor" for *PyClass* in C should receive a pointer to an
> initialized *Bar* object.

Uaually this is an error of reasoning, as a non-trival constructor will
be non-trivial outside __cinit__ as well. So calling it from __cinit__
cannot be more difficult than calling it from anywhere else.

Sturla


 
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.
Czarek Tomczak  
View profile  
 More options Sep 27 2012, 7:35 am
From: Czarek Tomczak <czarek.tomc...@gmail.com>
Date: Thu, 27 Sep 2012 04:35:30 -0700 (PDT)
Local: Thurs, Sep 27 2012 7:35 am
Subject: Re: Passing C pointers to Cython function

For each cdef class create a global cdef function that acts as a
constructor, CefResponse is a C++ object, PyResponse a python equivalent of
a c++ object:

   1. cdef object CreatePyResponse(CefRefPtr[CefResponse] cefResponse):
   2.  
   3.     pyResponse = PyResponse()
   4.     pyResponse.cefResponse = cefResponse
   5.     return pyResponse
   6.  
   7. cdef class PyResponse:
   8.  
   9.     cdef CefRefPtr[CefResponse] cefResponse
   10.  
   11.     def GetStatus(self):
   12.  
   13.         return (<CefResponse*>(self.cefResponse.get())).GetStatus()

Czarek.


 
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.
End of messages
« Back to Discussions « Newer topic     Older topic »