8000 gh-132171: Fix `_interpreters.run_string` crash on string subclass by sobolevn · Pull Request #132173 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-132171: Fix _interpreters.run_string crash on string subclass #132173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 7, 2025
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
0