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

pass PyObject * to C function #90

Open
dlindsaye opened this issue Jan 30, 2023 · 0 comments
Open

pass PyObject * to C function #90

dlindsaye opened this issue Jan 30, 2023 · 0 comments

Comments

@dlindsaye
Copy link

Great job with this package!

I'm using Dl.dlopen from the ctypes.foreign package to load a collection of C functions which use the Python C API. As such, coming from OCaml and pyml, I need a way to call a C function and pass it a naked PyObject * . If I use Pycaml.pyunwrap_value I get a type error.

More specifically, I have:

Py.initialize ();
let globals = Py.Module.get_dict (Py.Module.main ()) in
let open Ctypes in
let c_set_item = Dl.(dlopen ~filename:"c_py_set_item.so" ~flags:[RTLD_NOW]) in
let my_set_item_int = Foreign.foreign ~from:c_set_item "my_set_item_int" (ptr void @-> string @-> int @-> returning void) in
let _ = my_set_item_int (Pycaml.pyunwrap_value globals) "my_variable" 4 in

and the following mundane C code:

#include <Python.h>

void
my_set_item_int(PyObject *obj, char *var_name, int n)
{
    PyObject *value = Py_BuildValue("%d", n);
    PyDict_SetItemString(obj, var_name, value);
}

This yields the error pyml_unwrap_value: type mismatch from within pyml_stubs.c as the extracted pointer is coming from the wrong type of Python capsule.

Any suggestions on how to get this to go through? Should I be using something other than Pycaml.pyunwrap_value?

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

1 participant