Auto free Buffer objects in C API?
Stuart D. Gathman
stuart at bmsi.com
Thu Jul 3 16:20:32 EDT 2003
I have a Python C module (dspam.c - http://bmsi.com/python/milter.html).
It needs to return some large buffers allocated by the library I am
wrapping. The buffers must be freed by the caller when finished. At
present, I do the following to copy, then free the buffer:
PyObject *
toBuffer(char *buf;int len) {
PyObject *sig = PyBuffer_New(len);
if (sig) {
void *data;
int dlen;
if (!PyObject_AsWriteBuffer(sig,&data,&dlen))
memcpy(data,buf,dlen);
else {
Py_DECREF(sig);
sig = 0;
}
}
free(buf);
return sig;
}
Now, I would like to use PyBuffer_FromMemory(buf,len), but then I have no
way of knowing when to free(buf). Or do I? Am I missing something? Is
there a way for me to know when the Buffer object is being deleted so
that I can free the underlying C data?
More information about the Python-list
mailing list