summaryrefslogtreecommitdiffstats
path: root/Modules/_testcapimodule.c
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2016-03-14 11:04:26 (GMT)
committerVictor Stinner <victor.stinner@gmail.com>2016-03-14 11:04:26 (GMT)
commit34be807ca4dfecc5b0a9e577a48535e738dce32b (patch)
tree38e0e3860b3fabf2be4938330a1d0f37c5b169ab /Modules/_testcapimodule.c
parentc877658d1ff5f93f3a2c7b5f0a7ac913b7374838 (diff)
downloadcpython-34be807ca4dfecc5b0a9e577a48535e738dce32b.zip
cpython-34be807ca4dfecc5b0a9e577a48535e738dce32b.tar.gz
cpython-34be807ca4dfecc5b0a9e577a48535e738dce32b.tar.bz2
Add PYTHONMALLOC env var
Issue #26516: * Add PYTHONMALLOC environment variable to set the Python memory allocators and/or install debug hooks. * PyMem_SetupDebugHooks() can now also be used on Python compiled in release mode. * The PYTHONMALLOCSTATS environment variable can now also be used on Python compiled in release mode. It now has no effect if set to an empty string. * In debug mode, debug hooks are now also installed on Python memory allocators when Python is configured without pymalloc.
Diffstat (limited to 'Modules/_testcapimodule.c')
-rw-r--r--Modules/_testcapimodule.c29
1 files changed, 29 insertions, 0 deletions
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c
index a21a584..babffc4 100644
--- a/Modules/_testcapimodule.c
+++ b/Modules/_testcapimodule.c
@@ -3616,6 +3616,33 @@ get_recursion_depth(PyObject *self, PyObject *args)
return PyLong_FromLong(tstate->recursion_depth - 1);
}
+static PyObject*
+pymem_buffer_overflow(PyObject *self, PyObject *args)
+{
+ char *buffer;
+
+ /* Deliberate buffer overflow to check that PyMem_Free() detects
+ the overflow when debug hooks are installed. */
+ buffer = PyMem_Malloc(16);
+ buffer[16] = 'x';
+ PyMem_Free(buffer);
+
+ Py_RETURN_NONE;
+}
+
+static PyObject*
+pymem_api_misuse(PyObject *self, PyObject *args)
+{
+ char *buffer;
+
+ /* Deliberate misusage of Python allocators:
+ allococate with PyMem but release with PyMem_Raw. */
+ buffer = PyMem_Malloc(16);
+ PyMem_RawFree(buffer);
+
+ Py_RETURN_NONE;
+}
+
static PyMethodDef TestMethods[] = {
{"raise_exception", raise_exception, METH_VARARGS},
@@ -3798,6 +3825,8 @@ static PyMethodDef TestMethods[] = {
{"PyTime_AsMilliseconds", test_PyTime_AsMilliseconds, METH_VARARGS},
{"PyTime_AsMicroseconds", test_PyTime_AsMicroseconds, METH_VARARGS},
{"get_recursion_depth", get_recursion_depth, METH_NOARGS},
+ {"pymem_buffer_overflow", pymem_buffer_overflow, METH_NOARGS},
+ {"pymem_api_misuse", pymem_api_misuse, METH_NOARGS},
{NULL, NULL} /* sentinel */
};