bpo-32591: Add native coroutine origin tracking (#5250)

* Add coro.cr_origin and sys.set_coroutine_origin_tracking_depth
* Use coroutine origin information in the unawaited coroutine warning
* Stop using set_coroutine_wrapper in asyncio debug mode
* In BaseEventLoop.set_debug, enable debugging in the correct thread
This commit is contained in:
Nathaniel J. Smith 2018-01-21 06:44:07 -08:00 committed by Yury Selivanov
parent 1211c9a989
commit fc2f407829
20 changed files with 485 additions and 100 deletions

View file

@ -34,6 +34,13 @@ extern void *PyWin_DLLhModule;
extern const char *PyWin_DLLVersionString;
#endif
/*[clinic input]
module sys
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3726b388feee8cea]*/
#include "clinic/sysmodule.c.h"
_Py_IDENTIFIER(_);
_Py_IDENTIFIER(__sizeof__);
_Py_IDENTIFIER(_xoptions);
@ -710,9 +717,51 @@ sys_setrecursionlimit(PyObject *self, PyObject *args)
Py_RETURN_NONE;
}
/*[clinic input]
sys.set_coroutine_origin_tracking_depth
depth: int
Enable or disable origin tracking for coroutine objects in this thread.
Coroutine objects will track 'depth' frames of traceback information about
where they came from, available in their cr_origin attribute. Set depth of 0
to disable.
[clinic start generated code]*/
static PyObject *
sys_set_coroutine_origin_tracking_depth_impl(PyObject *module, int depth)
/*[clinic end generated code: output=0a2123c1cc6759c5 input=9083112cccc1bdcb]*/
{
if (depth < 0) {
PyErr_SetString(PyExc_ValueError, "depth must be >= 0");
return NULL;
}
_PyEval_SetCoroutineOriginTrackingDepth(depth);
Py_RETURN_NONE;
}
/*[clinic input]
sys.get_coroutine_origin_tracking_depth -> int
Check status of origin tracking for coroutine objects in this thread.
[clinic start generated code]*/
static int
sys_get_coroutine_origin_tracking_depth_impl(PyObject *module)
/*[clinic end generated code: output=3699f7be95a3afb8 input=335266a71205b61a]*/
{
return _PyEval_GetCoroutineOriginTrackingDepth();
}
static PyObject *
sys_set_coroutine_wrapper(PyObject *self, PyObject *wrapper)
{
if (PyErr_WarnEx(PyExc_DeprecationWarning,
"set_coroutine_wrapper is deprecated", 1) < 0) {
return NULL;
}
if (wrapper != Py_None) {
if (!PyCallable_Check(wrapper)) {
PyErr_Format(PyExc_TypeError,
@ -737,6 +786,10 @@ Set a wrapper for coroutine objects."
static PyObject *
sys_get_coroutine_wrapper(PyObject *self, PyObject *args)
{
if (PyErr_WarnEx(PyExc_DeprecationWarning,
"get_coroutine_wrapper is deprecated", 1) < 0) {
return NULL;
}
PyObject *wrapper = _PyEval_GetCoroutineWrapper();
if (wrapper == NULL) {
wrapper = Py_None;
@ -1512,6 +1565,8 @@ static PyMethodDef sys_methods[] = {
{"call_tracing", sys_call_tracing, METH_VARARGS, call_tracing_doc},
{"_debugmallocstats", sys_debugmallocstats, METH_NOARGS,
debugmallocstats_doc},
SYS_SET_COROUTINE_ORIGIN_TRACKING_DEPTH_METHODDEF
SYS_GET_COROUTINE_ORIGIN_TRACKING_DEPTH_METHODDEF
{"set_coroutine_wrapper", sys_set_coroutine_wrapper, METH_O,
set_coroutine_wrapper_doc},
{"get_coroutine_wrapper", sys_get_coroutine_wrapper, METH_NOARGS,