Better error handling for PyCapsule_GetPointer

This commit is contained in:
Siu Kwan Lam 2012-09-14 19:28:21 -07:00
commit d77c3a61e1
2 changed files with 87 additions and 4 deletions

View file

@ -88,23 +88,26 @@ void *get_object_arg(PyObject *args)
if (!PyArg_ParseTuple(args, "O", &o))
return NULL;
return PyCapsule_GetPointer(o, NULL);
void *ptr = PyCapsule_GetPointer(o, NULL);
if ( !ptr ) return NULL;
else return ptr;
}
// must delete [] returned array
void **make_array_from_list(PyObject *list, int n)
{
void **arr = new void*[n] ;
if (!arr)
if (!arr){
PyErr_NoMemory();
return NULL;
}
int i;
for (i=0; i<n; i++) {
PyObject *e = PyList_GetItem(list, i);
if ( e == Py_None ) { // is None object?
arr[i] = NULL;
}
else { // otherwise, it must be a PyCapsule
} else { // otherwise, it must be a PyCapsule
void * ptr = PyCapsule_GetPointer(e, NULL);
if ( !ptr ) return NULL; // exception is raised
else arr[i] = ptr;