SWIG Python gives segfault on char * argument


Joined
Jun 6, 2010
Messages
1
Reaction score
0
I have a code written in C++ for a modified version of SRILM. The filenames are srilm.c and srilm.h . I wish to build the Python module _srilm.so from it so that I can test my module on python. The Makefile is as follows:
Code:
SRILM_LIBS=/cs/natlang-sw/packages/srilm-1.5.7/lib/x86_64-bit-generic
SRILM_INC=/cs/natlang-sw/packages/srilm-1.5.7/include
PYTHON_INC=/cs/natlang-sw/Linux-x86_64/NL/LANG/PYTHON/2.6.2/include/python2.6
PERL_INC=/usr/lib/perl5/5.8.0/i386-linux-thread-multi/CORE

python: clean _srilm.so

_srilm.so: srilm.o srilm_python_wrap.o
   g++ -shared $^ -loolm -ldstruct -lmisc -L$(SRILM_LIBS) -o [email protected]

srilm_python_wrap.o: srilm_python_wrap.c
   g++ -c -fpic $< -I/usr/local/include/ -I$(SRILM_INC) -I$(PYTHON_INC)

srilm_python_wrap.c: srilm_python.i
   swig -python $<
While compiling srilm_python_wrap.c, I get the following errors:
Code:
srilm_python_wrap.c: In function ‘int SWIG_Python_ConvertFunctionPtr(PyObject*, void**, swig_type_info*)’:
srilm_python_wrap.c:2034: error: invalid conversion from ‘const char*’ to ‘char*’
srilm_python_wrap.c: In function ‘int SWIG_AsCharPtrAndSize(PyObject*, char**, size_t*, int*)’:
srilm_python_wrap.c:2648: error: cannot convert ‘int*’ to ‘Py_ssize_t*’ for argument ‘3’ to ‘int PyString_AsStringAndSize(PyObject*, char**, Py_ssize_t*)’
srilm_python_wrap.c: In function ‘void SWIG_Python_FixMethods(PyMethodDef*, swig_const_info*, swig_type_info**, swig_type_info**)’:
srilm_python_wrap.c:3699: error: invalid conversion from ‘const char*’ to ‘char*’
make: *** [srilm_python_wrap.o] Error 1
To get around it I manually typecasted the arguments and executed the command to get _srilm.so
My test code in python looks like this:
Code:
#!/usr/bin/env python2.4

# Use the srilm module
from srilm import *

# Initialize a trigram LM variable (1 = unigram, 2 = bigram and so on)
n = initLM(5)

# Read 'sample.lm' into the LM variable
print "Loading the LM file ..."
#readLM(n, "sample.lm")
readLM(n, "/cs/natlang-data/wmt10/lm/eparl_nc_news_2m.en.lm")
print "Loading complete!!"
the readLM() function segfaults.
The corresponding code for srilm_python_wrap.c generated by swig is as follows:
Code:
SWIGINTERN PyObject *_wrap_readLM(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {
  PyObject *resultobj = 0;
  Ngram *arg1 = (Ngram *) 0 ;
  char *arg2 = (char  *) 0;
  int result;
  void *argp1 = 0 ;
  int res1 = 0 ;
  int res2 ;
  char *buf2 = 0;
  int alloc2 = 0 ;
  PyObject * obj0 = 0 ;
  PyObject * obj1 = 0 ;

  if (!PyArg_ParseTuple(args,(char *)"OO:readLM",&obj0,&obj1)) SWIG_fail;
  res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_Ngram, 0 |  0 );
  if (!SWIG_IsOK(res1)) {
    SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "readLM" "', argument " "1"" of type '" "Ngram *""'"); 
  }
  arg1 = (Ngram *)(argp1);
  res2 = SWIG_AsCharPtrAndSize(obj1, &buf2, NULL, &alloc2);
  if (!SWIG_IsOK(res2)) {
    SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "readLM" "', argument " "2"" of type '" "char const *""'");
  }
  arg2=buf2;
  result = (int)readLM(arg1,(char const *)arg2);
  resultobj = SWIG_From_int((int)(result));
  if (alloc2 == SWIG_NEWOBJ) free((char*)buf2);
  return resultobj;
fail:
  if (alloc2 == SWIG_NEWOBJ) free((char*)buf2);
  return NULL;
}
The problem seems to be the variable buf2 is corrupt. If I hardcode arg2 with some value, the code works just fine. Also, if I try to print buf2 or arg2, the code segfaults. Also, even if i write:
arg2 = (char *)buf2;
the code still segfaults.Basically, wherever I assign buf2 to something or try to print buf2, the code segfaults.Can someone help point out what is wrong here and how I might get the code to work?
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top