Vanush Vee
Vanush Vee

Reputation: 454

SWIG, Python and Pointers

I'm trying to wrap a C API (static library) from which the source is not available. One of the first tasks in using the API is to set up the environment structure.

env_t * my_env = NULL;
result = env_setup(&my_env);

with declaration

error_code env_setup(env_t ** env);

Is it even possible to manufacture pointers of this type (env_t **) in SWIG, considering that the layout of the env structure is hidden?

Upvotes: 4

Views: 381

Answers (1)

ephemient
ephemient

Reputation: 204668

You may have to do some manipulation with typemaps.

%typemap(in) env_t ** (env_t *temp) %{
    temp = env_alloc();
    PyObject *iter = PyObject_GetIter($input);
    for (PyObject *item; (item = PyIter_Next(it));) {
        PyObject *key = PyObject_Str(item);
        PyObject *val = PyObject_GetItem($input, key);
        env_set(temp, PyString_AsString(key), PyString_AsString(val), ENV_OVERRIDE);
        Py_DECREF(val);
        Py_DECREF(key);
        Py_DECREF(item);
    }
    Py_DECREF(iter);
    $1 = &temp;
%}
%typemap(argout) env_t** %{
    PyObject *o = PyDict_New();
    for (char **val = env_array(*$1); *val; val++) {
        char *eq = strchr(*val, '=');
        *eq = '\0';
        PyMapping_SetItemString(o, *val, eq + 1);
    }
    env_free(*$1);
    $result = SWIG_Python_AppendOutput($result, o);
%}

Totally untested and lacking in error handling, but the intent here is to transform a dict into an env_t ** when calling from Python to C, and from env_t ** to dict when returning from C to Python.

Upvotes: 1

Related Questions