Help needed with using SWIG wrapped code in Python

Vipul Raheja vipul.iiith at gmail.com
Mon Aug 15 10:08:33 CEST 2011


Hi,

I have wrapped a library from C++ to Python using SWIG. But I am facing
problems while importing and using it in Python.

$ python
>>> import pyossimtest
>>> import pyossim
>>> a = ["Image1.png","Image2.png"]
>>> b = pyossimtest.Info()
>>> b.initialize(len(a),a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pyossimtest.py", line 84, in initialize
    def initialize(self, *args): return _pyossimtest.Info_initialize(self,
*args)
TypeError: in method 'Info_initialize', argument 3 of type 'char *[]'

What does this error message imply? I have already handled char** as a
special case in swig using typemaps.


Here is the code excerpt from the swig-generated .cxx file:

SWIGINTERN PyObject *_wrap_Info_initialize(PyObject *SWIGUNUSEDPARM(self),
PyObject *args) {
  PyObject *resultobj = 0;
  pyossimtest::Info *arg1 = (pyossimtest::Info *) 0 ;
  int arg2 ;
  char **arg3 ;
  void *argp1 = 0 ;
  int res1 = 0 ;
  PyObject * obj0 = 0 ;
  PyObject * obj1 = 0 ;
  bool result;

  if (!PyArg_ParseTuple(args,(char *)"OO:Info_initialize",&obj0,&obj1))
SWIG_fail;
  res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_pyossimtest__Info, 0 |  0
);
  if (!SWIG_IsOK(res1)) {
    SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Info_initialize"
"', argument " "1"" of type '" "pyossimtest::Info *""'");
  }
  arg1 = reinterpret_cast< pyossimtest::Info * >(argp1);
  {
    int i;

    if (!PyList_Check(obj1))
    {
      PyErr_SetString(PyExc_ValueError, "Expecting a list");
      return NULL;
    }

    arg2 = PyList_Size(obj1);
    arg3 = (char **) malloc((arg2+1)*sizeof(char *));

    for (i = 0; i < arg2; i++)
    {
      PyObject *s = PyList_GetItem(obj1,i);
      if (!PyString_Check(s))
      {
        free(arg3);
        PyErr_SetString(PyExc_ValueError, "List items must be strings");
        return NULL;
      }
      arg3[i] = PyString_AsString(s);
    }
    arg3[i] = 0;
  }
  {
    try
    {
      result = (bool)(arg1)->initialize(arg2,arg3);
    }
    catch (const std::exception& e)
    {
      SWIG_exception(SWIG_RuntimeError, e.what());
    }
  }
  resultobj = SWIG_From_bool(static_cast< bool >(result));
  {
    if (arg3) free(arg3);
  }
  return resultobj;
fail:
  {
    if (arg3) free(arg3);
  }
  return NULL;
}


Kindly help.

Thanks and regards,
Vipul Raheja
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110815/fcb0b04f/attachment.html>


More information about the Python-list mailing list