I want to call a c function in python code, and pass in a python dictionary as an argument.
little.c
#include <Python.h>
#include <stdio.h>
#include <stdlib.h>
PyObject *changeDict(PyObject *dict){
if(PyDict_Check(dict) == 1){
system("echo '==== is a dict ====' | systemd-cat -t 'little.c'");
}
// modify some value
return dict;
}
I use these commands to compile little.c:
gcc -g -fPIC -c little.c -I/usr/include/python2.7 -lpython2.7
gcc -shared little.o -o little.so
and move to LD_LIBRARY_PATH
mv little.so /usr/lib
little.py
from ctypes import *
mydll = PyDLL("little.so")
dic = {"status": 0}
dic = mydll.changeDict(dic)
print(dic)
python little.py
Then I got this error:
Traceback (most recent call last): File "little.py", line 13, in <module> dic = mydll.changeDict(dic) ctypes.ArgumentError: argument 1: <type 'exceptions.TypeError'>: Don't know how to convert parameter 1
Is it possible to pass a python dictionary directly to a C function?
CodePudding user response:
Always define .argtypes
and .restype
for your functions so ctypes
can type-check your parameters and know how to marshal them to and from C. py_object
is the type to use when passing a Python object directly.
Working example:
// test.c
#include <Python.h>
__declspec(dllexport) // for Windows exports
PyObject *changeDict(PyObject *dict) {
PyObject* value = PyUnicode_FromString("value");
PyDict_SetItemString(dict, "key", value); // Does not steal reference to value,
Py_DECREF(value); // so free this reference
Py_INCREF(dict); // because we're returning it...
return dict;
}
# test.py
from ctypes import *
# Use PyDLL when calling functions that use the Python API.
# It does not release the GIL during the call, which is required
# to use the Python API.
mydll = PyDLL('./test')
mydll.changeDict.argtypes = py_object, # Declare parameter type
mydll.changeDict.restype = py_object # and return value.
dic = {}
x = mydll.changeDict(dic)
print(x)
dic = {'key':2}
mydll.changeDict(dic) # Modified in-place, so really no need to return it.
print(dic)
Output:
{'key': 'value'}
{'key': 'value'}