Skip to content

Commit

Permalink
pythongh-117764: Add signatures for some functions in the sys module (p…
Browse files Browse the repository at this point in the history
…ythonGH-117770)

Use Argument Clinic if possible.
  • Loading branch information
serhiy-storchaka authored and diegorusso committed Apr 17, 2024
1 parent f27bed7 commit 750daf9
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 35 deletions.
34 changes: 29 additions & 5 deletions Python/clinic/sysmodule.c.h

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

74 changes: 44 additions & 30 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,8 @@ sys_addaudithook_impl(PyObject *module, PyObject *hook)
}

PyDoc_STRVAR(audit_doc,
"audit(event, *args)\n\
"audit($module, event, /, *args)\n\
--\n\
\n\
Passes the event to any audit hooks that are attached.");

Expand Down Expand Up @@ -644,7 +645,8 @@ sys_breakpointhook(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyOb
}

PyDoc_STRVAR(breakpointhook_doc,
"breakpointhook(*args, **kws)\n"
"breakpointhook($module, /, *args, **kwargs)\n"
"--\n"
"\n"
"This hook function is called by built-in breakpoint().\n"
);
Expand Down Expand Up @@ -1085,34 +1087,40 @@ trace_trampoline(PyObject *self, PyFrameObject *frame,
return 0;
}

/*[clinic input]
sys.settrace
function: object
/
Set the global debug tracing function.
It will be called on each function call. See the debugger chapter
in the library manual.
[clinic start generated code]*/

static PyObject *
sys_settrace(PyObject *self, PyObject *args)
sys_settrace(PyObject *module, PyObject *function)
/*[clinic end generated code: output=999d12e9d6ec4678 input=8107feb01c5f1c4e]*/
{
PyThreadState *tstate = _PyThreadState_GET();
if (args == Py_None) {
if (function == Py_None) {
if (_PyEval_SetTrace(tstate, NULL, NULL) < 0) {
return NULL;
}
}
else {
if (_PyEval_SetTrace(tstate, trace_trampoline, args) < 0) {
if (_PyEval_SetTrace(tstate, trace_trampoline, function) < 0) {
return NULL;
}
}
Py_RETURN_NONE;
}

PyDoc_STRVAR(settrace_doc,
"settrace(function)\n\
\n\
Set the global debug tracing function. It will be called on each\n\
function call. See the debugger chapter in the library manual."
);

/*[clinic input]
sys._settraceallthreads
arg: object
function as arg: object
/
Set the global debug tracing function in all running threads belonging to the current interpreter.
Expand All @@ -1123,7 +1131,7 @@ in the library manual.

static PyObject *
sys__settraceallthreads(PyObject *module, PyObject *arg)
/*[clinic end generated code: output=161cca30207bf3ca input=5906aa1485a50289]*/
/*[clinic end generated code: output=161cca30207bf3ca input=d4bde1f810d73675]*/
{
PyObject* argument = NULL;
Py_tracefunc func = NULL;
Expand Down Expand Up @@ -1159,45 +1167,51 @@ sys_gettrace_impl(PyObject *module)
return Py_NewRef(temp);
}

/*[clinic input]
sys.setprofile
function: object
/
Set the profiling function.
It will be called on each function call and return. See the profiler
chapter in the library manual.
[clinic start generated code]*/

static PyObject *
sys_setprofile(PyObject *self, PyObject *args)
sys_setprofile(PyObject *module, PyObject *function)
/*[clinic end generated code: output=1c3503105939db9c input=055d0d7961413a62]*/
{
PyThreadState *tstate = _PyThreadState_GET();
if (args == Py_None) {
if (function == Py_None) {
if (_PyEval_SetProfile(tstate, NULL, NULL) < 0) {
return NULL;
}
}
else {
if (_PyEval_SetProfile(tstate, profile_trampoline, args) < 0) {
if (_PyEval_SetProfile(tstate, profile_trampoline, function) < 0) {
return NULL;
}
}
Py_RETURN_NONE;
}

PyDoc_STRVAR(setprofile_doc,
"setprofile(function)\n\
\n\
Set the profiling function. It will be called on each function call\n\
and return. See the profiler chapter in the library manual."
);

/*[clinic input]
sys._setprofileallthreads
arg: object
function as arg: object
/
Set the profiling function in all running threads belonging to the current interpreter.
It will be called on each function call and return. See the profiler chapter
in the library manual.
It will be called on each function call and return. See the profiler
chapter in the library manual.
[clinic start generated code]*/

static PyObject *
sys__setprofileallthreads(PyObject *module, PyObject *arg)
/*[clinic end generated code: output=2d61319e27b309fe input=d1a356d3f4f9060a]*/
/*[clinic end generated code: output=2d61319e27b309fe input=a10589439ba20cee]*/
{
PyObject* argument = NULL;
Py_tracefunc func = NULL;
Expand Down Expand Up @@ -2525,11 +2539,11 @@ static PyMethodDef sys_methods[] = {
SYS_SETSWITCHINTERVAL_METHODDEF
SYS_GETSWITCHINTERVAL_METHODDEF
SYS_SETDLOPENFLAGS_METHODDEF
{"setprofile", sys_setprofile, METH_O, setprofile_doc},
SYS_SETPROFILE_METHODDEF
SYS__SETPROFILEALLTHREADS_METHODDEF
SYS_GETPROFILE_METHODDEF
SYS_SETRECURSIONLIMIT_METHODDEF
{"settrace", sys_settrace, METH_O, settrace_doc},
SYS_SETTRACE_METHODDEF
SYS__SETTRACEALLTHREADS_METHODDEF
SYS_GETTRACE_METHODDEF
SYS_CALL_TRACING_METHODDEF
Expand Down

0 comments on commit 750daf9

Please sign in to comment.