https://github.com/python/cpython/commit/80eec52fc813bc7d20478da3114ec6ffd73e7c31
commit: 80eec52fc813bc7d20478da3114ec6ffd73e7c31
branch: main
author: devdanzin <[email protected]>
committer: JelleZijlstra <[email protected]>
date: 2024-10-27T07:41:42-07:00
summary:

gh-126018: Avoid aborting due to unnecessary assert in `sys.audit` (#126020)


Co-authored-by: Bénédikt Tran <[email protected]>
Co-authored-by: Jelle Zijlstra <[email protected]>

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2024-10-26-23-50-03.gh-issue-126018.Hq-qcM.rst
M Lib/test/audit-tests.py
M Lib/test/test_audit.py
M Python/sysmodule.c

diff --git a/Lib/test/audit-tests.py b/Lib/test/audit-tests.py
index b9021467817f27..6df09d891433ea 100644
--- a/Lib/test/audit-tests.py
+++ b/Lib/test/audit-tests.py
@@ -567,6 +567,17 @@ def hook(event, args):
     _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 7206307d8b0664..ddd9f951143df7 100644
--- a/Lib/test/test_audit.py
+++ b/Lib/test/test_audit.py
@@ -307,5 +307,12 @@ def test_winapi_createnamedpipe(self):
 
         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 00000000000000..e019408638997b
--- /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 8b9209324002ce..24af4798eeac3b 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;

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to