Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to pass a Python ctypes pointer to Julia then use it in a ccall? #868

Closed
marius311 opened this issue Dec 12, 2020 · 2 comments

Comments

@marius311
Copy link
Contributor

Basically I have some Python code which uses ctypes to wrap some Fortran structs, and on the Python side I can do a byref(some_wrapped_object) to get a pointer which I can then pass as an argument to some Fortran calls. Is there a way to pass the pointer to Julia then use that in a ccall instead? The object looks like:

In [1]: byref(some_wrapped_object)                                                                                                                  
Out[1]: <cparam 'P' (0x55a0ec981d80)>
@stevengj
Copy link
Member

stevengj commented Dec 12, 2020

Use ctypes.cast to get a c_void_p, which PyCall knows to convert to a Julia pointer. For example:

julia> ctypes = pyimport("ctypes");

julia> o = ctypes.byref(pycall(ctypes.c_int, PyObject, 3))
PyObject <cparam 'P' (0x7fa9929a3910)>

julia> ctypes.cast(o, ctypes.c_void_p)
Ptr{Nothing} @0x00007fa9929a3910

@marius311
Copy link
Contributor Author

Beautiful, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants