Skip to content

Commit 3980718

Browse files
authored
gh-132171: Fix _interpreters.run_string crash on string subclass (#132173)
1 parent 895d983 commit 3980718

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
@@ -746,6 +746,12 @@ def test_bytes_for_script(self):
746746
with self.assertRaises(TypeError):
747747
_interpreters.run_string(self.id, b'print("spam")')
748748

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

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
@@ -330,7 +330,7 @@ get_code_str(PyObject *arg, Py_ssize_t *len_p, PyObject **bytes_p, int *flags_p)
330330
int flags = 0;
331331

332332
if (PyUnicode_Check(arg)) {
333-
assert(PyUnicode_CheckExact(arg)
333+
assert(PyUnicode_Check(arg)
334334
&& (check_code_str((PyUnicodeObject *)arg) == NULL));
335335
codestr = PyUnicode_AsUTF8AndSize(arg, &len);
336336
if (codestr == NULL) {

0 commit comments

Comments
 (0)