From 80eec52fc813bc7d20478da3114ec6ffd73e7c31 Mon Sep 17 00:00:00 2001 From: devdanzin <74280297+devdanzin@users.noreply.github.com> Date: Sun, 27 Oct 2024 11:41:42 -0300 Subject: [PATCH] gh-126018: Avoid aborting due to unnecessary assert in `sys.audit` (#126020) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> Co-authored-by: Jelle Zijlstra --- Lib/test/audit-tests.py | 11 +++++++++++ Lib/test/test_audit.py | 7 +++++++ .../2024-10-26-23-50-03.gh-issue-126018.Hq-qcM.rst | 2 ++ Python/sysmodule.c | 1 - 4 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2024-10-26-23-50-03.gh-issue-126018.Hq-qcM.rst diff --git a/Lib/test/audit-tests.py b/Lib/test/audit-tests.py index b9021467817..6df09d89143 100644 --- a/Lib/test/audit-tests.py +++ b/Lib/test/audit-tests.py @@ -567,6 +567,17 @@ def test_winapi_createnamedpipe(pipe_name): _winapi.CreateNamedPipe(pipe_name, _winapi.PIPE_ACCESS_DUPLEX, 8, 2, 0, 0, 0, 0) +def test_assert_unicode(): + import sys + sys.addaudithook(lambda *args: None) + try: + sys.audit(9) + except TypeError: + pass + else: + raise RuntimeError("Expected sys.audit(9) to fail.") + + if __name__ == "__main__": from test.support import suppress_msvcrt_asserts diff --git a/Lib/test/test_audit.py b/Lib/test/test_audit.py index 7206307d8b0..ddd9f951143 100644 --- a/Lib/test/test_audit.py +++ b/Lib/test/test_audit.py @@ -307,5 +307,12 @@ class AuditTest(unittest.TestCase): self.assertEqual(actual, expected) + def test_assert_unicode(self): + # See gh-126018 + returncode, _, stderr = self.run_python("test_assert_unicode") + if returncode: + self.fail(stderr) + + if __name__ == "__main__": unittest.main() diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2024-10-26-23-50-03.gh-issue-126018.Hq-qcM.rst b/Misc/NEWS.d/next/Core_and_Builtins/2024-10-26-23-50-03.gh-issue-126018.Hq-qcM.rst new file mode 100644 index 00000000000..e0194086389 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2024-10-26-23-50-03.gh-issue-126018.Hq-qcM.rst @@ -0,0 +1,2 @@ +Fix a crash in :func:`sys.audit` when passing a non-string as first argument +and Python was compiled in debug mode. diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 8b920932400..24af4798eea 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -519,7 +519,6 @@ sys_audit(PyObject *self, PyObject *const *args, Py_ssize_t argc) } assert(args[0] != NULL); - assert(PyUnicode_Check(args[0])); if (!should_audit(tstate->interp)) { Py_RETURN_NONE;