From ba8ac4d398dd37a7e1d221a96cb63da3cb0e4d85 Mon Sep 17 00:00:00 2001 From: devdanzin <74280297+devdanzin@users.noreply.github.com> Date: Sat, 26 Oct 2024 19:31:04 -0300 Subject: [PATCH 1/7] Remove an assert that sys.audit's args[0] passes PyUnicode_Check. --- Python/sysmodule.c | 1 - 1 file changed, 1 deletion(-) 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; From 620f682a980f9b5be3ae77dd645e48d172c85e02 Mon Sep 17 00:00:00 2001 From: devdanzin <74280297+devdanzin@users.noreply.github.com> Date: Sat, 26 Oct 2024 20:38:49 -0300 Subject: [PATCH 2/7] Test that passing a non-string to sys.audit doesn't trigger an assert in debug builds. --- Lib/test/audit-tests.py | 9 +++++++++ Lib/test/test_audit.py | 6 ++++++ 2 files changed, 15 insertions(+) diff --git a/Lib/test/audit-tests.py b/Lib/test/audit-tests.py index b9021467817f27..85636b9e837112 100644 --- a/Lib/test/audit-tests.py +++ b/Lib/test/audit-tests.py @@ -567,6 +567,15 @@ 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: + pass + + 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..8ac9e6bf641d94 100644 --- a/Lib/test/test_audit.py +++ b/Lib/test/test_audit.py @@ -307,5 +307,11 @@ def test_winapi_createnamedpipe(self): self.assertEqual(actual, expected) + def test_assert_unicode(self): + returncode, events, stderr = self.run_python("test_assert_unicode") + if returncode: + self.fail(stderr) + + if __name__ == "__main__": unittest.main() From b775f5d941c8027a8cc29ce406ec1f966d76fc53 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sat, 26 Oct 2024 23:50:08 +0000 Subject: [PATCH 3/7] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2024-10-26-23-50-03.gh-issue-126018.Hq-qcM.rst | 1 + 1 file changed, 1 insertion(+) 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/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..f9dc7a1449d91e --- /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 @@ +Fix a crash in :func:`sys.audit` when passing a non-string as first argument. From 6234c5dfd73241cf40751b39638a215bc9293aff Mon Sep 17 00:00:00 2001 From: devdanzin <74280297+devdanzin@users.noreply.github.com> Date: Sat, 26 Oct 2024 20:53:45 -0300 Subject: [PATCH 4/7] Apply suggestions from code review 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> --- Lib/test/audit-tests.py | 2 +- Lib/test/test_audit.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/audit-tests.py b/Lib/test/audit-tests.py index 85636b9e837112..1a4d20b70ea195 100644 --- a/Lib/test/audit-tests.py +++ b/Lib/test/audit-tests.py @@ -572,7 +572,7 @@ def test_assert_unicode(): sys.addaudithook(lambda *args: None) try: sys.audit(9) - except: + except TypeError: pass diff --git a/Lib/test/test_audit.py b/Lib/test/test_audit.py index 8ac9e6bf641d94..174ffa1b166e80 100644 --- a/Lib/test/test_audit.py +++ b/Lib/test/test_audit.py @@ -308,7 +308,7 @@ def test_winapi_createnamedpipe(self): self.assertEqual(actual, expected) def test_assert_unicode(self): - returncode, events, stderr = self.run_python("test_assert_unicode") + returncode, _, stderr = self.run_python("test_assert_unicode") if returncode: self.fail(stderr) From a6edd2ca507a14f1d908ea00fb17f7dad606ccc8 Mon Sep 17 00:00:00 2001 From: devdanzin <74280297+devdanzin@users.noreply.github.com> Date: Sat, 26 Oct 2024 21:00:33 -0300 Subject: [PATCH 5/7] Apply suggestions from code review by @Eclips4. --- Lib/test/test_audit.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_audit.py b/Lib/test/test_audit.py index 174ffa1b166e80..ddd9f951143df7 100644 --- a/Lib/test/test_audit.py +++ b/Lib/test/test_audit.py @@ -308,6 +308,7 @@ 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) From 57cc87c3c935320e7a5db10f46a4f4ec1c0e12d3 Mon Sep 17 00:00:00 2001 From: devdanzin <74280297+devdanzin@users.noreply.github.com> Date: Sun, 27 Oct 2024 06:47:07 -0300 Subject: [PATCH 6/7] Fail test_assert_unicode if no exception is raised by sys.audit(9) (thanks @JelleZijlstra). --- Lib/test/audit-tests.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Lib/test/audit-tests.py b/Lib/test/audit-tests.py index 1a4d20b70ea195..6df09d891433ea 100644 --- a/Lib/test/audit-tests.py +++ b/Lib/test/audit-tests.py @@ -574,6 +574,8 @@ def test_assert_unicode(): sys.audit(9) except TypeError: pass + else: + raise RuntimeError("Expected sys.audit(9) to fail.") if __name__ == "__main__": From c42c4d654e47d46bad48ad9debf986bbc44c8554 Mon Sep 17 00:00:00 2001 From: devdanzin <74280297+devdanzin@users.noreply.github.com> Date: Sun, 27 Oct 2024 06:48:49 -0300 Subject: [PATCH 7/7] Apply review suggestion for better description of issue in NEWS. Co-authored-by: Jelle Zijlstra --- .../2024-10-26-23-50-03.gh-issue-126018.Hq-qcM.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 index f9dc7a1449d91e..e019408638997b 100644 --- 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 @@ -1 +1,2 @@ -Fix a crash in :func:`sys.audit` when passing a non-string as first argument. +Fix a crash in :func:`sys.audit` when passing a non-string as first argument +and Python was compiled in debug mode.