]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-132171: Fix `_interpreters.run_string` crash on string subclass (GH-132173...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 7 Apr 2025 12:23:02 +0000 (14:23 +0200)
committerGitHub <noreply@github.com>
Mon, 7 Apr 2025 12:23:02 +0000 (12:23 +0000)
gh-132171: Fix `_interpreters.run_string` crash on string subclass (GH-132173)
(cherry picked from commit 398071871066548954f4c51e8250d13b7c9659e0)

Co-authored-by: sobolevn <mail@sobolevn.me>
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 8f32a446572d5a865b2fb655ecad3e1b5a91ce7e..3de774ae8b012ea786fdece8746332b3f9517a7e 100644 (file)
@@ -745,6 +745,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 c968b33939c4202921782dc4a0e8b43843f62437..8f912d803ba519aabed43852483e1cc0ee897884 100644 (file)
@@ -331,7 +331,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) {