[3.13] gh-132171: Fix _interpreters.run_string crash on string subclass (GH-132173) (#132219)

gh-132171: Fix `_interpreters.run_string` crash on string subclass (GH-132173)
(cherry picked from commit 3980718710)

Co-authored-by: sobolevn <mail@sobolevn.me>
This commit is contained in:
Miss Islington (bot) 2025-04-07 14:23:02 +02:00 committed by GitHub
parent 71b537572a
commit 6afab81f72
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 8 additions and 1 deletions

View file

@ -745,6 +745,12 @@ def test_bytes_for_script(self):
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
_interpreters.run_string(self.id, b'print("spam")') _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): def test_with_shared(self):
r, w = os.pipe() r, w = os.pipe()

View file

@ -0,0 +1 @@
Fix crash of ``_interpreters.run_string`` on string subclasses.

View file

@ -331,7 +331,7 @@ get_code_str(PyObject *arg, Py_ssize_t *len_p, PyObject **bytes_p, int *flags_p)
int flags = 0; int flags = 0;
if (PyUnicode_Check(arg)) { if (PyUnicode_Check(arg)) {
assert(PyUnicode_CheckExact(arg) assert(PyUnicode_Check(arg)
&& (check_code_str((PyUnicodeObject *)arg) == NULL)); && (check_code_str((PyUnicodeObject *)arg) == NULL));
codestr = PyUnicode_AsUTF8AndSize(arg, &len); codestr = PyUnicode_AsUTF8AndSize(arg, &len);
if (codestr == NULL) { if (codestr == NULL) {