Skip to content
Merged
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
gh-126018: Avoid aborting due to unnecessary assert in sys.audit (G…
…H-126020)

(cherry picked from commit 80eec52)

Co-authored-by: devdanzin <74280297+devdanzin@users.noreply.github.com>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
  • Loading branch information
3 people authored and miss-islington committed Oct 27, 2024
commit e0eeb7070132b331733870bd236ced0b55b1ac58
11 changes: 11 additions & 0 deletions Lib/test/audit-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 7 additions & 0 deletions Lib/test/test_audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Original file line number Diff line number Diff line change
@@ -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.
1 change: 0 additions & 1 deletion Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,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;
Expand Down