Note that the PyObject_Malloc() functions have never been documented.
(Well, there are references regarding their mere existence in the docs, but
nothing more than that.)
https://docs.python.org/3.6/search.html?q=pyobject_malloc&check_keywords=yes&area=default
And, for example, the "what's new in 2.5" document says:
"""
Python’s API has many different functions for allocating memory that are
grouped into families. For example, PyMem_Malloc(), PyMem_Realloc(), and
PyMem_Free() are one family that allocates raw memory, while
PyObject_Malloc(), PyObject_Realloc(), and PyObject_Free() are another
family that’s supposed to be used for creating Python objects.
"""
I don't think there are many extensions out there in which *object* memory
gets allocated manually, which implicitly puts a pretty clear "don't use"
marker on these functions.
Stefan
Yeah, there is an old bug to track this:
http://bugs.python.org/issue20064
> And, for example, the "what's new in 2.5" document says:
>
> """
> Python’s API has many different functions for allocating memory that are
> grouped into families. For example, PyMem_Malloc(), PyMem_Realloc(), and
> PyMem_Free() are one family that allocates raw memory, while
> PyObject_Malloc(), PyObject_Realloc(), and PyObject_Free() are another
> family that’s supposed to be used for creating Python objects.
> """
>
> I don't think there are many extensions out there in which *object* memory
> gets allocated manually, which implicitly puts a pretty clear "don't use"
> marker on these functions.
Should I understand that it's another good reason to make
PyMem_Malloc() faster for everyone?
Victor
2016-02-08 15:18 GMT+01:00 Victor Stinner <victor....@gmail.com>:
>> Perhaps if you add some guards somewhere :-)
>
> We have runtime checks but only implemented in debug mode for efficiency.
>
> By the way, I proposed once to add an environment variable to allow to
> enable these checks without having to recompile Python. Since the PEP
> 445, it became easy to implement this. What do you think?
> https://www.python.org/dev/peps/pep-0445/#add-a-new-pydebugmalloc-environment-variable
Ok, I wrote a patch to implement a new PYTHONMALLOC environment variable:
http://bugs.python.org/issue26516
PYTHONMALLOC=debug installs debug hooks to:
* detect API violations, ex: PyObject_Free() called on a buffer
allocated by PyMem_Malloc()
* detect write before the start of the buffer (buffer underflow)
* detect write after the end of the buffer (buffer overflow)
https://docs.python.org/dev/c-api/memory.html#c.PyMem_SetupDebugHooks
The main advantage of this variable is that you don't have to
recompile Python in debug mode to benefit of these checks.