How to expose a C++ class to Python without building a module -
i want know if there way expose c++ class python without building intermediate shared library.
here desirable scenario. example have following c++ class:
class toto { public: toto(int ivalue1_, int ivalue2_): ivalue1(ivalue1_), ivalue2(ivalue2_) {} int addition(void) const {if (!this) return 0; return ivalue1 + ivalue2;} private: int ivalue1; int ivalue2; };
i convert somehow class (or intance) pyobject* in order send paremter (args) example pyobject_callobject:
pyobject* pyobject_callobject(pyobject* wrapperfunction, pyobject* args)
in other hand in python side, i'll have wrapperfunction gets pointer on c++ class (or instance) parameter , calls methods or uses properties:
def wrapper_function(cplusplusclass): instance = cplusplusclass(4, 5) result = instance.addition()
as can see, don't need/want have separate shared library or build module boost python. need find way convert c++ code pyobject , send python. cannot find way c python libraries, boost or swig.
do have idea? help.
as far know, there no easy way accomplish this.
to extend python c++ neither module nor intermediate library, require dynamically loading library, importing functions. approach used ctypes
module. accomplish same c++, 1 need write ctypes
-like library understood c++ abi target compiler(s).
to extend python without introducing module, intermediate library created provided c api wraps c++ library. intermediate library used in python through ctypes
. while not provide exact calling syntax , introduce intermediate library, less effort building ctypes
-like library interface directly c++.
however, if intermediate library going introduced, may worthwhile use boost.python, swig, or other c++/python language binding tool. while many of these tools introduce extension via module, provide cleaner calling conventions, better error checking in binding process, , may easier maintain.
Comments
Post a Comment