Skip to content

bpo-38644: Use _PySys_Audit(): pass tstate explicitly #19183

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

Merged
merged 4 commits into from
Mar 27, 2020
Merged
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
19 changes: 11 additions & 8 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "pycore_pyerrors.h"
#include "pycore_pylifecycle.h"
#include "pycore_pystate.h"
#include "pycore_sysmodule.h"
#include "pycore_tupleobject.h"

#include "code.h"
Expand Down Expand Up @@ -4693,9 +4694,10 @@ _PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
/* The caller must hold the GIL */
assert(PyGILState_Check());

/* Call PySys_Audit() in the context of the current thread state,
/* Call _PySys_Audit() in the context of the current thread state,
even if tstate is not the current thread state. */
if (PySys_Audit("sys.setprofile", NULL) < 0) {
PyThreadState *current_tstate = _PyThreadState_GET();
if (_PySys_Audit(current_tstate, "sys.setprofile", NULL) < 0) {
return -1;
}

Expand All @@ -4721,7 +4723,7 @@ PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
{
PyThreadState *tstate = _PyThreadState_GET();
if (_PyEval_SetProfile(tstate, func, arg) < 0) {
/* Log PySys_Audit() error */
/* Log _PySys_Audit() error */
_PyErr_WriteUnraisableMsg("in PyEval_SetProfile", NULL);
}
}
Expand All @@ -4733,9 +4735,10 @@ _PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
/* The caller must hold the GIL */
assert(PyGILState_Check());

/* Call PySys_Audit() in the context of the current thread state,
/* Call _PySys_Audit() in the context of the current thread state,
even if tstate is not the current thread state. */
if (PySys_Audit("sys.settrace", NULL) < 0) {
PyThreadState *current_tstate = _PyThreadState_GET();
if (_PySys_Audit(current_tstate, "sys.settrace", NULL) < 0) {
return -1;
}

Expand Down Expand Up @@ -4765,7 +4768,7 @@ PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
{
PyThreadState *tstate = _PyThreadState_GET();
if (_PyEval_SetTrace(tstate, func, arg) < 0) {
/* Log PySys_Audit() error */
/* Log _PySys_Audit() error */
_PyErr_WriteUnraisableMsg("in PyEval_SetTrace", NULL);
}
}
Expand All @@ -4790,7 +4793,7 @@ _PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
{
PyThreadState *tstate = _PyThreadState_GET();

if (PySys_Audit("sys.set_asyncgen_hook_firstiter", NULL) < 0) {
if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_firstiter", NULL) < 0) {
return -1;
}

Expand All @@ -4811,7 +4814,7 @@ _PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
{
PyThreadState *tstate = _PyThreadState_GET();

if (PySys_Audit("sys.set_asyncgen_hook_finalizer", NULL) < 0) {
if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_finalizer", NULL) < 0) {
return -1;
}

Expand Down
3 changes: 2 additions & 1 deletion Python/errors.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "pycore_initconfig.h"
#include "pycore_pyerrors.h"
#include "pycore_pystate.h"
#include "pycore_sysmodule.h"
#include "pycore_traceback.h"

#ifndef __STDC__
Expand Down Expand Up @@ -1410,7 +1411,7 @@ _PyErr_WriteUnraisableMsg(const char *err_msg_str, PyObject *obj)
goto default_hook;
}

if (PySys_Audit("sys.unraisablehook", "OO", hook, hook_args) < 0) {
if (_PySys_Audit(tstate, "sys.unraisablehook", "OO", hook, hook_args) < 0) {
Py_DECREF(hook_args);
err_msg_str = "Exception ignored in audit hook";
obj = NULL;
Expand Down
9 changes: 5 additions & 4 deletions Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "pycore_pylifecycle.h"
#include "pycore_pymem.h"
#include "pycore_pystate.h"
#include "pycore_sysmodule.h"
#include "errcode.h"
#include "marshal.h"
#include "code.h"
Expand Down Expand Up @@ -1735,10 +1736,10 @@ import_find_and_load(PyThreadState *tstate, PyObject *abs_name)
PyObject *sys_path = PySys_GetObject("path");
PyObject *sys_meta_path = PySys_GetObject("meta_path");
PyObject *sys_path_hooks = PySys_GetObject("path_hooks");
if (PySys_Audit("import", "OOOOO",
abs_name, Py_None, sys_path ? sys_path : Py_None,
sys_meta_path ? sys_meta_path : Py_None,
sys_path_hooks ? sys_path_hooks : Py_None) < 0) {
if (_PySys_Audit(tstate, "import", "OOOOO",
abs_name, Py_None, sys_path ? sys_path : Py_None,
sys_meta_path ? sys_meta_path : Py_None,
sys_path_hooks ? sys_path_hooks : Py_None) < 0) {
return NULL;
}

Expand Down
25 changes: 13 additions & 12 deletions Python/pythonrun.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "pycore_pyerrors.h"
#include "pycore_pylifecycle.h"
#include "pycore_pystate.h"
#include "pycore_sysmodule.h"
#include "grammar.h"
#include "node.h"
#include "token.h"
Expand Down Expand Up @@ -696,8 +697,8 @@ _PyErr_PrintEx(PyThreadState *tstate, int set_sys_last_vars)
}
}
hook = _PySys_GetObjectId(&PyId_excepthook);
if (PySys_Audit("sys.excepthook", "OOOO", hook ? hook : Py_None,
exception, v, tb) < 0) {
if (_PySys_Audit(tstate, "sys.excepthook", "OOOO", hook ? hook : Py_None,
exception, v, tb) < 0) {
if (PyErr_ExceptionMatches(PyExc_RuntimeError)) {
PyErr_Clear();
goto done;
Expand Down Expand Up @@ -1100,7 +1101,7 @@ flush_io(void)
}

static PyObject *
run_eval_code_obj(PyCodeObject *co, PyObject *globals, PyObject *locals)
run_eval_code_obj(PyThreadState *tstate, PyCodeObject *co, PyObject *globals, PyObject *locals)
{
PyObject *v;
/*
Expand All @@ -1117,14 +1118,14 @@ run_eval_code_obj(PyCodeObject *co, PyObject *globals, PyObject *locals)

/* Set globals['__builtins__'] if it doesn't exist */
if (globals != NULL && PyDict_GetItemString(globals, "__builtins__") == NULL) {
PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
if (PyDict_SetItemString(globals, "__builtins__", interp->builtins) < 0) {
if (PyDict_SetItemString(globals, "__builtins__",
tstate->interp->builtins) < 0) {
return NULL;
}
}

v = PyEval_EvalCode((PyObject*)co, globals, locals);
if (!v && PyErr_Occurred() == PyExc_KeyboardInterrupt) {
if (!v && _PyErr_Occurred(tstate) == PyExc_KeyboardInterrupt) {
_Py_UnhandledKeyboardInterrupt = 1;
}
return v;
Expand All @@ -1134,18 +1135,17 @@ static PyObject *
run_mod(mod_ty mod, PyObject *filename, PyObject *globals, PyObject *locals,
PyCompilerFlags *flags, PyArena *arena)
{
PyCodeObject *co;
PyObject *v;
co = PyAST_CompileObject(mod, filename, flags, -1, arena);
PyThreadState *tstate = _PyThreadState_GET();
PyCodeObject *co = PyAST_CompileObject(mod, filename, flags, -1, arena);
if (co == NULL)
return NULL;

if (PySys_Audit("exec", "O", co) < 0) {
if (_PySys_Audit(tstate, "exec", "O", co) < 0) {
Py_DECREF(co);
return NULL;
}

v = run_eval_code_obj(co, globals, locals);
PyObject *v = run_eval_code_obj(tstate, co, globals, locals);
Py_DECREF(co);
return v;
}
Expand All @@ -1154,6 +1154,7 @@ static PyObject *
run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
PyObject *locals, PyCompilerFlags *flags)
{
PyThreadState *tstate = _PyThreadState_GET();
PyCodeObject *co;
PyObject *v;
long magic;
Expand Down Expand Up @@ -1182,7 +1183,7 @@ run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
}
fclose(fp);
co = (PyCodeObject *)v;
v = run_eval_code_obj(co, globals, locals);
v = run_eval_code_obj(tstate, co, globals, locals);
if (v && flags)
flags->cf_flags |= (co->co_flags & PyCF_MASK);
Py_DECREF(co);
Expand Down