5 ms·
Cppyy – Automatic Python-C++ bindings
- funkythingsss 8y agothis looks awesome! Got to try it out on some of my code
- gshulegaard 8y agoThis is super interesting but if I am looking to bring C++ into my Python code base it would be because performance is important enough to do so. With that in mind I am not sure how this stacks up against compiled half steps like Cython or foreign function interfaces likes ctypes or CFFI instead.
- carlsonmark 8y agoIf you have a Python code base and are looking to speed up some part of it, Cython is the way to go. Pybind11 and cppyy are more for cases where you have a bunch of C++ code that you want to provide a Python interface for.
- ris 8y agoCython unfortunately doesn't have a very good pypy story, which cppyy does.
- arcticbull 8y agoCheck out the Rust python bindings too; specifically PyO3. If you're starting from scratch it's worth exploring. [1] [1] https://www.benfrederickson.com/writing-python-extensions-in-rust-using-pyo3/ https://www.benfrederickson.com/writing-python-extensions-in...
- malkia 8y agoThere is also the less known https://github.com/google/clif https://github.com/google/clif (but not automatic)
- hatred 8y agoDoes anyone know of any well known tools that generate bindings in other languages e.g., Python from C++ code? IIUC, this tool does the opposite i.e., generate C++/other language bindings from Python
- dangerbird2 8y agoThe entirety of cpython’s runtime is exposed as a C api, so you can easily manipulate python objects in c or any language with a ffi. boost::python provides a c++ wrapper for cpython, which can be used to call both c/c++ in python and vice versa
- adenner 8y agoHow does this compare to pybind11?
- carlsonmark 8y agoIf you have a C++ project, you want to have Python bindings, and you don't want to share your header files, pybind11 is the way to go. If you want to / can share your header files, cppyy is a very convenient way of being able to call your C++ code from Python. I'm not sure if there is a way to use it without headers.
- rightbyte 8y agoHow do you link any cpp binary without headers without guessing the calling convention?
- carlsonmark 8y agoIt's not so much that you never have the headers... more that you don't always want to provide the headers to everyone (since they may contain things you don't want to share.) I looked into it a bit further, and cppyy does have provisions for this: https://cppyy.readthedocs.io/en/latest/bindings_generation.html https://cppyy.readthedocs.io/en/latest/bindings_generation.h... I may have misread that page, but it looks like you can create a .rootmap and .pcm file and distribute that instead of the C++ headers.
- d3k 8y agoOne thing I wanted to do for a project at work is to add python bindings to our c++ library, having the least effort for having a decent python binding. Two features I am looking for are support for return values of custom objects of arbitrary size and c++ exceptions raising python exceptions. Cppyy seems to support these, do you know if/how they work on pybind11?
- deleted 8y ago[deleted]
- corysama 8y ago> PyPy 5.7 and 5.8 have a built-in module cppyy. Nice.
- jrussino 8y agoI need to create Python bindings for a C++ library and after a brief survey of the available options was planning on using SWIG (http://www.swig.org/tutorial.html http://www.swig.org/tutorial.html). How does this compare?
- mochomocha 8y agoSWIG is not always smooth to use, and you need to write interface files manually which in my experience are hard to debug. In Cppyy it looks like you don't have to do any of that.
- orbifold 8y agoUse pybind11 super easy to get startet with and reasonably easy to automate with libclang if your C++ API is simple.
- backslash_16 8y agoFrom the comments below, it sounds like interfacing my C++ code with python using Cppyy would sacrifice performance. Even so this will be awesome for unit testing. I really like the testing interface pytest provides, along with its parametrized tests. Anyone else using python to test the logic their C/C++ code? I can't think of how I would test the memory allocation and freeing portion. Anyone think it's a bad idea?
- rightbyte 8y agoThe fuzz converting data types back and forth is probably not worth it. But one can test the interface to the program nicely from python (i.e. text or socket i/o).
- kazinator 8y agoHow does this know the difference between: const char *cls::func(void) { return "abc"; // static: caller must not free } and const char *cls::func(void) { return strdup("abc"); // caller must free } Edit: I see: designed for large scale programs in high performance computing that use modern C++. Modern C++ meaning, no pointers anywhere.
- progval 8y agoIt looks like this gives you a `ctypes` pointer: https://cppyy.readthedocs.io/en/latest/basic_types.html#pointers https://cppyy.readthedocs.io/en/latest/basic_types.html#poin... So you handle it like any other ctypes pointer, ie. `libc = ctypes.CDLL(find_library("c")); libc.free(ptr)` Which is not fun in Python. So yeah, better avoid raw pointers.
- ttul 8y agoGoogle PyInline
- ttul 8y agohttp://pyinline.sourceforge.net/ http://pyinline.sourceforge.net/ 2001 vintage