From 14bbcd8bd51bd95218474dd0204f4d3a458bf048 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 26 Mar 2020 19:18:47 +0100 Subject: [PATCH 1/4] bpo-38644: Use _PySys_Audit(): pass tstate explicitly Add the dependency to tstate more explicit. --- Parser/parsetok.c | 7 +++++-- Python/ceval.c | 5 +++-- Python/errors.c | 3 ++- Python/import.c | 9 +++++---- Python/pythonrun.c | 25 +++++++++++++------------ 5 files changed, 28 insertions(+), 21 deletions(-) diff --git a/Parser/parsetok.c b/Parser/parsetok.c index 554455dbc2badf2..2eb0a6044129f73 100644 --- a/Parser/parsetok.c +++ b/Parser/parsetok.c @@ -2,6 +2,8 @@ /* Parser-tokenizer link implementation */ #include "Python.h" +#include "pycore_pyerrors.h" +#include "pycore_sysmodule.h" #include "tokenizer.h" #include "node.h" #include "grammar.h" @@ -94,7 +96,8 @@ PyParser_ParseStringObject(const char *s, PyObject *filename, if (initerr(err_ret, filename) < 0) return NULL; - if (PySys_Audit("compile", "yO", s, err_ret->filename) < 0) { + PyThreadState *tstate = PyThreadState_GET(); + if (_PySys_Audit(tstate, "compile", "yO", s, err_ret->filename) < 0) { err_ret->error = E_ERROR; return NULL; } @@ -104,7 +107,7 @@ PyParser_ParseStringObject(const char *s, PyObject *filename, else tok = PyTokenizer_FromString(s, exec_input); if (tok == NULL) { - err_ret->error = PyErr_Occurred() ? E_DECODE : E_NOMEM; + err_ret->error = _PyErr_Occurred(tstate) ? E_DECODE : E_NOMEM; return NULL; } if (*flags & PyPARSE_TYPE_COMMENTS) { diff --git a/Python/ceval.c b/Python/ceval.c index afaa6ff1b3c513f..7aad984a78624e9 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -19,6 +19,7 @@ #include "pycore_pylifecycle.h" #include "pycore_pystate.h" #include "pycore_tupleobject.h" +#include "pycore_sysmodule.h" #include "code.h" #include "dictobject.h" @@ -4790,7 +4791,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; } @@ -4811,7 +4812,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; } diff --git a/Python/errors.c b/Python/errors.c index 4656fb2a3367060..a2fe52b812006f7 100644 --- a/Python/errors.c +++ b/Python/errors.c @@ -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__ @@ -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; diff --git a/Python/import.c b/Python/import.c index 645ebdf7b3cf224..5d500df93235f4f 100644 --- a/Python/import.c +++ b/Python/import.c @@ -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" @@ -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; } diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 76bc48d19b27eaf..95571a8c7518a14 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -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" @@ -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; @@ -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; /* @@ -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; @@ -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; } @@ -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; @@ -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); From 08c375ed889f49d5a8d5cc1d9d48b541cc27d596 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 26 Mar 2020 19:58:57 +0100 Subject: [PATCH 2/4] Update _PyEval_SetProfile() --- Python/ceval.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/Python/ceval.c b/Python/ceval.c index 7aad984a78624e9..3e6c5160611f8ba 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -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 "pycore_sysmodule.h" @@ -4694,9 +4695,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; } @@ -4722,7 +4724,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); } } @@ -4734,9 +4736,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; } @@ -4766,7 +4769,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); } } From d52c0cfe508dfad7ce4d66ad51c90daa68b0d7e9 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 26 Mar 2020 22:18:45 +0100 Subject: [PATCH 3/4] Revert changes --- Parser/parsetok.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/Parser/parsetok.c b/Parser/parsetok.c index 2eb0a6044129f73..554455dbc2badf2 100644 --- a/Parser/parsetok.c +++ b/Parser/parsetok.c @@ -2,8 +2,6 @@ /* Parser-tokenizer link implementation */ #include "Python.h" -#include "pycore_pyerrors.h" -#include "pycore_sysmodule.h" #include "tokenizer.h" #include "node.h" #include "grammar.h" @@ -96,8 +94,7 @@ PyParser_ParseStringObject(const char *s, PyObject *filename, if (initerr(err_ret, filename) < 0) return NULL; - PyThreadState *tstate = PyThreadState_GET(); - if (_PySys_Audit(tstate, "compile", "yO", s, err_ret->filename) < 0) { + if (PySys_Audit("compile", "yO", s, err_ret->filename) < 0) { err_ret->error = E_ERROR; return NULL; } @@ -107,7 +104,7 @@ PyParser_ParseStringObject(const char *s, PyObject *filename, else tok = PyTokenizer_FromString(s, exec_input); if (tok == NULL) { - err_ret->error = _PyErr_Occurred(tstate) ? E_DECODE : E_NOMEM; + err_ret->error = PyErr_Occurred() ? E_DECODE : E_NOMEM; return NULL; } if (*flags & PyPARSE_TYPE_COMMENTS) { From c9feef2a1ef1bf6254d7346580a83db62a0481e7 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 26 Mar 2020 23:04:50 +0100 Subject: [PATCH 4/4] Remove redundant include --- Python/ceval.c | 1 - 1 file changed, 1 deletion(-) diff --git a/Python/ceval.c b/Python/ceval.c index 3e6c5160611f8ba..3c2f8710d15d4b4 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -20,7 +20,6 @@ #include "pycore_pystate.h" #include "pycore_sysmodule.h" #include "pycore_tupleobject.h" -#include "pycore_sysmodule.h" #include "code.h" #include "dictobject.h"