Skip to content
Open
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
1 change: 0 additions & 1 deletion Include/internal/pycore_interp_structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,6 @@ struct _is {
struct _obmalloc_state *obmalloc;

PyObject *audit_hooks;
PyMutex audit_hooks_mutex;
PyType_WatchCallback type_watchers[TYPE_MAX_WATCHERS];
PyCode_WatchCallback code_watchers[CODE_MAX_WATCHERS];
PyContext_WatchCallback context_watchers[CONTEXT_MAX_WATCHERS];
Expand Down
1 change: 0 additions & 1 deletion Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,6 @@ init_interpreter(PyInterpreterState *interp,
llist_init(&interp->mem_free_queue.head);
llist_init(&interp->asyncio_tasks_head);
interp->asyncio_tasks_lock = (PyMutex){0};
interp->audit_hooks_mutex = (PyMutex){0};
for (int i = 0; i < _PY_MONITORING_UNGROUPED_EVENTS; i++) {
interp->monitors.tools[i] = 0;
}
Expand Down
37 changes: 16 additions & 21 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,12 @@ should_audit(PyInterpreterState *interp)
if (!interp) {
return 0;
}
// interp->audit_hooks can only ever be NULL very early during initialization
// or very late during finalization.
int interp_has_audit_hooks = (interp->audit_hooks != NULL
&& PyList_GET_SIZE(interp->audit_hooks) > 0);
return (interp->runtime->audit_hooks.head
|| FT_ATOMIC_LOAD_PTR_ACQUIRE(interp->audit_hooks)
|| interp_has_audit_hooks
|| PyDTrace_AUDIT_ENABLED());
}

Expand Down Expand Up @@ -306,8 +310,9 @@ sys_audit_tstate(PyThreadState *ts, const char *event,
}

/* Call interpreter hooks */
PyObject *audit_hooks = FT_ATOMIC_LOAD_PTR_ACQUIRE(is->audit_hooks);
if (audit_hooks) {
PyObject *audit_hooks = is->audit_hooks;
assert(audit_hooks != NULL);
if (PyList_GET_SIZE(audit_hooks) > 0) {
eventName = PyUnicode_FromString(event);
if (!eventName) {
goto exit;
Expand Down Expand Up @@ -537,29 +542,13 @@ sys_addaudithook_impl(PyObject *module, PyObject *hook)
}

PyInterpreterState *interp = tstate->interp;
PyMutex mutex = interp->audit_hooks_mutex;
PyMutex_Lock(&mutex);

if (interp->audit_hooks == NULL) {
PyObject *new_list = PyList_New(0);
if (new_list == NULL) {
goto error;
}
/* Avoid having our list of hooks show up in the GC module */
PyObject_GC_UnTrack(new_list);
FT_ATOMIC_STORE_PTR_RELEASE(interp->audit_hooks, new_list);
}

assert(interp->audit_hooks != NULL);
if (PyList_Append(interp->audit_hooks, hook) < 0) {
goto error;
return NULL;
}

PyMutex_Unlock(&mutex);
Py_RETURN_NONE;

error:
PyMutex_Unlock(&mutex);
return NULL;
}

/*[clinic input]
Expand Down Expand Up @@ -4323,6 +4312,12 @@ _PySys_Create(PyThreadState *tstate, PyObject **sysmod_p)

PyInterpreterState *interp = tstate->interp;

PyObject *audit_hooks = PyList_New(0);
if (audit_hooks == NULL) {
goto error;
}
interp->audit_hooks = audit_hooks;

PyObject *modules = _PyImport_InitModules(interp);
if (modules == NULL) {
goto error;
Expand Down
Loading