News for hackers. Stuff that matters.
Re:Interfacing Python and C: Advanced “ctypes” Features by fafhrd91 on Tuesday November 07, 2017 @10:54AM (#15644411)
why not rust? much simpler and safer with libs like https://github.com/PyO3/pyo3
Re:Interfacing Python and C: Advanced “ctypes” Features by chadcatlett on Tuesday November 07, 2017 @11:13AM (#15644569)
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 by fafhrd91 on Tuesday November 07, 2017 @11:25AM (#15644690)
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
Re:Interfacing Python and C: Advanced “ctypes” Features by acdha on Tuesday November 07, 2017 @11:43AM (#15644893)
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 other language and since it does a massive amount of computation the overhead of ctypes calling it is a small rounding error in the total processing time.

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 by fafhrd91 on Tuesday November 07, 2017 @11:46AM (#15644932)
Pyo3 support both. You can expose rust to python, and would look to python as native functions and classes or you can call python code from rust

Pyo3 uses rust refs ownership to enforce proper Gil usage. You code won’t compile if you use Gil in wrong way

Re:Interfacing Python and C: Advanced “ctypes” Features by orbifold on Tuesday November 07, 2017 @11:17AM (#15644606)
Because the library you are trying to use it is written in C / C++, which is more likely than that is written in rust.