-
-
Notifications
You must be signed in to change notification settings - Fork 30.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bpo-32591: Add native coroutine origin tracking #5250
Changes from 1 commit
091dc24
d504810
5d8f591
82d0c3e
6c7f73a
7738cc4
2157af3
b0e52ec
c0feb3b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -502,6 +502,12 @@ def extract(): | |
msg_lines.append("Coroutine created at (most recent call last)\n") | ||
msg_lines += traceback.format_list(list(extract())) | ||
msg = "".join(msg_lines).rstrip("\n") | ||
# Passing source= here means that if the user happens to have tracemalloc | ||
# enabled and tracking where the coroutine was created, the warning will | ||
# contain that traceback. This does mean that if they have *both* | ||
# coroutine origin tracking *and* tracemalloc enabled, they'll get two | ||
# partially-redundant tracebacks. If we wanted to be clever we could | ||
# probably detect this case and avoid it, but for now we don't bother. | ||
warn(msg, category=RuntimeWarning, stacklevel=2, source=coro) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Small, easy to miss point here: passing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think a redundant warning is OK. |
||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,8 +32,9 @@ gen_traverse(PyGenObject *gen, visitproc visit, void *arg) | |
Py_VISIT(gen->gi_code); | ||
Py_VISIT(gen->gi_name); | ||
Py_VISIT(gen->gi_qualname); | ||
if (((PyCodeObject *)gen->gi_code)->co_flags & CO_COROUTINE) | ||
if (((PyCodeObject *)gen->gi_code)->co_flags & CO_COROUTINE) { | ||
Py_VISIT(((PyCoroObject *)gen)->cr_origin); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you only store ints and strings in tuples of that list (without cycles), you can skip the |
||
} | ||
return exc_state_traverse(&gen->gi_exc_state, visit, arg); | ||
} | ||
|
||
|
@@ -76,8 +77,9 @@ _PyGen_Finalize(PyObject *self) | |
if (gen->gi_code != NULL && | ||
((PyCodeObject *)gen->gi_code)->co_flags & CO_COROUTINE && | ||
gen->gi_frame->f_lasti == -1) { | ||
if (!error_value) | ||
if (!error_value) { | ||
_PyErr_WarnUnawaitedCoroutine((PyObject *)gen); | ||
} | ||
} | ||
else { | ||
res = gen_close(gen, NULL); | ||
|
@@ -136,8 +138,9 @@ gen_dealloc(PyGenObject *gen) | |
gen->gi_frame->f_gen = NULL; | ||
Py_CLEAR(gen->gi_frame); | ||
} | ||
if (((PyCodeObject *)gen->gi_code)->co_flags & CO_COROUTINE) | ||
if (((PyCodeObject *)gen->gi_code)->co_flags & CO_COROUTINE) { | ||
Py_CLEAR(((PyCoroObject *)gen)->cr_origin); | ||
} | ||
Py_CLEAR(gen->gi_code); | ||
Py_CLEAR(gen->gi_name); | ||
Py_CLEAR(gen->gi_qualname); | ||
|
@@ -1160,47 +1163,56 @@ PyTypeObject _PyCoroWrapper_Type = { | |
0, /* tp_free */ | ||
}; | ||
|
||
static PyObject * | ||
compute_cr_origin(int origin_depth) | ||
{ | ||
PyObject *cr_origin = PyList_New(origin_depth); | ||
PyFrameObject *frame = PyEval_GetFrame(); | ||
int i = 0; | ||
for (; frame && i < origin_depth; ++i) { | ||
PyObject *frameinfo = Py_BuildValue( | ||
"OiO", | ||
frame->f_code->co_filename, | ||
PyFrame_GetLineNumber(frame), | ||
frame->f_code->co_name); | ||
if (!frameinfo) { | ||
Py_DECREF(cr_origin); | ||
return NULL; | ||
} | ||
PyList_SET_ITEM(cr_origin, i, frameinfo); | ||
frame = frame->f_back; | ||
} | ||
/* Truncate the list if necessary */ | ||
if (i < origin_depth) { | ||
if (PyList_SetSlice(cr_origin, i, origin_depth, NULL) < 0) { | ||
Py_DECREF(cr_origin); | ||
return NULL; | ||
} | ||
} | ||
|
||
return cr_origin; | ||
} | ||
|
||
PyObject * | ||
PyCoro_New(PyFrameObject *f, PyObject *name, PyObject *qualname) | ||
{ | ||
PyObject *coro = gen_new_with_qualname(&PyCoro_Type, f, name, qualname); | ||
if (!coro) | ||
if (!coro) { | ||
return NULL; | ||
} | ||
|
||
PyThreadState *tstate = PyThreadState_GET(); | ||
int depth = tstate->coroutine_origin_tracking_depth; | ||
int origin_depth = tstate->coroutine_origin_tracking_depth; | ||
|
||
if (depth == 0) { | ||
if (origin_depth == 0) { | ||
((PyCoroObject *)coro)->cr_origin = NULL; | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, per PEP 7: if ( ... ) {
}
else {
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also I'd like this whole |
||
PyObject *origin = PyList_New(depth); | ||
/* Immediately pass ownership to coro, so on error paths we don't have | ||
to worry about it separately. */ | ||
((PyCoroObject *)coro)->cr_origin = origin; | ||
PyFrameObject *frame = PyEval_GetFrame(); | ||
int i = 0; | ||
for (; i < depth; ++i) { | ||
if (!frame) | ||
break; | ||
|
||
PyObject *frameinfo = Py_BuildValue( | ||
"OiO", | ||
frame->f_code->co_filename, | ||
PyFrame_GetLineNumber(frame), | ||
frame->f_code->co_name); | ||
if (!frameinfo) { | ||
Py_DECREF(coro); | ||
return NULL; | ||
} | ||
PyList_SET_ITEM(origin, i, frameinfo); | ||
|
||
frame = frame->f_back; | ||
} | ||
/* Truncate the list if necessary */ | ||
if (PyList_SetSlice(origin, i, depth, NULL) < 0) { | ||
PyObject *cr_origin = compute_cr_origin(origin_depth); | ||
if (!cr_origin) { | ||
Py_DECREF(coro); | ||
return NULL; | ||
} | ||
((PyCoroObject *)coro)->cr_origin = cr_origin; | ||
} | ||
|
||
return coro; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a link to the
set_coroutine_origin_tracking_depth
documentation snippet?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, that was my original thought. The problem is that
is too long to fit in the ascii-art box. So... either we need a shorter name for the function, or we need to redraw this whole giant table, and I couldn't think of a satisfactory way to do either in the 2 minutes I spent thinking about it :-). Any suggestions?
I guess we could use that weird ReST substitution thing? I'm not sure how that works.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this work: http://docutils.sourceforge.net/docs/user/rst/quickref.html#hyperlink-targets ?