gh-85283: Add PySys_AuditTuple() function (#108965)

sys.audit() now has assertions to check that the event argument is
not NULL and that the format argument does not use the "N" format.

Add tests on PySys_AuditTuple().
This commit is contained in:
Victor Stinner 2023-10-05 23:59:35 +02:00 • committed by GitHub
parent aaf297c048
commit bb057b3370
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 109 additions and 9 deletions

View file

@ -1278,11 +1278,16 @@ static int _test_audit(Py_ssize_t setValue)
printf("Set event failed");
return 4;
}
if (PyErr_Occurred()) {
printf("Exception raised");
return 5;
}
if (sawSet != 42) {
printf("Failed to see *userData change\n");
return 5;
return 6;
}
return 0;
}
@ -1296,6 +1301,57 @@ static int test_audit(void)
return result;
}
static int test_audit_tuple(void)
{
#define ASSERT(TEST, EXITCODE) \
if (!(TEST)) { \
printf("ERROR test failed at %s:%i\n", __FILE__, __LINE__); \
return (EXITCODE); \
}
Py_ssize_t sawSet = 0;
// we need at least one hook, otherwise code checking for
// PySys_AuditTuple() is skipped.
PySys_AddAuditHook(_audit_hook, &sawSet);
_testembed_Py_InitializeFromConfig();
ASSERT(!PyErr_Occurred(), 0);
// pass Python tuple object
PyObject *tuple = Py_BuildValue("(i)", 444);
if (tuple == NULL) {
goto error;
}
ASSERT(PySys_AuditTuple("_testembed.set", tuple) == 0, 10);
ASSERT(!PyErr_Occurred(), 11);
ASSERT(sawSet == 444, 12);
Py_DECREF(tuple);
// pass Python int object
PyObject *int_arg = PyLong_FromLong(555);
if (int_arg == NULL) {
goto error;
}
ASSERT(PySys_AuditTuple("_testembed.set", int_arg) == -1, 20);
ASSERT(PyErr_ExceptionMatches(PyExc_TypeError), 21);
PyErr_Clear();
Py_DECREF(int_arg);
// NULL is accepted and means "no arguments"
ASSERT(PySys_AuditTuple("_testembed.test_audit_tuple", NULL) == 0, 30);
ASSERT(!PyErr_Occurred(), 31);
Py_Finalize();
return 0;
error:
PyErr_Print();
return 1;
#undef ASSERT
}
static volatile int _audit_subinterpreter_interpreter_count = 0;
static int _audit_subinterpreter_hook(const char *event, PyObject *args, void *userdata)
@ -2140,6 +2196,7 @@ static struct TestCase TestCases[] = {
// Audit
{"test_open_code_hook", test_open_code_hook},
{"test_audit", test_audit},
{"test_audit_tuple", test_audit_tuple},
{"test_audit_subinterpreter", test_audit_subinterpreter},
{"test_audit_run_command", test_audit_run_command},
{"test_audit_run_file", test_audit_run_file},