]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-132171: Fix `_interpreters.run_string` crash on string subclass (#132173)
authorsobolevn <mail@sobolevn.me>
Mon, 7 Apr 2025 11:59:44 +0000 (14:59 +0300)
committerGitHub <noreply@github.com>
Mon, 7 Apr 2025 11:59:44 +0000 (14:59 +0300)
Lib/test/test__interpreters.py
Misc/NEWS.d/next/Library/2025-04-06-23-09-21.gh-issue-132171.zZqvfn.rst [new file with mode: 0644]
Modules/_interpretersmodule.c

index 7fba16bafbac37901a85327c42bcc58c7fcc1874..ec376773e40f62a0ba7ead680b592d7bf5512c4c 100644 (file)
@@ -746,6 +746,12 @@ class RunStringTests(TestBase):
         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()
 
diff --git a/Misc/NEWS.d/next/Library/2025-04-06-23-09-21.gh-issue-132171.zZqvfn.rst b/Misc/NEWS.d/next/Library/2025-04-06-23-09-21.gh-issue-132171.zZqvfn.rst
new file mode 100644 (file)
index 0000000..89f34fa
--- /dev/null
@@ -0,0 +1 @@
+Fix crash of ``_interpreters.run_string`` on string subclasses.
index 6cbf026469b4b23a77828df188898ea00be688ba..74f1c02cfab4c964db56e94b2468963146e90e3b 100644 (file)
@@ -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)
                && (check_code_str((PyUnicodeObject *)arg) == NULL));
         codestr = PyUnicode_AsUTF8AndSize(arg, &len);
         if (codestr == NULL) {