diff -r 36a6106e78ac Doc/library/tracemalloc.rst --- a/Doc/library/tracemalloc.rst Thu Nov 21 21:44:42 2013 +0100 +++ b/Doc/library/tracemalloc.rst Sat Nov 23 02:35:32 2013 +0100 @@ -21,8 +21,7 @@ start tracing Python memory allocations. By default, a trace of an allocated memory block only stores the most recent frame (1 frame). To store 25 frames at startup: set the :envvar:`PYTHONTRACEMALLOC` environment variable to ``25``, or use the -:option:`-X` ``tracemalloc=25`` command line option. The -:func:`set_traceback_limit` function can be used at runtime to set the limit. +:option:`-X` ``tracemalloc=25`` command line option. .. versionadded:: 3.4 @@ -120,8 +119,8 @@ Code to display the traceback of the big import linecache import tracemalloc - tracemalloc.set_traceback_limit(25) - tracemalloc.start() + # Store 25 frames + tracemalloc.start(25) # ... run your application ... @@ -267,10 +266,10 @@ Functions Get the maximum number of frames stored in the traceback of a trace. - By default, a trace of a memory block only stores the most recent - frame: the limit is ``1``. + The :mod:`tracemalloc` module must be tracing memory allocations to + get the limit, otherwise an exception is raised. - Use the :func:`set_traceback_limit` function to change the limit. + The limit is set by the :func:`start` function. .. function:: get_traced_memory() @@ -294,10 +293,13 @@ Functions See also :func:`start` and :func:`stop` functions. -.. function:: set_traceback_limit(nframe: int) +.. function:: start(nframe: int=1) - Set the maximum number of frames stored in the traceback of a trace. - *nframe* must be greater or equal to ``1``. + Start tracing Python memory allocations: install hooks on Python memory + allocators. Set also the maximum number of frames stored in the traceback of + a trace to *nframe*. By default, a trace of a memory block only stores the + most recent frame: the limit is ``1``. *nframe* must be greater or equal to + ``1``. Storing more than ``1`` frame is only useful to compute statistics grouped by ``'traceback'`` or to compute cumulative statistics: see the @@ -309,17 +311,11 @@ Functions The :envvar:`PYTHONTRACEMALLOC` environment variable (``PYTHONTRACEMALLOC=NFRAME``) and the :option:`-X` ``tracemalloc=NFRAME`` - command line option can be used to set the limit at startup. + command line option can be used to start tracing with a the limit at + startup. - Use the :func:`get_traceback_limit` function to get the current limit. - - -.. function:: start() - - Start tracing Python memory allocations: install hooks on Python memory - allocators. - - See also :func:`stop` and :func:`is_tracing` functions. + See also :func:`stop`, :func:`is_tracing` and :func:`get_traceback_limit` + functions. .. function:: stop() @@ -342,7 +338,7 @@ Functions :mod:`tracemalloc` module started to trace memory allocations. Tracebacks of traces are limited to :func:`get_traceback_limit` frames. Use - :func:`set_traceback_limit` to store more frames. + the *nframe* parameter of the :func:`start` function to store more frames. The :mod:`tracemalloc` module must be tracing memory allocations to take a snapshot, see the the :func:`start` function. diff -r 36a6106e78ac Modules/_tracemalloc.c --- a/Modules/_tracemalloc.c Thu Nov 21 21:44:42 2013 +0100 +++ b/Modules/_tracemalloc.c Sat Nov 23 02:35:32 2013 +0100 @@ -1153,13 +1153,27 @@ done: } PyDoc_STRVAR(tracemalloc_start_doc, - "start()\n" + "start(nframe: int=1)\n" "\n" - "Start tracing Python memory allocations."); + "Start tracing Python memory allocations. Set also the maximum number \n" + "of frames stored in the traceback of a trace to nframe."); static PyObject* -py_tracemalloc_start(PyObject *self) +py_tracemalloc_start(PyObject *self, PyObject *args) { + Py_ssize_t nframe; + + if (!PyArg_ParseTuple(args, "n:start", &nframe)) + return NULL; + + if (nframe < 1 || nframe > MAX_NFRAME) { + PyErr_Format(PyExc_ValueError, + "the number of frames must be in range [1; %i]", + MAX_NFRAME); + return NULL; + } + tracemalloc_config.max_nframe = Py_SAFE_DOWNCAST(nframe, Py_ssize_t, int); + if (tracemalloc_start() < 0) return NULL; @@ -1194,31 +1208,6 @@ py_tracemalloc_get_traceback_limit(PyObj return PyLong_FromLong(tracemalloc_config.max_nframe); } -PyDoc_STRVAR(tracemalloc_set_traceback_limit_doc, - "set_traceback_limit(nframe: int)\n" - "\n" - "Set the maximum number of frames stored in the traceback of a trace."); - -static PyObject* -tracemalloc_set_traceback_limit(PyObject *self, PyObject *args) -{ - Py_ssize_t nframe; - - if (!PyArg_ParseTuple(args, "n:set_traceback_limit", - &nframe)) - return NULL; - - if (nframe < 1 || nframe > MAX_NFRAME) { - PyErr_Format(PyExc_ValueError, - "the number of frames must be in range [1; %i]", - MAX_NFRAME); - return NULL; - } - tracemalloc_config.max_nframe = Py_SAFE_DOWNCAST(nframe, Py_ssize_t, int); - - Py_RETURN_NONE; -} - PyDoc_STRVAR(tracemalloc_get_tracemalloc_memory_doc, "get_tracemalloc_memory() -> int\n" "\n" @@ -1282,8 +1271,6 @@ static PyMethodDef module_methods[] = { METH_NOARGS, tracemalloc_stop_doc}, {"get_traceback_limit", (PyCFunction)py_tracemalloc_get_traceback_limit, METH_NOARGS, tracemalloc_get_traceback_limit_doc}, - {"set_traceback_limit", (PyCFunction)tracemalloc_set_traceback_limit, - METH_VARARGS, tracemalloc_set_traceback_limit_doc}, {"get_tracemalloc_memory", (PyCFunction)tracemalloc_get_tracemalloc_memory, METH_NOARGS, tracemalloc_get_tracemalloc_memory_doc}, {"get_traced_memory", (PyCFunction)tracemalloc_get_traced_memory,