[Python-3000] cleaning up different ways to free an object

Brett Cannon brett at python.org
Sun Aug 19 23:08:41 CEST 2007


On 8/19/07, Neal Norwitz <nnorwitz at gmail.com> wrote:
> I just fixed a bug in the new memoryview that used PyObject_DEL which
> caused a problem in debug mode.  I had to change it to a Py_DECREF.
> It seems we have a lot of spellings of ways to free an object and I
> wonder if there are more problems lurking in there.
>
> $ cat */*.c | grep -c PyObject_Del
> 103
> $ cat */*.c | grep -c PyObject_DEL
> 16
> $ cat */*.c | grep -c PyObject_Free
> 16
> $ cat */*.c | grep -c PyObject_FREE
> 19
>
> I don't know how many of these are correct or incorrect.
>
> Note in Include/objimpl, the Del and Free variants are the same.  I
> plan to get rid of one of them.
>
> #define PyObject_Del            PyObject_Free
> #define PyObject_DEL            PyObject_FREE
>
> PyObject_{MALLOC,REALLOC,FREE} depend upon whether python is compiled
> with debug mode, pymalloc, or not.
>
> What are the rules for when a particular API should be used (or not
> used) to free an object?

If you read the comment at the top of Include/objimpl.h, it says that
PyObject_(New|NewVar|Del) are for object allocation while
PyObject_(Malloc|Realloc|Free) are just like malloc/free, but they use
pymalloc instead of the system malloc.

After that there are the usual performance macros.

I am sure that prefixing the pymalloc versions of malloc/free PyObject
is confusing for people.  Maybe that can change to something like
PyMalloc_* or something to disambiguate better.

-Brett


More information about the Python-3000 mailing list