[Python-Dev] PyMem_Malloc() vs PyObject_Malloc()

Kristján Valur Jónsson kristjan at ccpgames.com
Fri Sep 4 11:19:09 CEST 2009

I noticed something (in 2.5) yesterday, which may be a feature, but is more likely a bug.
In tokenizer.c, tok->encoding is allocated using PyMem_MALLOC().
However, this then gets handed to a node->r_str in parsetok.c, and then released in node.c using PyObject_Free().

Now, by coincidence, PyObject_Free() will default to free() for objects that it doesn't recognize, so this works.  But is this documented behavior?  The reason I ran into this was that I had redirect the PyMem_* API to a different allocator, but left the PyObject_* one alone.

My feeling Is that these two APIs shouldn't be interchangeable.  Especially since you can't hand a PyObject_Malloc'd object to PyMem_Free() so the inverse shouldn't be expected to work.

Any thoughts?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20090904/fdb0a03a/attachment.htm>

More information about the Python-Dev mailing list