Fixing segfault with PyArg_ParseTuple for strings

This commit is contained in:
AndrewBC 2011-06-12 15:51:26 -05:00
commit cb9fcd70cb
2 changed files with 498 additions and 439 deletions

View file

@ -67,8 +67,17 @@ _wLLVMModuleCreateWithName(PyObject *self, PyObject *args)
const char *s; const char *s;
LLVMModuleRef module; LLVMModuleRef module;
#ifdef LLVM_PY_USE_PYCAPSULE
PyObject *s_;
if (!PyArg_ParseTuple(args, "U:LLVMModuleCreateWithName", &s_)) {
return NULL;
}
s = PyUnicode_AS_DATA(s_);
#else
if (!PyArg_ParseTuple(args, "s", &s)) if (!PyArg_ParseTuple(args, "s", &s))
return NULL; return NULL;
#endif
module = LLVMModuleCreateWithName(s); module = LLVMModuleCreateWithName(s);
return ctor_LLVMModuleRef(module); return ctor_LLVMModuleRef(module);
@ -431,8 +440,17 @@ _wLLVMConstString(PyObject *self, PyObject *args)
int dont_null_terminate; int dont_null_terminate;
LLVMValueRef val; LLVMValueRef val;
#ifdef LLVM_PY_USE_PYCAPSULE
PyObject *s_;
if (!PyArg_ParseTuple(args, "Ui:LLVMConstString", &s_, &dont_null_terminate)) {
return NULL;
}
s = PyUnicode_AS_DATA(s_);
#else
if (!PyArg_ParseTuple(args, "si", &s, &dont_null_terminate)) if (!PyArg_ParseTuple(args, "si", &s, &dont_null_terminate))
return NULL; return NULL;
#endif
val = LLVMConstString(s, strlen(s), dont_null_terminate); val = LLVMConstString(s, strlen(s), dont_null_terminate);
return ctor_LLVMValueRef(val); return ctor_LLVMValueRef(val);
@ -648,8 +666,17 @@ _wLLVMBuildInvoke(PyObject *self, PyObject *args)
LLVMBasicBlockRef then_blk, catch_blk; LLVMBasicBlockRef then_blk, catch_blk;
LLVMValueRef inst; LLVMValueRef inst;
#ifdef LLVM_PY_USE_PYCAPSULE
PyObject *name_;
if (!PyArg_ParseTuple(args, "OOOOOU:LLVMBuildInvoke", &obj1, &obj2, &obj3, &obj4, &obj5, &name_)) {
return NULL;
}
name = PyUnicode_AS_DATA(name_);
#else
if (!PyArg_ParseTuple(args, "OOOOOs", &obj1, &obj2, &obj3, &obj4, &obj5, &name)) if (!PyArg_ParseTuple(args, "OOOOOs", &obj1, &obj2, &obj3, &obj4, &obj5, &name))
return NULL; return NULL;
#endif
#ifdef LLVM_PY_USE_PYCAPSULE #ifdef LLVM_PY_USE_PYCAPSULE
builder = (LLVMBuilderRef) PyCapsule_GetPointer(obj1, NULL); builder = (LLVMBuilderRef) PyCapsule_GetPointer(obj1, NULL);
@ -764,8 +791,17 @@ _wLLVMCreateMemoryBufferWithContentsOfFile(PyObject *self, PyObject *args)
char *outmsg; char *outmsg;
PyObject *ret; PyObject *ret;
#ifdef LLVM_PY_USE_PYCAPSULE
PyObject *path_;
if (!PyArg_ParseTuple(args, "U:LLVMCreateMemoryBufferWithContentsOfFile", &path_)) {
return NULL;
}
path = PyUnicode_AS_DATA(path_);
#else
if (!PyArg_ParseTuple(args, "s", &path)) if (!PyArg_ParseTuple(args, "s", &path))
return NULL; return NULL;
#endif
if (!LLVMCreateMemoryBufferWithContentsOfFile(path, &ref, &outmsg)) { if (!LLVMCreateMemoryBufferWithContentsOfFile(path, &ref, &outmsg)) {
ret = ctor_LLVMMemoryBufferRef(ref); ret = ctor_LLVMMemoryBufferRef(ref);
@ -1217,14 +1253,22 @@ _wrap_objintlist2obj(LLVMGetIntrinsic, LLVMModuleRef, LLVMTypeRef,
static PyObject * static PyObject *
_wLLVMLoadLibraryPermanently(PyObject *self, PyObject *args) _wLLVMLoadLibraryPermanently(PyObject *self, PyObject *args)
{ {
// XXX TODO: Figure out why this is segfaulting
const char *filename; const char *filename;
char *outmsg; char *outmsg;
PyObject *ret; PyObject *ret;
#ifdef LLVM_PY_USE_PYCAPSULE
PyObject *filename_;
if (!PyArg_ParseTuple(args, "U:LLVMLoadLibraryPermanently", &filename_)) {
return NULL;
}
filename = PyUnicode_AS_DATA(filename_);
#else
if (!PyArg_ParseTuple(args, "s", &filename)) { if (!PyArg_ParseTuple(args, "s", &filename)) {
return NULL; return NULL;
} }
#endif
outmsg = 0; outmsg = 0;
if (!LLVMLoadLibraryPermanently(filename, &outmsg)) { if (!LLVMLoadLibraryPermanently(filename, &outmsg)) {

View file

@ -183,6 +183,7 @@ _w ## func (PyObject *self, PyObject *args) \
\ \
if (!PyArg_ParseTuple(args, "OO", &obj1, &obj2)) \ if (!PyArg_ParseTuple(args, "OO", &obj1, &obj2)) \
return NULL; \ return NULL; \
\
arg1 = ( intype1 ) PyCapsule_GetPointer(obj1, NULL); \ arg1 = ( intype1 ) PyCapsule_GetPointer(obj1, NULL); \
arg2 = ( intype2 ) PyCapsule_GetPointer(obj2, NULL); \ arg2 = ( intype2 ) PyCapsule_GetPointer(obj2, NULL); \
\ \
@ -203,6 +204,7 @@ _w ## func (PyObject *self, PyObject *args) \
\ \
if (!PyArg_ParseTuple(args, "OO", &obj1, &obj2)) \ if (!PyArg_ParseTuple(args, "OO", &obj1, &obj2)) \
return NULL; \ return NULL; \
\
arg1 = ( intype1 ) PyCapsule_GetPointer(obj1, NULL);\ arg1 = ( intype1 ) PyCapsule_GetPointer(obj1, NULL);\
arg2 = ( intype2 ) PyCapsule_GetPointer(obj2, NULL);\ arg2 = ( intype2 ) PyCapsule_GetPointer(obj2, NULL);\
\ \
@ -224,6 +226,7 @@ _w ## func (PyObject *self, PyObject *args) \
\ \
if (!PyArg_ParseTuple(args, "OI", &obj1, &arg2)) \ if (!PyArg_ParseTuple(args, "OI", &obj1, &arg2)) \
return NULL; \ return NULL; \
\
arg1 = ( intype1 ) PyCapsule_GetPointer(obj1, NULL);\ arg1 = ( intype1 ) PyCapsule_GetPointer(obj1, NULL);\
\ \
return ctor_ ## outtype ( func (arg1, arg2)); \ return ctor_ ## outtype ( func (arg1, arg2)); \
@ -328,11 +331,14 @@ _w ## func (PyObject *self, PyObject *args) \
static PyObject * \ static PyObject * \
_w ## func (PyObject *self, PyObject *args) \ _w ## func (PyObject *self, PyObject *args) \
{ \ { \
PyObject *arg1_; \
const char *arg1; \ const char *arg1; \
\ \
if (!PyArg_ParseTuple(args, "s", &arg1)) \ if (!PyArg_ParseTuple(args, "U", &arg1_)) \
return NULL; \ return NULL; \
\ \
arg1 = PyUnicode_AS_DATA(arg1_); \
\
return ctor_ ## outtype ( func (arg1)); \ return ctor_ ## outtype ( func (arg1)); \
} }
@ -391,14 +397,15 @@ _w ## func (PyObject *self, PyObject *args) \
static PyObject * \ static PyObject * \
_w ## func (PyObject *self, PyObject *args) \ _w ## func (PyObject *self, PyObject *args) \
{ \ { \
PyObject *obj1; \ PyObject *obj1, *arg2_; \
const char *arg2; \ const char *arg2; \
intype1 arg1; \ intype1 arg1; \
\ \
if (!PyArg_ParseTuple(args, "Os", &obj1, &arg2)) \ if (!PyArg_ParseTuple(args, "OU", &obj1, &arg2_)) \
return NULL; \ return NULL; \
\ \
arg1 = ( intype1 ) PyCapsule_GetPointer(obj1, NULL);\ arg1 = ( intype1 ) PyCapsule_GetPointer(obj1, NULL);\
arg2 = PyUnicode_AS_DATA(arg2_); \
\ \
func (arg1, arg2); \ func (arg1, arg2); \
Py_RETURN_NONE; \ Py_RETURN_NONE; \
@ -412,14 +419,15 @@ _w ## func (PyObject *self, PyObject *args) \
static PyObject * \ static PyObject * \
_w ## func (PyObject *self, PyObject *args) \ _w ## func (PyObject *self, PyObject *args) \
{ \ { \
PyObject *obj1; \ PyObject *obj1, *arg2_; \
const char *arg2; \ const char *arg2; \
intype1 arg1; \ intype1 arg1; \
\ \
if (!PyArg_ParseTuple(args, "Os", &obj1, &arg2)) \ if (!PyArg_ParseTuple(args, "OU", &obj1, &arg2_)) \
return NULL; \ return NULL; \
\ \
arg1 = ( intype1 ) PyCapsule_GetPointer(obj1, NULL);\ arg1 = ( intype1 ) PyCapsule_GetPointer(obj1, NULL);\
arg2 = PyUnicode_AS_DATA(arg2_); \
\ \
return ctor_ ## outtype ( func (arg1, arg2)); \ return ctor_ ## outtype ( func (arg1, arg2)); \
} }
@ -474,16 +482,17 @@ _w ## func (PyObject *self, PyObject *args) \
static PyObject * \ static PyObject * \
_w ## func (PyObject *self, PyObject *args) \ _w ## func (PyObject *self, PyObject *args) \
{ \ { \
PyObject *obj1, *obj2; \ PyObject *obj1, *obj2, *arg3_; \
intype1 arg1; \ intype1 arg1; \
intype2 arg2; \ intype2 arg2; \
const char *arg3; \ const char *arg3; \
\ \
if (!PyArg_ParseTuple(args, "OOs", &obj1, &obj2, &arg3)) \ if (!PyArg_ParseTuple(args, "OOU", &obj1, &obj2, &arg3_)) \
return NULL; \ return NULL; \
\ \
arg1 = ( intype1 ) PyCapsule_GetPointer(obj1, NULL); \ arg1 = ( intype1 ) PyCapsule_GetPointer(obj1, NULL); \
arg2 = ( intype2 ) PyCapsule_GetPointer(obj2, NULL); \ arg2 = ( intype2 ) PyCapsule_GetPointer(obj2, NULL); \
arg3 = PyUnicode_AS_DATA(arg3_); \
\ \
return ctor_ ## outtype ( func (arg1, arg2, arg3)); \ return ctor_ ## outtype ( func (arg1, arg2, arg3)); \
} }
@ -583,15 +592,16 @@ _w ## func (PyObject *self, PyObject *args) \
static PyObject * \ static PyObject * \
_w ## func (PyObject *self, PyObject *args) \ _w ## func (PyObject *self, PyObject *args) \
{ \ { \
PyObject *obj1, *obj3; \ PyObject *obj1, *obj3, *arg2_; \
intype1 arg1; \ intype1 arg1; \
const char *arg2; \ const char *arg2; \
intype3 arg3; \ intype3 arg3; \
\ \
if (!PyArg_ParseTuple(args, "OsO", &obj1, &arg2, &obj3)) \ if (!PyArg_ParseTuple(args, "OUO", &obj1, &arg2_, &obj3)) \
return NULL; \ return NULL; \
\ \
arg1 = ( intype1 ) PyCapsule_GetPointer(obj1, NULL); \ arg1 = ( intype1 ) PyCapsule_GetPointer(obj1, NULL); \
arg2 = PyUnicode_AS_DATA(arg2_); \
arg3 = ( intype3 ) PyCapsule_GetPointer(obj3, NULL); \ arg3 = ( intype3 ) PyCapsule_GetPointer(obj3, NULL); \
\ \
return ctor_ ## outtype ( func (arg1, arg2, arg3)); \ return ctor_ ## outtype ( func (arg1, arg2, arg3)); \
@ -606,17 +616,18 @@ _w ## func (PyObject *self, PyObject *args) \
static PyObject * \ static PyObject * \
_w ## func (PyObject *self, PyObject *args) \ _w ## func (PyObject *self, PyObject *args) \
{ \ { \
PyObject *obj1, *obj2; \ PyObject *obj1, *obj2, *arg4_; \
intype1 arg1; \ intype1 arg1; \
intype2 arg2; \ intype2 arg2; \
int arg3; \ int arg3; \
const char *arg4; \ const char *arg4; \
\ \
if (!PyArg_ParseTuple(args, "OOis", &obj1, &obj2, &arg3, &arg4)) \ if (!PyArg_ParseTuple(args, "OOiU", &obj1, &obj2, &arg3, &arg4_)) \
return NULL; \ return NULL; \
\ \
arg1 = ( intype1 ) PyCapsule_GetPointer(obj1, NULL); \ arg1 = ( intype1 ) PyCapsule_GetPointer(obj1, NULL); \
arg2 = ( intype2 ) PyCapsule_GetPointer(obj2, NULL); \ arg2 = ( intype2 ) PyCapsule_GetPointer(obj2, NULL); \
arg4 = PyUnicode_AS_DATA(arg4_); \
\ \
return ctor_ ## outtype ( func (arg1, arg2, arg3, arg4)) ; \ return ctor_ ## outtype ( func (arg1, arg2, arg3, arg4)) ; \
} }
@ -629,18 +640,19 @@ _w ## func (PyObject *self, PyObject *args) \
static PyObject * \ static PyObject * \
_w ## func (PyObject *self, PyObject *args) \ _w ## func (PyObject *self, PyObject *args) \
{ \ { \
PyObject *obj1, *obj2, *obj3; \ PyObject *obj1, *obj2, *obj3, *arg4_; \
intype1 arg1; \ intype1 arg1; \
intype2 arg2; \ intype2 arg2; \
intype3 arg3; \ intype3 arg3; \
const char *arg4; \ const char *arg4; \
\ \
if (!PyArg_ParseTuple(args, "OOOs", &obj1, &obj2, &obj3, &arg4)) \ if (!PyArg_ParseTuple(args, "OOOU", &obj1, &obj2, &obj3, &arg4_)) \
return NULL; \ return NULL; \
\ \
arg1 = ( intype1 ) PyCapsule_GetPointer(obj1, NULL); \ arg1 = ( intype1 ) PyCapsule_GetPointer(obj1, NULL); \
arg2 = ( intype2 ) PyCapsule_GetPointer(obj2, NULL); \ arg2 = ( intype2 ) PyCapsule_GetPointer(obj2, NULL); \
arg3 = ( intype3 ) PyCapsule_GetPointer(obj3, NULL); \ arg3 = ( intype3 ) PyCapsule_GetPointer(obj3, NULL); \
arg4 = PyUnicode_AS_DATA(arg4_); \
\ \
return ctor_ ## outtype ( func (arg1, arg2, arg3, arg4)); \ return ctor_ ## outtype ( func (arg1, arg2, arg3, arg4)); \
} }
@ -702,20 +714,21 @@ _w ## func (PyObject *self, PyObject *args) \
static PyObject * \ static PyObject * \
_w ## func (PyObject *self, PyObject *args) \ _w ## func (PyObject *self, PyObject *args) \
{ \ { \
PyObject *obj1, *obj2, *obj3, *obj4; \ PyObject *obj1, *obj2, *obj3, *obj4, *arg5_; \
intype1 arg1; \ intype1 arg1; \
intype2 arg2; \ intype2 arg2; \
intype3 arg3; \ intype3 arg3; \
intype4 arg4; \ intype4 arg4; \
const char *arg5; \ const char *arg5; \
\ \
if (!PyArg_ParseTuple(args, "OOOOs", &obj1, &obj2, &obj3, &obj4, &arg5)) \ if (!PyArg_ParseTuple(args, "OOOOU", &obj1, &obj2, &obj3, &obj4, &arg5_)) \
return NULL; \ return NULL; \
\ \
arg1 = ( intype1 ) PyCapsule_GetPointer(obj1, NULL); \ arg1 = ( intype1 ) PyCapsule_GetPointer(obj1, NULL); \
arg2 = ( intype2 ) PyCapsule_GetPointer(obj2, NULL); \ arg2 = ( intype2 ) PyCapsule_GetPointer(obj2, NULL); \
arg3 = ( intype3 ) PyCapsule_GetPointer(obj3, NULL); \ arg3 = ( intype3 ) PyCapsule_GetPointer(obj3, NULL); \
arg4 = ( intype4 ) PyCapsule_GetPointer(obj4, NULL); \ arg4 = ( intype4 ) PyCapsule_GetPointer(obj4, NULL); \
arg5 = PyUnicode_AS_DATA(arg5_); \
\ \
return ctor_ ## outtype ( func (arg1, arg2, arg3, arg4, arg5)); \ return ctor_ ## outtype ( func (arg1, arg2, arg3, arg4, arg5)); \
} }
@ -728,19 +741,20 @@ _w ## func (PyObject *self, PyObject *args) \
static PyObject * \ static PyObject * \
_w ## func (PyObject *self, PyObject *args) \ _w ## func (PyObject *self, PyObject *args) \
{ \ { \
PyObject *obj1, *obj3, *obj4; \ PyObject *obj1, *obj3, *obj4, *arg5_; \
intype1 arg1; \ intype1 arg1; \
intype2 arg2; \ intype2 arg2; \
intype3 arg3; \ intype3 arg3; \
intype4 arg4; \ intype4 arg4; \
const char *arg5; \ const char *arg5; \
\ \
if (!PyArg_ParseTuple(args, "OiOOs", &obj1, &arg2, &obj3, &obj4, &arg5)) \ if (!PyArg_ParseTuple(args, "OiOOU", &obj1, &arg2, &obj3, &obj4, &arg5_)) \
return NULL; \ return NULL; \
\ \
arg1 = ( intype1 ) PyCapsule_GetPointer(obj1, NULL); \ arg1 = ( intype1 ) PyCapsule_GetPointer(obj1, NULL); \
arg3 = ( intype3 ) PyCapsule_GetPointer(obj3, NULL); \ arg3 = ( intype3 ) PyCapsule_GetPointer(obj3, NULL); \
arg4 = ( intype4 ) PyCapsule_GetPointer(obj4, NULL); \ arg4 = ( intype4 ) PyCapsule_GetPointer(obj4, NULL); \
arg5 = PyUnicode_AS_DATA(arg5_); \
\ \
return ctor_ ## outtype ( func (arg1, arg2, arg3, arg4, arg5)); \ return ctor_ ## outtype ( func (arg1, arg2, arg3, arg4, arg5)); \
} }
@ -923,7 +937,7 @@ _w ## func (PyObject *self, PyObject *args) \
static PyObject * \ static PyObject * \
_w ## func (PyObject *self, PyObject *args) \ _w ## func (PyObject *self, PyObject *args) \
{ \ { \
PyObject *obj1, *obj2, *obj3; \ PyObject *obj1, *obj2, *obj3, *arg4_; \
const char *arg4; \ const char *arg4; \
intype1 arg1; \ intype1 arg1; \
intype2 arg2; \ intype2 arg2; \
@ -931,12 +945,13 @@ _w ## func (PyObject *self, PyObject *args) \
unsigned arg3n; \ unsigned arg3n; \
outtype ret; \ outtype ret; \
\ \
if (!PyArg_ParseTuple(args, "OOOs", &obj1, &obj2, &obj3, &arg4))\ if (!PyArg_ParseTuple(args, "OOOU", &obj1, &obj2, &obj3, &arg4_)) \
return NULL; \ return NULL; \
\ \
arg1 = ( intype1 ) PyCapsule_GetPointer(obj1, NULL); \ arg1 = ( intype1 ) PyCapsule_GetPointer(obj1, NULL); \
arg2 = ( intype2 ) PyCapsule_GetPointer(obj2, NULL); \ arg2 = ( intype2 ) PyCapsule_GetPointer(obj2, NULL); \
arg3n = ( unsigned ) PyList_Size(obj3); \ arg3n = ( unsigned ) PyList_Size(obj3); \
arg4 = PyUnicode_AS_DATA(arg4_); \
if (!(arg3v = ( intype3 *)make_array_from_list(obj3, arg3n))) \ if (!(arg3v = ( intype3 *)make_array_from_list(obj3, arg3n))) \
return PyErr_NoMemory(); \ return PyErr_NoMemory(); \
\ \