c++ - Segmentation fault in PyString_AsString function -
c++ code
//quicktest.cpp #include <python.h> #include <new> extern "c" { int test1(pyobject *src) { char *src1 = pystring_asstring(src); // <-- segmentation fault return 0; } }
python code
import ctypes test_lib = ctypes.cdll('./quicktest.so'); test_lib.test1("test") # <-- segmentation fault
there segmentation fault ("received sigserv") in pystring_asstring call.
linux 64 bit, python2.7
can explain, error in code?
[edit: code in question has changed]
the function pystring_asstring needs pointer pyobject , passing pointer char. python/c api reference manual
from ctypes documentation (calling functions):
byte strings , unicode strings passed pointer memory block contains data (char * or wchar_t *)
now, after changing question, improperly casting pointer char pointer pyobject when calling test1 function.
so function test1 receiving pointer char and, @ end of day, pystring_asstring receiving pointer char converted pointer pyobject wrongly.
to learn more: python c/api tutorial
Comments
Post a Comment