Index: Python/sysmodule.c =================================================================== --- Python/sysmodule.c (revision 59545) +++ Python/sysmodule.c (working copy) @@ -397,6 +397,28 @@ ); static PyObject * +sys_gettrace(PyObject *self, PyObject *args) +{ + PyThreadState *tstate = PyThreadState_GET(); + PyObject *temp = tstate->c_traceobj; + + if (temp == NULL) { + temp = Py_None; + } + + Py_XINCREF(temp); + + return temp; +} + +PyDoc_STRVAR(gettrace_doc, +"gettrace()\n\ +\n\ +Return the global debug tracing function set by sys.settrace.\n\ +See the debugger chapter in the library manual." +); + +static PyObject * sys_setprofile(PyObject *self, PyObject *args) { if (trace_init() == -1) @@ -798,6 +820,7 @@ {"settscdump", sys_settscdump, METH_VARARGS, settscdump_doc}, #endif {"settrace", sys_settrace, METH_O, settrace_doc}, + {"gettrace", sys_gettrace, METH_NOARGS, gettrace_doc}, {"call_tracing", sys_call_tracing, METH_VARARGS, call_tracing_doc}, {NULL, NULL} /* sentinel */ }; Index: Doc/library/sys.rst =================================================================== --- Doc/library/sys.rst (revision 59545) +++ Doc/library/sys.rst (working copy) @@ -353,7 +353,23 @@ This function should be used for internal and specialized purposes only. +.. function:: gettrace() + .. index:: + single: trace function + single: debugger + + Get the trace function as set by :func:`settrace`. + + .. note:: + + The :func:`gettrace` function is intended only for implementing debuggers, + profilers, coverage tools and the like. Its behavior is part of the + implementation platform, rather than part of the language definition, and thus + may not be available in all Python implementations. + + .. versionadded:: 2.6 + .. function:: getwindowsversion() Return a tuple containing five components, describing the Windows version Index: Lib/test/test_trace.py =================================================================== --- Lib/test/test_trace.py (revision 59545) +++ Lib/test/test_trace.py (working copy) @@ -268,6 +268,20 @@ self.compare_events(func.func_code.co_firstlineno, tracer.events, func.events) + def set_and_retrieve_none(self): + sys.settrace(None) + assert sys.gettrace() is None + + def set_and_retrieve_func(self): + def fn(*args): + pass + + sys.settrace(fn) + try: + assert sys.gettrace() is fn + finally: + sys.settrace(None) + def test_01_basic(self): self.run_test(basic) def test_02_arigo(self):