[capi-sig] varargs in the C API.

Guillaume Yziquel guillaume.yziquel at citycable.ch
Sat Feb 20 02:57:33 CET 2010


Hello.

I've been trying to wrap up the C functions that have an ellipsis, or a 
vararg, such as PyTuple_Pack.

I tried to use avcall.h to pass a dynamically-determined number of 
arguments to the underlying function.

Having a hard time:

> ocaml -init ocamlinit
>         Objective Caml version 3.11.1
> 
> # let x = ((Python.of_int 6) :> Python.obj);;
> val x : Python.obj = <abstr>
> # let y = ((Python.of_int 4) :> Python.obj);;
> val y : Python.obj = <abstr>
> # let args = Python.tuple_pack [x; y];;
> [seldon:09731] *** Process received signal ***
> [seldon:09731] Signal: Segmentation fault (11)
> [seldon:09731] Signal code: Address not mapped (1)
> [seldon:09731] Failing at address: 0x5
> [seldon:09731] [ 0] /lib/libpthread.so.0 [0x2b9099aca990]
> [seldon:09731] [ 1] /usr/lib/libavcall.so(__builtin_avcall+0x2a5) [0x2b909a14c905]
> [seldon:09731] *** End of error message ***
> make: *** [test] Erreur de segmentation

So just wondering: has anyone succeeded in wrapping functions like 
PyTuple_Pack using stuff like avcall, libffi, or cinvoke?

Here's the code of my wrapper:

> CAMLprim value ocamlpython_py_tuple_pack (value ml_len, value ml_pyobjs) {
> 
>   av_alist argList;
>   PyObject * retVal;
>   av_start_ptr(argList, PyTuple_Pack, PyObject*, retVal);
> 
>   #if defined(__s390__) || defined(__hppa__) || defined(__cris__)
>   #define av_Py_ssize_t av_long
>   #else
>   #define av_Py_ssize_t av_int
>   #endif
> 
>   av_Py_ssize_t(argList, Pyoffset_val(ml_len));
>   while (ml_pyobjs != Val_emptylist) {
>     av_ptr(argList, PyObject*, Pyobj_val(Field(ml_pyobjs, 0)));
>     ml_pyobjs = Field(ml_pyobjs, 1);
>   }
> 
>   av_call(argList);
>   return(Val_owned_pyobj(retVal));
> }

Please tell me if you see something fishy that my eyes do not catch.

-- 
      Guillaume Yziquel
http://yziquel.homelinux.org/


More information about the capi-sig mailing list