Auto free Buffer objects in C API?

Stuart D. Gathman stuart at
Thu Jul 3 22:20:32 CEST 2003

I have a Python C module (dspam.c -
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))
    else {
      sig = 0;
  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