Skip to content

Commit 6afab81

Browse files
[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 3980718) Co-authored-by: sobolevn <[email protected]>
1 parent 71b5375 commit 6afab81

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

Lib/test/test__interpreters.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,12 @@ def test_bytes_for_script(self):
745745
with self.assertRaises(TypeError):
746746
_interpreters.run_string(self.id, b'print("spam")')
747747

748+
def test_str_subclass_string(self):
749+
class StrSubclass(str): pass
750+
751+
output = _run_output(self.id, StrSubclass('print(1 + 2)'))
752+
self.assertEqual(output, '3\n')
753+
748754
def test_with_shared(self):
749755
r, w = os.pipe()
750756

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix crash of ``_interpreters.run_string`` on string subclasses.

Modules/_interpretersmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ get_code_str(PyObject *arg, Py_ssize_t *len_p, PyObject **bytes_p, int *flags_p)
331331
int flags = 0;
332332

333333
if (PyUnicode_Check(arg)) {
334-
assert(PyUnicode_CheckExact(arg)
334+
assert(PyUnicode_Check(arg)
335335
&& (check_code_str((PyUnicodeObject *)arg) == NULL));
336336
codestr = PyUnicode_AsUTF8AndSize(arg, &len);
337337
if (codestr == NULL) {

0 commit comments

Comments
 (0)