Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Lib/test/test__interpreters.py
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,12 @@ def test_bytes_for_script(self):
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()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix crash of ``_interpreters.run_string`` on string subclasses.
2 changes: 1 addition & 1 deletion Modules/_interpretersmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check is already happening in the if condition, then why do the check again? Shouldn't we remove it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In hindsight, sure. But I don't think it's worth changing it now; it's not wrong, it's just redundant.

&& (check_code_str((PyUnicodeObject *)arg) == NULL));
codestr = PyUnicode_AsUTF8AndSize(arg, &len);
if (codestr == NULL) {
Expand Down
Loading