Skip to content

Commit 08faf00

Browse files
authored
bpo-38644: Add _PySys_Audit() which takes tstate (GH-19180)
Add _PySys_Audit() function to the internal C API: similar to PySys_Audit(), but requires a mandatory tstate parameter. Cleanup sys_audit_tstate() code: remove code path for NULL tstate, since the function exits at entry if tstate is NULL. Remove also code path for NULL tstate->interp: should_audit() now ensures that it is not NULL (even if tstate->interp cannot be NULL in practice). PySys_AddAuditHook() now checks if tstate is not NULL to decide if tstate can be used or not, and tstate is set to NULL if the runtime is not initialized yet. Use _PySys_Audit() in sysmodule.c.
1 parent e0b8101 commit 08faf00

File tree

7 files changed

+145
-66
lines changed

7 files changed

+145
-66
lines changed

Include/cpython/sysmodule.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ PyAPI_FUNC(size_t) _PySys_GetSizeOf(PyObject *);
1313

1414
typedef int(*Py_AuditHookFunction)(const char *, PyObject *, void *);
1515

16-
PyAPI_FUNC(int) PySys_Audit(const char*, const char *, ...);
16+
PyAPI_FUNC(int) PySys_Audit(
17+
const char *event,
18+
const char *argFormat,
19+
...);
1720
PyAPI_FUNC(int) PySys_AddAuditHook(Py_AuditHookFunction, void*);
1821

1922
#ifdef __cplusplus

Include/internal/pycore_sysmodule.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifndef Py_INTERNAL_SYSMODULE_H
2+
#define Py_INTERNAL_SYSMODULE_H
3+
#ifdef __cplusplus
4+
extern "C" {
5+
#endif
6+
7+
#ifndef Py_BUILD_CORE
8+
# error "this header requires Py_BUILD_CORE define"
9+
#endif
10+
11+
PyAPI_FUNC(int) _PySys_Audit(
12+
PyThreadState *tstate,
13+
const char *event,
14+
const char *argFormat,
15+
...);
16+
17+
/* We want minimal exposure of this function, so use extern rather than
18+
PyAPI_FUNC() to not export the symbol. */
19+
extern void _PySys_ClearAuditHooks(PyThreadState *tstate);
20+
21+
#ifdef __cplusplus
22+
}
23+
#endif
24+
#endif /* !Py_INTERNAL_SYSMODULE_H */

Makefile.pre.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,7 @@ PYTHON_HEADERS= \
11011101
$(srcdir)/Include/internal/pycore_pylifecycle.h \
11021102
$(srcdir)/Include/internal/pycore_pymem.h \
11031103
$(srcdir)/Include/internal/pycore_pystate.h \
1104+
$(srcdir)/Include/internal/pycore_sysmodule.h \
11041105
$(srcdir)/Include/internal/pycore_traceback.h \
11051106
$(srcdir)/Include/internal/pycore_tupleobject.h \
11061107
$(srcdir)/Include/internal/pycore_warnings.h \

PCbuild/pythoncore.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@
183183
<ClInclude Include="..\Include\internal\pycore_pylifecycle.h" />
184184
<ClInclude Include="..\Include\internal\pycore_pymem.h" />
185185
<ClInclude Include="..\Include\internal\pycore_pystate.h" />
186+
<ClInclude Include="..\Include\internal\pycore_sysmodule.h" />
186187
<ClInclude Include="..\Include\internal\pycore_traceback.h" />
187188
<ClInclude Include="..\Include\internal\pycore_tupleobject.h" />
188189
<ClInclude Include="..\Include\internal\pycore_warnings.h" />

PCbuild/pythoncore.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,9 @@
252252
<ClInclude Include="..\Include\internal\pycore_pystate.h">
253253
<Filter>Include</Filter>
254254
</ClInclude>
255+
<ClInclude Include="..\Include\internal\pycore_sysmodule.h">
256+
<Filter>Include</Filter>
257+
</ClInclude>
255258
<ClInclude Include="..\Include\internal\pycore_traceback.h">
256259
<Filter>Include</Filter>
257260
</ClInclude>

Python/pylifecycle.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "pycore_pylifecycle.h"
1717
#include "pycore_pymem.h"
1818
#include "pycore_pystate.h"
19+
#include "pycore_sysmodule.h"
1920
#include "pycore_traceback.h"
2021
#include "grammar.h"
2122
#include "node.h"
@@ -1409,11 +1410,7 @@ Py_FinalizeEx(void)
14091410
_PyGC_CollectIfEnabled();
14101411

14111412
/* Clear all loghooks */
1412-
/* We want minimal exposure of this function, so define the extern
1413-
* here. The linker should discover the correct function without
1414-
* exporting a symbol. */
1415-
extern void _PySys_ClearAuditHooks(void);
1416-
_PySys_ClearAuditHooks();
1413+
_PySys_ClearAuditHooks(tstate);
14171414

14181415
/* Destroy all modules */
14191416
_PyImport_Cleanup(tstate);

0 commit comments

Comments
 (0)