Interfacing Python and C: Advanced “ctypes” Features
11–18 of 18 posts
Re: Interfacing Python and C: Advanced “ctypes” Features
#12why not rust? much simpler and safer with libs like https://github.com/PyO3/pyo3
Re: Interfacing Python and C: Advanced “ctypes” Features
#13why not rust? much simpler and safer with libs like https://github.com/PyO3/pyo3
I might be wrong, but it seems PyO3 is more for going from Rust to Python, versus ctypes being for Python to C.
Re: Interfacing Python and C: Advanced “ctypes” Features
#14Re: Interfacing Python and C: Advanced “ctypes” Features
#15Why would you use CTypes instead of CFFI?
If all you need is a simple interface to a couple of functions and you want portability across system variants, that might be the path of least resistance and the performance is often negligible if the amount of work being done in the C code is significant.
Re: Interfacing Python and C: Advanced “ctypes” Features
#16Earlier quoted context omitted.
I might be wrong, but it seems PyO3 is more for going from Rust to Python, versus ctypes being for Python to C.
Sure, coupes is fine if do not need to write anything and ctypes is enough. But as soon as you need to make c to python bridge it safer to do in rust. Pyo3 handles ref counting and Gil management for you
Unless the PyO3 documentation is completely leaving out a major application, it doesn't support this scenario.
Re: Interfacing Python and C: Advanced “ctypes” Features
#17Re: Interfacing Python and C: Advanced “ctypes” Features
#18Earlier quoted context omitted.
Sure, coupes is fine if do not need to write anything and ctypes is enough. But as soon as you need to make c to python bridge it safer to do in rust. Pyo3 handles ref counting and Gil management for you
That sounds like the opposite direction: ctypes is for when you have a Python program which wants to call a C library, not when you want C code to call Python. As a concrete example, I work with JPEG 2000 imagery. I use ctypes so Python programs can load an image using a JP2 codec such as OpenJPEG to decode the image and save image tiles. Given the complexity of OpenJPEG, it would be a major project to port it to any…
Pyo3 uses rust refs ownership to enforce proper Gil usage. You code won’t compile if you use Gil in wrong way