struct ccn_parsed_interest * _pyccn_interest_get_pi(PyObject *py_interest) { struct interest_data *context; struct ccn_charbuf *interest; int r; assert(CCNObject_IsValid(INTEREST, py_interest)); context = PyCapsule_GetContext(py_interest); assert(context); if (context->pi) return context->pi; interest = CCNObject_Get(INTEREST, py_interest); context->pi = calloc(1, sizeof(struct ccn_parsed_interest)); JUMP_IF_NULL_MEM(context->pi, error); /* TODO: we should also use the comps argument */ r = ccn_parse_interest(interest->buf, interest->length, context->pi, NULL); if (r < 0) { PyErr_SetString(g_PyExc_CCNInterestError, "Unable to parse the" " Interest"); goto error; } assert(context->pi); return context->pi; error: return NULL; }
static CapsuleContext* GetContext(PyObject *obj) { void* context = PyCapsule_GetContext(obj); if (!context) { PyErr_SetString(PyExc_TypeError, "PyCapsule has no context."); return NULL; } return static_cast<CapsuleContext*>(context); }
static void cleanup_convert(PyObject *self) { typedef int (*destr_t)(PyObject *, void *); destr_t destr = (destr_t)PyCapsule_GetContext(self); void *ptr = PyCapsule_GetPointer(self, GETARGS_CAPSULE_NAME_CLEANUP_CONVERT); if (ptr && destr) destr(NULL, ptr); }
static CapsuleContext* getContext(PyObject* self, PyObject* args) { PyObject* obj; if (!PyArg_ParseTuple(args, "O", &obj)) { return NULL; } void* context = PyCapsule_GetContext(obj); if (!context) { PyErr_SetString(PyExc_TypeError, "PyCapsule has no context."); return NULL; } return static_cast<CapsuleContext*>(context); }
void _pyccn_interest_set_pi(PyObject *py_interest, struct ccn_parsed_interest *pi) { struct interest_data *context; assert(CCNObject_IsValid(INTEREST, py_interest)); context = PyCapsule_GetContext(py_interest); assert(context); if (context->pi) free(context->pi); context->pi = pi; }
void _pyccn_content_object_set_comps(PyObject *py_content_object, struct ccn_indexbuf *comps) { struct content_object_data *context; assert(CCNObject_IsValid(CONTENT_OBJECT, py_content_object)); context = PyCapsule_GetContext(py_content_object); assert(context); if (context->comps) free(context->comps); context->comps = comps; }
void _pyccn_content_object_set_pco(PyObject *py_content_object, struct ccn_parsed_ContentObject *pco) { struct content_object_data *context; assert(CCNObject_IsValid(CONTENT_OBJECT, py_content_object)); context = PyCapsule_GetContext(py_content_object); assert(context); if (context->pco) free(context->pco); context->pco = pco; }
static int parse_ContentObject(PyObject *py_content_object) { struct content_object_data *context; struct ccn_charbuf *content_object; int r; assert(CCNObject_IsValid(CONTENT_OBJECT, py_content_object)); context = PyCapsule_GetContext(py_content_object); assert(context); if (context->pco) free(context->pco); ccn_indexbuf_destroy(&context->comps); /* * no error happens between deallocation and following line, so I'm not * setting context->pco to NULL */ context->pco = calloc(1, sizeof(struct ccn_parsed_ContentObject)); JUMP_IF_NULL_MEM(context->pco, error); context->comps = ccn_indexbuf_create(); JUMP_IF_NULL_MEM(context->comps, error); content_object = CCNObject_Get(CONTENT_OBJECT, py_content_object); r = ccn_parse_ContentObject(content_object->buf, content_object->length, context->pco, context->comps); if (r < 0) { PyErr_SetString(g_PyExc_CCNContentObjectError, "Unable to parse the" " ContentObject"); goto error; } return 0; error: if (context->pco) { free(context->pco); context->pco = NULL; } ccn_indexbuf_destroy(&context->comps); return -1; }
struct ccn_indexbuf * _pyccn_content_object_get_comps(PyObject *py_content_object) { struct content_object_data *context; int r; assert(CCNObject_IsValid(CONTENT_OBJECT, py_content_object)); context = PyCapsule_GetContext(py_content_object); assert(context); if (context->comps) return context->comps; r = parse_ContentObject(py_content_object); JUMP_IF_NEG(r, error); assert(context->comps); return context->comps; error: return NULL; }
static void array_dtor(PyObject *capsule) { delete_array(PyCapsule_GetPointer(capsule, 0), reinterpret_cast<const sipTypeDef *>( PyCapsule_GetContext(capsule))); }
static void pyccn_Capsule_Destructor(PyObject *capsule) { const char *name; void *pointer; enum _pyccn_capsules type; assert(PyCapsule_CheckExact(capsule)); name = PyCapsule_GetName(capsule); type = name2type(name); pointer = PyCapsule_GetPointer(capsule, name); assert(pointer); switch (type) { case CLOSURE: { PyObject *py_obj_closure; struct ccn_closure *p = pointer; py_obj_closure = PyCapsule_GetContext(capsule); assert(py_obj_closure); Py_DECREF(py_obj_closure); /* No longer referencing Closure object */ /* If we store something else, than ourselves, it probably is a bug */ assert(capsule == p->data); free(p); } break; case CONTENT_OBJECT: { struct content_object_data *context; struct ccn_charbuf *p = pointer; context = PyCapsule_GetContext(capsule); if (context) { if (context->pco) free(context->pco); ccn_indexbuf_destroy(&context->comps); free(context); } ccn_charbuf_destroy(&p); } break; case HANDLE: { struct ccn *p = pointer; ccn_disconnect(p); ccn_destroy(&p); } break; case INTEREST: { struct interest_data *context; struct ccn_charbuf *p = pointer; context = PyCapsule_GetContext(capsule); if (context) { if (context->pi) free(context->pi); free(context); } ccn_charbuf_destroy(&p); } break; case PKEY_PRIV: case PKEY_PUB: { struct ccn_pkey *p = pointer; ccn_pubkey_free(p); } break; case EXCLUSION_FILTER: case KEY_LOCATOR: case NAME: case SIGNATURE: case SIGNED_INFO: { struct ccn_charbuf *p = pointer; ccn_charbuf_destroy(&p); } break; case SIGNING_PARAMS: { struct ccn_signing_params *p = pointer; if (p->template_ccnb) ccn_charbuf_destroy(&p->template_ccnb); free(p); } break; default: debug("Got capsule: %s\n", PyCapsule_GetName(capsule)); panic("Unable to destroy the object: got an unknown capsule"); } }
static PyObject *Py_GeometricTransform(PyObject *obj, PyObject *args) { PyArrayObject *input = NULL, *output = NULL; PyArrayObject *coordinates = NULL, *matrix = NULL, *shift = NULL; PyObject *fnc = NULL, *extra_arguments = NULL, *extra_keywords = NULL; int mode, order; double cval; void *func = NULL, *data = NULL; NI_PythonCallbackData cbdata; ccallback_t callback; static ccallback_signature_t callback_signatures[] = { {"int (intptr_t *, double *, int, int, void *)"}, {"int (npy_intp *, double *, int, int, void *)"}, #if NPY_SIZEOF_INTP == NPY_SIZEOF_SHORT {"int (short *, double *, int, int, void *)"}, #endif #if NPY_SIZEOF_INTP == NPY_SIZEOF_INT {"int (int *, double *, int, int, void *)"}, #endif #if NPY_SIZEOF_INTP == NPY_SIZEOF_LONG {"int (long *, double *, int, int, void *)"}, #endif #if NPY_SIZEOF_INTP == NPY_SIZEOF_LONGLONG {"int (long long *, double *, int, int, void *)"}, #endif {NULL} }; callback.py_function = NULL; callback.c_function = NULL; if (!PyArg_ParseTuple(args, "O&OO&O&O&O&iidOO", NI_ObjectToInputArray, &input, &fnc, NI_ObjectToOptionalInputArray, &coordinates, NI_ObjectToOptionalInputArray, &matrix, NI_ObjectToOptionalInputArray, &shift, NI_ObjectToOutputArray, &output, &order, &mode, &cval, &extra_arguments, &extra_keywords)) goto exit; if (fnc != Py_None) { if (!PyTuple_Check(extra_arguments)) { PyErr_SetString(PyExc_RuntimeError, "extra_arguments must be a tuple"); goto exit; } if (!PyDict_Check(extra_keywords)) { PyErr_SetString(PyExc_RuntimeError, "extra_keywords must be a dictionary"); goto exit; } if (PyCapsule_CheckExact(fnc) && PyCapsule_GetName(fnc) == NULL) { func = PyCapsule_GetPointer(fnc, NULL); data = PyCapsule_GetContext(fnc); #if PY_VERSION_HEX < 0x03000000 } else if (PyCObject_Check(fnc)) { /* 'Legacy' low-level callable on Py2 */ func = PyCObject_AsVoidPtr(fnc); data = PyCObject_GetDesc(fnc); #endif } else { int ret; ret = ccallback_prepare(&callback, callback_signatures, fnc, CCALLBACK_DEFAULTS); if (ret == -1) { goto exit; } if (callback.py_function != NULL) { cbdata.extra_arguments = extra_arguments; cbdata.extra_keywords = extra_keywords; callback.info_p = (void*)&cbdata; func = Py_Map; data = (void*)&callback; } else { func = callback.c_function; data = callback.user_data; } } } NI_GeometricTransform(input, func, data, matrix, shift, coordinates, output, order, (NI_ExtendMode)mode, cval); #ifdef HAVE_WRITEBACKIFCOPY PyArray_ResolveWritebackIfCopy(output); #endif exit: if (callback.py_function != NULL || callback.c_function != NULL) { ccallback_release(&callback); } Py_XDECREF(input); Py_XDECREF(output); Py_XDECREF(coordinates); Py_XDECREF(matrix); Py_XDECREF(shift); return PyErr_Occurred() ? NULL : Py_BuildValue(""); }
static PyObject *Py_GenericFilter(PyObject *obj, PyObject *args) { PyArrayObject *input = NULL, *output = NULL, *footprint = NULL; PyObject *fnc = NULL, *extra_arguments = NULL, *extra_keywords = NULL; void *func = NULL, *data = NULL; NI_PythonCallbackData cbdata; int mode; PyArray_Dims origin; double cval; ccallback_t callback; static ccallback_signature_t callback_signatures[] = { {"int (double *, intptr_t, double *, void *)"}, {"int (double *, npy_intp, double *, void *)"}, #if NPY_SIZEOF_INTP == NPY_SIZEOF_SHORT {"int (double *, short, double *, void *)"}, #endif #if NPY_SIZEOF_INTP == NPY_SIZEOF_INT {"int (double *, int, double *, void *)"}, #endif #if NPY_SIZEOF_INTP == NPY_SIZEOF_LONG {"int (double *, long, double *, void *)"}, #endif #if NPY_SIZEOF_INTP == NPY_SIZEOF_LONGLONG {"int (double *, long long, double *, void *)"}, #endif {NULL} }; callback.py_function = NULL; callback.c_function = NULL; if (!PyArg_ParseTuple(args, "O&OO&O&idO&OO", NI_ObjectToInputArray, &input, &fnc, NI_ObjectToInputArray, &footprint, NI_ObjectToOutputArray, &output, &mode, &cval, PyArray_IntpConverter, &origin, &extra_arguments, &extra_keywords)) { goto exit; } if (!_validate_origin(input, origin)) { goto exit; } if (!PyTuple_Check(extra_arguments)) { PyErr_SetString(PyExc_RuntimeError, "extra_arguments must be a tuple"); goto exit; } if (!PyDict_Check(extra_keywords)) { PyErr_SetString(PyExc_RuntimeError, "extra_keywords must be a dictionary"); goto exit; } if (PyCapsule_CheckExact(fnc) && PyCapsule_GetName(fnc) == NULL) { func = PyCapsule_GetPointer(fnc, NULL); data = PyCapsule_GetContext(fnc); #if PY_VERSION_HEX < 0x03000000 } else if (PyCObject_Check(fnc)) { /* 'Legacy' low-level callable on Py2 */ func = PyCObject_AsVoidPtr(fnc); data = PyCObject_GetDesc(fnc); #endif } else { int ret; ret = ccallback_prepare(&callback, callback_signatures, fnc, CCALLBACK_DEFAULTS); if (ret == -1) { goto exit; } if (callback.py_function != NULL) { cbdata.extra_arguments = extra_arguments; cbdata.extra_keywords = extra_keywords; callback.info_p = (void*)&cbdata; func = Py_FilterFunc; data = (void*)&callback; } else { func = callback.c_function; data = callback.user_data; } } NI_GenericFilter(input, func, data, footprint, output, (NI_ExtendMode)mode, cval, origin.ptr); #ifdef HAVE_WRITEBACKIFCOPY PyArray_ResolveWritebackIfCopy(output); #endif exit: if (callback.py_function != NULL || callback.c_function != NULL) { ccallback_release(&callback); } Py_XDECREF(input); Py_XDECREF(output); Py_XDECREF(footprint); PyDimMem_FREE(origin.ptr); return PyErr_Occurred() ? NULL : Py_BuildValue(""); }