]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-126018: Avoid aborting due to unnecessary assert in `sys.audit` (#126020)
authordevdanzin <74280297+devdanzin@users.noreply.github.com>
Sun, 27 Oct 2024 14:41:42 +0000 (11:41 -0300)
committerGitHub <noreply@github.com>
Sun, 27 Oct 2024 14:41:42 +0000 (07:41 -0700)
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Lib/test/audit-tests.py
Lib/test/test_audit.py
Misc/NEWS.d/next/Core_and_Builtins/2024-10-26-23-50-03.gh-issue-126018.Hq-qcM.rst [new file with mode: 0644]
Python/sysmodule.c

index b9021467817f27c127d50709bee40050da33fdc0..6df09d891433ea99c9bb451b450ec6335f8e45aa 100644 (file)
@@ -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
 
index 7206307d8b0664daabeeb4157314524b9a2b3277..ddd9f951143df7d9acf11474fa461b99489451ea 100644 (file)
@@ -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 (file)
index 0000000..e019408
--- /dev/null
@@ -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.
index 8b9209324002cefffe1341762c7ee5a980e5cbac..24af4798eeac3bcb7452f93ce2285c8b7334a84a 100644 (file)
@@ -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;