4

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?

1
  • 1
    This is a good question, however (a) please improve the formatting, and (b) please go back to your previous questions and accept the answers so as to encourage people to answer this one. Commented Apr 10, 2012 at 4:00

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the reply, but I was able to do it in Cython in < 20 lines. If anyone reads this, I suggest to use Cython instead of SWIG if you only need Python support.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.