Skip to content

bpo-37088: Add the sys.addpendingcall() function. #13656

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Doc/library/sys.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ always available.
functions will not see the hook.


.. function:: addpendingcall(callback)

Schedule a *callback* to eventually execute in the main thread.

A :exc:`RuntimeError` is raised if the *callback* could not be
scheduled.

If *callback* raises an exception it is converted into an
*unraisable exception*, see :func:`sys.unraisablehook` for more
details.

.. versionadded:: 3.8


.. data:: argv

The list of command line arguments passed to a Python script. ``argv[0]`` is the
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add the sys.addpendingcall() function.
34 changes: 33 additions & 1 deletion Python/clinic/sysmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1895,13 +1895,52 @@ sys_getandroidapilevel_impl(PyObject *module)
#endif /* ANDROID_API_LEVEL */


static int
_call_pending_pycallback(void *arg)
{
PyObject *callback = (PyObject *)arg;
PyObject *res = PyObject_CallFunctionObjArgs(callback, NULL);

if (res == NULL) {
PyErr_WriteUnraisable(callback);
}
else {
Py_DECREF(res);
}

return 0;
}


/*[clinic input]
sys.addpendingcall

callback: object

Schedule a *callback* to eventually execute in the main thread.
[clinic start generated code]*/

static PyObject *
sys_addpendingcall_impl(PyObject *module, PyObject *callback)
/*[clinic end generated code: output=8af6dede85199179 input=edaaec4bd3f2c753]*/
{
if (Py_AddPendingCall(&_call_pending_pycallback, (void *)callback)) {
PyErr_SetString(PyExc_RuntimeError,
"failed to add a pending callback");
return NULL;
}

Py_RETURN_NONE;
}


static PyMethodDef sys_methods[] = {
/* Might as well keep this in alphabetic order */
SYS_ADDAUDITHOOK_METHODDEF
{"audit", (PyCFunction)(void(*)(void))sys_audit, METH_FASTCALL, audit_doc },
{"breakpointhook", (PyCFunction)(void(*)(void))sys_breakpointhook,
METH_FASTCALL | METH_KEYWORDS, breakpointhook_doc},
SYS_ADDPENDINGCALL_METHODDEF
SYS_CALLSTATS_METHODDEF
SYS__CLEAR_TYPE_CACHE_METHODDEF
SYS__CURRENT_FRAMES_METHODDEF
Expand Down