From 398071871066548954f4c51e8250d13b7c9659e0 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Mon, 7 Apr 2025 14:59:44 +0300 Subject: [PATCH] gh-132171: Fix `_interpreters.run_string` crash on string subclass (#132173) --- Lib/test/test__interpreters.py | 6 ++++++ .../Library/2025-04-06-23-09-21.gh-issue-132171.zZqvfn.rst | 1 + Modules/_interpretersmodule.c | 2 +- 3 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2025-04-06-23-09-21.gh-issue-132171.zZqvfn.rst diff --git a/Lib/test/test__interpreters.py b/Lib/test/test__interpreters.py index 7fba16bafbac..ec376773e40f 100644 --- a/Lib/test/test__interpreters.py +++ b/Lib/test/test__interpreters.py @@ -746,6 +746,12 @@ class RunStringTests(TestBase): with self.assertRaises(TypeError): _interpreters.run_string(self.id, b'print("spam")') + def test_str_subclass_string(self): + class StrSubclass(str): pass + + output = _run_output(self.id, StrSubclass('print(1 + 2)')) + self.assertEqual(output, '3\n') + def test_with_shared(self): r, w = os.pipe() diff --git a/Misc/NEWS.d/next/Library/2025-04-06-23-09-21.gh-issue-132171.zZqvfn.rst b/Misc/NEWS.d/next/Library/2025-04-06-23-09-21.gh-issue-132171.zZqvfn.rst new file mode 100644 index 000000000000..89f34fa3168c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-04-06-23-09-21.gh-issue-132171.zZqvfn.rst @@ -0,0 +1 @@ +Fix crash of ``_interpreters.run_string`` on string subclasses. diff --git a/Modules/_interpretersmodule.c b/Modules/_interpretersmodule.c index 6cbf026469b4..74f1c02cfab4 100644 --- a/Modules/_interpretersmodule.c +++ b/Modules/_interpretersmodule.c @@ -330,7 +330,7 @@ get_code_str(PyObject *arg, Py_ssize_t *len_p, PyObject **bytes_p, int *flags_p) int flags = 0; if (PyUnicode_Check(arg)) { - assert(PyUnicode_CheckExact(arg) + assert(PyUnicode_Check(arg) && (check_code_str((PyUnicodeObject *)arg) == NULL)); codestr = PyUnicode_AsUTF8AndSize(arg, &len); if (codestr == NULL) { -- 2.47.3