Skip to content

Commit 80eec52

Browse files
devdanzinpicnixzJelleZijlstra
authored
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]>
1 parent dc76a4a commit 80eec52

File tree

4 files changed

+20
-1
lines changed

4 files changed

+20
-1
lines changed

Lib/test/audit-tests.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,17 @@ def hook(event, args):
567567
_winapi.CreateNamedPipe(pipe_name, _winapi.PIPE_ACCESS_DUPLEX, 8, 2, 0, 0, 0, 0)
568568

569569

570+
def test_assert_unicode():
571+
import sys
572+
sys.addaudithook(lambda *args: None)
573+
try:
574+
sys.audit(9)
575+
except TypeError:
576+
pass
577+
else:
578+
raise RuntimeError("Expected sys.audit(9) to fail.")
579+
580+
570581
if __name__ == "__main__":
571582
from test.support import suppress_msvcrt_asserts
572583

Lib/test/test_audit.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,5 +307,12 @@ def test_winapi_createnamedpipe(self):
307307

308308
self.assertEqual(actual, expected)
309309

310+
def test_assert_unicode(self):
311+
# See gh-126018
312+
returncode, _, stderr = self.run_python("test_assert_unicode")
313+
if returncode:
314+
self.fail(stderr)
315+
316+
310317
if __name__ == "__main__":
311318
unittest.main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a crash in :func:`sys.audit` when passing a non-string as first argument
2+
and Python was compiled in debug mode.

Python/sysmodule.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,6 @@ sys_audit(PyObject *self, PyObject *const *args, Py_ssize_t argc)
519519
}
520520

521521
assert(args[0] != NULL);
522-
assert(PyUnicode_Check(args[0]));
523522

524523
if (!should_audit(tstate->interp)) {
525524
Py_RETURN_NONE;

0 commit comments

Comments
 (0)