Skip to content

gh-136336: add tests for PySys_Audit() and PySys_AuditTuple() #136337

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
64 changes: 59 additions & 5 deletions Lib/test/test_capi/test_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@
NULL = None

class CAPITest(unittest.TestCase):
# TODO: Test the following functions:
#
# PySys_Audit()
# PySys_AuditTuple()

maxDiff = None

@unittest.skipIf(_testlimitedcapi is None, 'need _testlimitedcapi module')
Expand Down Expand Up @@ -211,6 +206,65 @@ def test_sys_writestderr(self):
# Test PySys_WriteStderr()
self._test_sys_writestream('PySys_WriteStderr', 'stderr')

@unittest.skipIf(_testlimitedcapi is None, 'need _testlimitedcapi module')
def test_sys_audit(self):
# Test PySys_Audit()
sys_audit = _testlimitedcapi.sys_audit

result = sys_audit("test.event", "OO", 1, "a")
self.assertEqual(result, 0)

result = sys_audit("test.no_args", "")
self.assertEqual(result, 0)

with self.assertRaises(TypeError):
sys_audit(123, "O", 1)

result = sys_audit("テスト.イベント", "O", 42)
self.assertEqual(result, 0)

result = sys_audit(None, "O", 1)
self.assertEqual(result, 0)

result = sys_audit(b"test.non_utf8\xff", "O", 1)
self.assertEqual(result, 0)

result = sys_audit("test.event", "(")
self.assertEqual(result, 0)

result = sys_audit("test.event", "&")
self.assertEqual(result, 0)

result = sys_audit("test.event", b"\xff")
self.assertEqual(result, 0)

result = sys_audit("test.event", "{OO}", [], [])
self.assertEqual(result, 0)


@unittest.skipIf(_testlimitedcapi is None, 'need _testlimitedcapi module')
def test_sys_audittuple(self):
# Test PySys_AuditTuple()
sys_audittuple = _testlimitedcapi.sys_audittuple

result = sys_audittuple("test.event", (1, "a"))
self.assertEqual(result, 0)

result = sys_audittuple("test.null_tuple")
self.assertEqual(result, 0)

with self.assertRaises(TypeError):
sys_audittuple("test.bad_tuple", [1, 2])

result = sys_audittuple("テスト.イベント", (42,))
self.assertEqual(result, 0)

result = sys_audittuple(None, (123,))
self.assertEqual(result, 0)

result = sys_audittuple(b"test.non_utf8\xff", (1,))
self.assertEqual(result, 0)


if __name__ == "__main__":
unittest.main()
43 changes: 43 additions & 0 deletions Modules/_testlimitedcapi/sys.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#endif
#include "parts.h"
#include "util.h"
#include "audit.h"


static PyObject *
Expand Down Expand Up @@ -106,6 +107,46 @@ sys_getxoptions(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(ignored))
return Py_XNewRef(result);
}

static PyObject *
sys_audit(PyObject *Py_UNUSED(module), PyObject *args)
{
const char *event;
Py_ssize_t event_len;
const char *argFormat;
Py_ssize_t format_len;
PyObject *arg1 = NULL, *arg2 = NULL, *arg3 = NULL;

if (!PyArg_ParseTuple(args, "z#z#|OOO", &event, &event_len, &argFormat, &format_len, &arg1, &arg2, &arg3)) {
return NULL;
}

int result;
if (arg1 == NULL) {
result = PySys_Audit(event, argFormat);
} else if (arg2 == NULL) {
result = PySys_Audit(event, argFormat, arg1);
} else if (arg3 == NULL) {
result = PySys_Audit(event, argFormat, arg1, arg2);
} else {
result = PySys_Audit(event, argFormat, arg1, arg2, arg3);
}

RETURN_INT(result);
}

static PyObject *
sys_audittuple(PyObject *Py_UNUSED(module), PyObject *args)
{
const char *event;
Py_ssize_t event_len;
PyObject *tuple_args = NULL;

if (!PyArg_ParseTuple(args, "z#|O", &event, &event_len, &tuple_args)) {
return NULL;
}

RETURN_INT(PySys_AuditTuple(event, tuple_args));
}

static PyMethodDef test_methods[] = {
{"sys_getattr", sys_getattr, METH_O},
Expand All @@ -115,6 +156,8 @@ static PyMethodDef test_methods[] = {
{"sys_getobject", sys_getobject, METH_O},
{"sys_setobject", sys_setobject, METH_VARARGS},
{"sys_getxoptions", sys_getxoptions, METH_NOARGS},
{"sys_audit", sys_audit, METH_VARARGS},
{"sys_audittuple", sys_audittuple, METH_VARARGS},
{NULL},
};

Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy