By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,143 Members | 708 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,143 IT Pros & Developers. It's quick & easy.

SWIG Python gives segfault on char * argument

P: 2
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:

Expand|Select|Wrap|Line Numbers
  1. SRILM_LIBS=/cs/natlang-sw/packages/srilm-1.5.7/lib/x86_64-bit-generic
  2. SRILM_INC=/cs/natlang-sw/packages/srilm-1.5.7/include
  3. PYTHON_INC=/cs/natlang-sw/Linux-x86_64/NL/LANG/PYTHON/2.6.2/include/python2.6
  4. PERL_INC=/usr/lib/perl5/5.8.0/i386-linux-thread-multi/CORE
  5.  
  6. python: clean _srilm.so
  7.  
  8. _srilm.so: srilm.o srilm_python_wrap.o
  9.    g++ -shared $^ -loolm -ldstruct -lmisc -L$(SRILM_LIBS) -o $@
  10.  
  11. srilm_python_wrap.o: srilm_python_wrap.c
  12.    g++ -c -fpic $< -I/usr/local/include/ -I$(SRILM_INC) -I$(PYTHON_INC)
  13.  
  14. srilm_python_wrap.c: srilm_python.i
  15.    swig -python $
While compiling srilm_python_wrap.c, I get the following errors:
Expand|Select|Wrap|Line Numbers
  1. srilm_python_wrap.c: In function ‘int SWIG_Python_ConvertFunctionPtr(PyObject*, void**, swig_type_info*)’:
  2. srilm_python_wrap.c:2034: error: invalid conversion from ‘const char*’ to ‘char*’
  3. srilm_python_wrap.c: In function ‘int SWIG_AsCharPtrAndSize(PyObject*, char**, size_t*, int*)’:
  4. 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*)’
  5. srilm_python_wrap.c: In function ‘void SWIG_Python_FixMethods(PyMethodDef*, swig_const_info*, swig_type_info**, swig_type_info**)’:
  6. srilm_python_wrap.c:3699: error: invalid conversion from ‘const char*’ to ‘char*’
  7. 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:
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/env python2.4
  2.  
  3. # Use the srilm module
  4. from srilm import *
  5.  
  6. # Initialize a trigram LM variable (1 = unigram, 2 = bigram and so on)
  7. n = initLM(5)
  8.  
  9. # Read 'sample.lm' into the LM variable
  10. print "Loading the LM file ..."
  11. #readLM(n, "sample.lm")
  12. readLM(n, "/cs/natlang-data/wmt10/lm/eparl_nc_news_2m.en.lm")
  13. print "Loading complete!!"
the readLM() function segfaults.
The corresponding code for srilm_python_wrap.c generated by swig is as follows:
Expand|Select|Wrap|Line Numbers
  1. SWIGINTERN PyObject *_wrap_readLM(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {
  2.   PyObject *resultobj = 0;
  3.   Ngram *arg1 = (Ngram *) 0 ;
  4.   char *arg2 = (char  *) 0;
  5.   int result;
  6.   void *argp1 = 0 ;
  7.   int res1 = 0 ;
  8.   int res2 ;
  9.   char *buf2 = 0;
  10.   int alloc2 = 0 ;
  11.   PyObject * obj0 = 0 ;
  12.   PyObject * obj1 = 0 ;
  13.  
  14.   if (!PyArg_ParseTuple(args,(char *)"OO:readLM",&obj0,&obj1)) SWIG_fail;
  15.   res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_Ngram, 0 |  0 );
  16.   if (!SWIG_IsOK(res1)) {
  17.     SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "readLM" "', argument " "1"" of type '" "Ngram *""'"); 
  18.   }
  19.   arg1 = (Ngram *)(argp1);
  20.   res2 = SWIG_AsCharPtrAndSize(obj1, &buf2, NULL, &alloc2);
  21.   if (!SWIG_IsOK(res2)) {
  22.     SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "readLM" "', argument " "2"" of type '" "char const *""'");
  23.   }
  24.   arg2=buf2;
  25.   result = (int)readLM(arg1,(char const *)arg2);
  26.   resultobj = SWIG_From_int((int)(result));
  27.   if (alloc2 == SWIG_NEWOBJ) free((char*)buf2);
  28.   return resultobj;
  29. fail:
  30.   if (alloc2 == SWIG_NEWOBJ) free((char*)buf2);
  31.   return NULL;
  32. }
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:
Expand|Select|Wrap|Line Numbers
  1. 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?
Jun 6 '10 #1
Share this Question
Share on Google+
2 Replies

Expert 100+
P: 626
This is not the C forum but wouldn't you dereference arg2:
Expand|Select|Wrap|Line Numbers
  1. char *arg2 = (char  *) 0;
  2. //arg2=buf2;   // original statement
  3. arg2 = &buf2; 
Jun 6 '10 #2

P: 2
@dwblas: No, that is incorrect as both arg2 and buf are char *. I tried this, it's giving compilation error.
Jun 6 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.