From 6334396df7e78fea2a8e9cb3804d92e5a9b51be8 Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Wed, 30 Oct 2024 23:41:17 -0400 Subject: [PATCH 01/11] Catch PyUnicode_AsUTF8() errors. --- Modules/_interpretersmodule.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Modules/_interpretersmodule.c b/Modules/_interpretersmodule.c index 6f3392fe6ea28d..fe3e3220173823 100644 --- a/Modules/_interpretersmodule.c +++ b/Modules/_interpretersmodule.c @@ -402,6 +402,11 @@ config_from_object(PyObject *configobj, PyInterpreterConfig *config) } } else if (PyUnicode_Check(configobj)) { + const char *utf8 = PyUnicode_AsUTF8(configobj); + if (utf8 == NULL) + { + return -1; + } if (init_named_config(config, PyUnicode_AsUTF8(configobj)) < 0) { return -1; } From c945cebf6060aa437a727aea0b55426093a150fc Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Wed, 30 Oct 2024 23:42:48 -0400 Subject: [PATCH 02/11] Add blurb --- .../next/Library/2024-10-30-23-42-44.gh-issue-126223.k2qooc.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2024-10-30-23-42-44.gh-issue-126223.k2qooc.rst diff --git a/Misc/NEWS.d/next/Library/2024-10-30-23-42-44.gh-issue-126223.k2qooc.rst b/Misc/NEWS.d/next/Library/2024-10-30-23-42-44.gh-issue-126223.k2qooc.rst new file mode 100644 index 00000000000000..d8c5686c7199d9 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-10-30-23-42-44.gh-issue-126223.k2qooc.rst @@ -0,0 +1,2 @@ +Fixed :exc:`SystemError` upon calling :func:`!_interpreters.create` without +an invalid unicode character. From 9da2977ca2f7b0522417f0dc573b2f09819161a4 Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Wed, 30 Oct 2024 23:46:54 -0400 Subject: [PATCH 03/11] Add simple test. --- Lib/test/test_interpreters/test_api.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Lib/test/test_interpreters/test_api.py b/Lib/test/test_interpreters/test_api.py index 5e3d7a052bae91..16338516738ab8 100644 --- a/Lib/test/test_interpreters/test_api.py +++ b/Lib/test/test_interpreters/test_api.py @@ -54,6 +54,10 @@ def test_in_main(self): self.assertIsInstance(interp, interpreters.Interpreter) self.assertIn(interp, interpreters.list_all()) + # GH-126221: Passing an invalid unicode character used to cause a SystemError + with self.assertRaises(UnicodeEncodeError): + _interpreters.create('\udc80') + def test_in_thread(self): lock = threading.Lock() interp = None From 519cf9b161ffe0d0336c5e2c7edc7fa7c69e7cbb Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Wed, 30 Oct 2024 23:48:25 -0400 Subject: [PATCH 04/11] Remove extra call. --- Modules/_interpretersmodule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/_interpretersmodule.c b/Modules/_interpretersmodule.c index fe3e3220173823..e648930b19f037 100644 --- a/Modules/_interpretersmodule.c +++ b/Modules/_interpretersmodule.c @@ -407,7 +407,7 @@ config_from_object(PyObject *configobj, PyInterpreterConfig *config) { return -1; } - if (init_named_config(config, PyUnicode_AsUTF8(configobj)) < 0) { + if (init_named_config(config, utf8) < 0) { return -1; } } From 2ba24a48b243aa83563326db2506b915f6eabc3b Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Wed, 30 Oct 2024 23:49:54 -0400 Subject: [PATCH 05/11] Fix typo in blurb. --- .../next/Library/2024-10-30-23-42-44.gh-issue-126223.k2qooc.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2024-10-30-23-42-44.gh-issue-126223.k2qooc.rst b/Misc/NEWS.d/next/Library/2024-10-30-23-42-44.gh-issue-126223.k2qooc.rst index d8c5686c7199d9..793440c8393f93 100644 --- a/Misc/NEWS.d/next/Library/2024-10-30-23-42-44.gh-issue-126223.k2qooc.rst +++ b/Misc/NEWS.d/next/Library/2024-10-30-23-42-44.gh-issue-126223.k2qooc.rst @@ -1,2 +1,2 @@ -Fixed :exc:`SystemError` upon calling :func:`!_interpreters.create` without +Fixed :exc:`SystemError` upon calling :func:`!_interpreters.create` with an invalid unicode character. From 4443bbccc39b15e1ce912f126a2f4d3fa466e7be Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Thu, 31 Oct 2024 08:11:39 -0400 Subject: [PATCH 06/11] Update _interpretersmodule.c Co-authored-by: sobolevn --- Modules/_interpretersmodule.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Modules/_interpretersmodule.c b/Modules/_interpretersmodule.c index e648930b19f037..860bbe92fdca5e 100644 --- a/Modules/_interpretersmodule.c +++ b/Modules/_interpretersmodule.c @@ -403,8 +403,7 @@ config_from_object(PyObject *configobj, PyInterpreterConfig *config) } else if (PyUnicode_Check(configobj)) { const char *utf8 = PyUnicode_AsUTF8(configobj); - if (utf8 == NULL) - { + if (utf8 == NULL) { return -1; } if (init_named_config(config, utf8) < 0) { From b937fc7c4ac8d6e9a34e891a9f6bdc6a4815869a Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Thu, 31 Oct 2024 08:18:49 -0400 Subject: [PATCH 07/11] Update test_api.py 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/test_interpreters/test_api.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Lib/test/test_interpreters/test_api.py b/Lib/test/test_interpreters/test_api.py index 16338516738ab8..a61bf9307ffa60 100644 --- a/Lib/test/test_interpreters/test_api.py +++ b/Lib/test/test_interpreters/test_api.py @@ -55,8 +55,7 @@ def test_in_main(self): self.assertIn(interp, interpreters.list_all()) # GH-126221: Passing an invalid unicode character used to cause a SystemError - with self.assertRaises(UnicodeEncodeError): - _interpreters.create('\udc80') + self.assertRaises(UnicodeEncodeError, _interpreters.create, '\udc80') def test_in_thread(self): lock = threading.Lock() From be4e5eb315a71ae61f7f126e8a32331e2a9fe20e Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Thu, 31 Oct 2024 08:19:13 -0400 Subject: [PATCH 08/11] Update test_api.py 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/test_interpreters/test_api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_interpreters/test_api.py b/Lib/test/test_interpreters/test_api.py index a61bf9307ffa60..a8f04f62325169 100644 --- a/Lib/test/test_interpreters/test_api.py +++ b/Lib/test/test_interpreters/test_api.py @@ -54,7 +54,7 @@ def test_in_main(self): self.assertIsInstance(interp, interpreters.Interpreter) self.assertIn(interp, interpreters.list_all()) - # GH-126221: Passing an invalid unicode character used to cause a SystemError + # GH-126221: Passing an invalid Unicode character used to cause a SystemError self.assertRaises(UnicodeEncodeError, _interpreters.create, '\udc80') def test_in_thread(self): From 61c24e46d1b2d0d3e5b9a9f086a26bcfbcb006df Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Thu, 31 Oct 2024 08:19:22 -0400 Subject: [PATCH 09/11] Update _interpretersmodule.c 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> --- Modules/_interpretersmodule.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Modules/_interpretersmodule.c b/Modules/_interpretersmodule.c index 860bbe92fdca5e..63f2bb38768511 100644 --- a/Modules/_interpretersmodule.c +++ b/Modules/_interpretersmodule.c @@ -402,11 +402,11 @@ config_from_object(PyObject *configobj, PyInterpreterConfig *config) } } else if (PyUnicode_Check(configobj)) { - const char *utf8 = PyUnicode_AsUTF8(configobj); - if (utf8 == NULL) { + const char *utf8name = PyUnicode_AsUTF8(configobj); + if (utf8name == NULL) { return -1; } - if (init_named_config(config, utf8) < 0) { + if (init_named_config(config, utf8name) < 0) { return -1; } } From 2ac7f6b0c62c802c05805a9734cdf7c1048175e5 Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Thu, 31 Oct 2024 08:20:31 -0400 Subject: [PATCH 10/11] Update 2024-10-30-23-42-44.gh-issue-126223.k2qooc.rst 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> --- .../Library/2024-10-30-23-42-44.gh-issue-126223.k2qooc.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2024-10-30-23-42-44.gh-issue-126223.k2qooc.rst b/Misc/NEWS.d/next/Library/2024-10-30-23-42-44.gh-issue-126223.k2qooc.rst index 793440c8393f93..fee391c030b941 100644 --- a/Misc/NEWS.d/next/Library/2024-10-30-23-42-44.gh-issue-126223.k2qooc.rst +++ b/Misc/NEWS.d/next/Library/2024-10-30-23-42-44.gh-issue-126223.k2qooc.rst @@ -1,2 +1,2 @@ -Fixed :exc:`SystemError` upon calling :func:`!_interpreters.create` with -an invalid unicode character. +Raise a :exc:`UnicodeEncodeError` instead of a :exc:`SystemError` upon +calling :func:`!_interpreters.create` with an invalid Unicode character. From 9ff672f8074d97b580aa7cc2f67890458b4d1176 Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Thu, 31 Oct 2024 08:56:24 -0400 Subject: [PATCH 11/11] Fix lint. --- Lib/test/test_interpreters/test_api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_interpreters/test_api.py b/Lib/test/test_interpreters/test_api.py index a8f04f62325169..a9befbba64daa0 100644 --- a/Lib/test/test_interpreters/test_api.py +++ b/Lib/test/test_interpreters/test_api.py @@ -55,7 +55,7 @@ def test_in_main(self): self.assertIn(interp, interpreters.list_all()) # GH-126221: Passing an invalid Unicode character used to cause a SystemError - self.assertRaises(UnicodeEncodeError, _interpreters.create, '\udc80') + self.assertRaises(UnicodeEncodeError, _interpreters.create, '\udc80') def test_in_thread(self): lock = threading.Lock()