Cython: How to wrap a C++ function that returns a C++ object?

MCor

I'm working on a Python project where I'd like to interface with a C++ package that has already been written. Since I'll be using Cython in other portions of this project, I'd prefer to wrap using Cython.

In brief, I need to wrap a function, FooBar, that returns an object of custom class type Bar.

Here's the Bar.h:

#include <cstddef> // For size_t
#include <vector>

/* data returned by function FooBar()*/
class Bar {
public:
    size_t X;
    std::vector<size_t> Y;      
    std::vector<double> Z;  
    std::vector<double> M;  
    std::vector<size_t> N;  

};


Bar FooBar(const std::vector<double> & O, size_t P, size_t Q);

And PyBar.pyx:

from libcpp.vector cimport vector

cdef extern from "Bar.h":
    cdef cppclass Bar:
        size_t X
        vector[size_t] Y    
        vector[double] Z    
        vector[double] M 
        vector[size_t] N    
    cdef Bar FooBar(const vector[double] & O, size_t P, size_t Q)

cdef class PyBar:
    cdef Bar *thisptr      # hold a C++ instance which we're wrapping

    def __cinit__(self, O, P, Q):
        C_Bar = FooBar(O, P, Q)
        self.thisptr = &C_Bar

    def __dealloc__(self):
        del self.thisptr

Actual Question: Is this even the right approach to what I want to do? For reference, if I just tried to wrap the class by itself I have had no problem: I can import the module, create objects using PyBar(), and underlying C methods implemented on the class would work. The issue is trying to wrap a function that returns objects of the C++ class. In the wild, I'll never actually want to create PyBar representation of any Bar object that wasn't created by FooBar, so this is the approach I decided upon after much head scratching.

DavidW

With respect to the first part of the problem, I think the more elegant change would be to have FooBar defined as:

Bar* FooBar(const std::vector<double> & O, size_t P, size_t Q);

and have it return a "new" allocated pointer. I think in your original Cython code __cinit__ you'll create a stack allocated Bar, take a pointer of that, and then that will expire resulting in eventual disaster.

An alternative solution that might work would be be to keep FooBar returning Bar, change PyBar so it starts

cdef class PyBar:
   cdef Bar this_obj

   def __cinit__(self, O, P, Q):
      self.this_obj = FooBar(O,P,Q)

i.e. keeps an object rather than a pointer. No __dealloc__ should be necessary.

I don't know about the undefined symbol error...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

wrap C ++ function in c # that returns pointer

From Dev

How to wrap a C++ function that returns boost::optional<T>?

From Dev

How to expose a function returning a C++ object to Python using Cython?

From Dev

Passing Cython class object as argument to C function

From Dev

How to use function written in C in Cython

From Dev

How to speed up a function that returns a pointer to object in c++?

From Dev

cython wrap struct as object

From Dev

C++ wrap multiple returns

From Dev

Wrap alloca function in C

From Dev

Wrap alloca function in C

From Dev

How to pass python function as an argument to c++ function using Cython

From Dev

How to wrap a C struct that contains a C++ object in python ctypes

From Dev

Cython: How do I wrap a C++ class where public member variables are custom objects?

From Dev

How to pass Python list to C function using Cython

From Dev

Using Cython to wrap a c++ template to accept any numpy array

From Dev

Wrap C++ Class with cython, getting the basic example to work

From Dev

How to wrap or call a C function with void as return in Emscripten?

From Dev

Using SWIG and the Python/C API to wrap a function which returns a std::map

From Dev

Return c++ object (not a pointer preferably) in cython

From Dev

C a function that returns an array

From Dev

Wrap c++ function that needs function pointer

From Dev

Passing primitive pointers to C function from Cython

From Dev

return numpy array in cython defined c function

From Dev

Cython: calling C function throws 'undefined symbol'

From Dev

cython/python: how to catch as exception an external function that "exits" instead of "returns"

From Dev

cython/python: how to catch as exception an external function that "exits" instead of "returns"

From Dev

How do I declare a function that returns a pointer to a function that returns a function pointer without using a typedef in C?

From Dev

A way to pass c++ object to another object's method in cython

From Dev

A way to pass c++ object to another object's method in cython

Related Related

  1. 1

    wrap C ++ function in c # that returns pointer

  2. 2

    How to wrap a C++ function that returns boost::optional<T>?

  3. 3

    How to expose a function returning a C++ object to Python using Cython?

  4. 4

    Passing Cython class object as argument to C function

  5. 5

    How to use function written in C in Cython

  6. 6

    How to speed up a function that returns a pointer to object in c++?

  7. 7

    cython wrap struct as object

  8. 8

    C++ wrap multiple returns

  9. 9

    Wrap alloca function in C

  10. 10

    Wrap alloca function in C

  11. 11

    How to pass python function as an argument to c++ function using Cython

  12. 12

    How to wrap a C struct that contains a C++ object in python ctypes

  13. 13

    Cython: How do I wrap a C++ class where public member variables are custom objects?

  14. 14

    How to pass Python list to C function using Cython

  15. 15

    Using Cython to wrap a c++ template to accept any numpy array

  16. 16

    Wrap C++ Class with cython, getting the basic example to work

  17. 17

    How to wrap or call a C function with void as return in Emscripten?

  18. 18

    Using SWIG and the Python/C API to wrap a function which returns a std::map

  19. 19

    Return c++ object (not a pointer preferably) in cython

  20. 20

    C a function that returns an array

  21. 21

    Wrap c++ function that needs function pointer

  22. 22

    Passing primitive pointers to C function from Cython

  23. 23

    return numpy array in cython defined c function

  24. 24

    Cython: calling C function throws 'undefined symbol'

  25. 25

    cython/python: how to catch as exception an external function that "exits" instead of "returns"

  26. 26

    cython/python: how to catch as exception an external function that "exits" instead of "returns"

  27. 27

    How do I declare a function that returns a pointer to a function that returns a function pointer without using a typedef in C?

  28. 28

    A way to pass c++ object to another object's method in cython

  29. 29

    A way to pass c++ object to another object's method in cython

HotTag

Archive