Open elbaro opened 6 years ago
@elbaro, unfortunately, cgo
doesn't handle C functions that are variadic (IIRC).
so, essentially, one would need to implement Py_BuildValue
in terms of e.g. PyTuple_New
+ PyTuple_SET_ITEM
.
and, for this to work, one would need to also implement a parser for the Py_BuildValue(format)
string to infer the tuple size and the various types for the elements of that tuple:
that said that could be done. PR accepted :P
(note that, according to Python2 C-API Py_BuildValue
doesn't always return a slice, so this should also be taken care of...)
What about a hacky solution where there are a bunch of C wrapping functions with a fixed number of parameters. This code is obviously wrong, but it gets the idea across:
PyObject* Py_BuildValue_1(const char *format, PyObject* a1) {
return Py_BuildValue(format, a1);
}
PyObject* Py_BuildValue_2(const char *format, PyObject* a1, PyObject* a2) {
return Py_BuildValue(format, a1, a2);
}
PyObject* Py_BuildValue_3(const char *format, PyObject* a1, PyObject* a2, PyObject* a3) {
return Py_BuildValue(format, a1, a2, a3);
}
func Py_BuildValue(format string, args ...*PyObject) *PyObject {
switch len(args) {
case 1:
return C.Py_BuildValue_1(format, args[0])
case 2:
return C.Py_BuildValue_2(format, args[0], args[1])
case 3:
return C.Py_BuildValue_3(format, args[0], args[1], args[3])
default:
return Py_None
}
}
SGTM.
that's actually what I did there:
https://github.com/sbinet/go-python/blob/master/go-python.c#L16 for PyObject_CallFunction
Py_BuildValue
is not implemented.Example: f(7,8,9) =
should be possible with