https://github.com/python/cpython/commit/0d2b9abd18ff4fc6d2fbf1b398b411ed3b76266c
commit: 0d2b9abd18ff4fc6d2fbf1b398b411ed3b76266c
branch: 3.13
author: Miss Islington (bot) <[email protected]>
committer: sobolevn <[email protected]>
date: 2025-01-09T20:10:08Z
summary:

[3.13] gh-127196: Fix crash in `_interpreters`, when `shared` had invalid 
encodings (GH-127220) (#128689)

gh-127196: Fix crash in `_interpreters`, when `shared` had invalid encodings 
(GH-127220)
(cherry picked from commit 087bb48acac997c06e69dae25bae2dd75194b980)

Co-authored-by: sobolevn <[email protected]>

files:
A Misc/NEWS.d/next/Library/2024-11-24-14-53-35.gh-issue-127196.8CBkUa.rst
M Lib/test/test__interpreters.py
M Modules/_interpretersmodule.c

diff --git a/Lib/test/test__interpreters.py b/Lib/test/test__interpreters.py
index 533120a3221987..8f32a446572d5a 100644
--- a/Lib/test/test__interpreters.py
+++ b/Lib/test/test__interpreters.py
@@ -557,7 +557,7 @@ def setUp(self):
         self.id = _interpreters.create()
 
     def test_signatures(self):
-        # for method in ['exec', 'run_string', 'run_func']:
+        # See https://github.com/python/cpython/issues/126654
         msg = "expected 'shared' to be a dict"
         with self.assertRaisesRegex(TypeError, msg):
             _interpreters.exec(self.id, 'a', 1)
@@ -568,6 +568,17 @@ def test_signatures(self):
         with self.assertRaisesRegex(TypeError, msg):
             _interpreters.run_func(self.id, lambda: None, shared=1)
 
+    def test_invalid_shared_encoding(self):
+        # See https://github.com/python/cpython/issues/127196
+        bad_shared = {"\uD82A": 0}
+        msg = 'surrogates not allowed'
+        with self.assertRaisesRegex(UnicodeEncodeError, msg):
+            _interpreters.exec(self.id, 'a', shared=bad_shared)
+        with self.assertRaisesRegex(UnicodeEncodeError, msg):
+            _interpreters.run_string(self.id, 'a', shared=bad_shared)
+        with self.assertRaisesRegex(UnicodeEncodeError, msg):
+            _interpreters.run_func(self.id, lambda: None, shared=bad_shared)
+
 
 class RunStringTests(TestBase):
 
diff --git 
a/Misc/NEWS.d/next/Library/2024-11-24-14-53-35.gh-issue-127196.8CBkUa.rst 
b/Misc/NEWS.d/next/Library/2024-11-24-14-53-35.gh-issue-127196.8CBkUa.rst
new file mode 100644
index 00000000000000..471f64d185deab
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-11-24-14-53-35.gh-issue-127196.8CBkUa.rst
@@ -0,0 +1,2 @@
+Fix crash when dict with keys in invalid encoding were passed to several
+functions in ``_interpreters`` module.
diff --git a/Modules/_interpretersmodule.c b/Modules/_interpretersmodule.c
index 706316234c7d24..c968b33939c420 100644
--- a/Modules/_interpretersmodule.c
+++ b/Modules/_interpretersmodule.c
@@ -462,7 +462,12 @@ _run_in_interpreter(PyInterpreterState *interp,
 
     // Prep and switch interpreters.
     if (_PyXI_Enter(&session, interp, shareables) < 0) {
-        assert(!PyErr_Occurred());
+        if (PyErr_Occurred()) {
+            // If an error occured at this step, it means that interp
+            // was not prepared and switched.
+            return -1;
+        }
+        // Now, apply the error from another interpreter:
         PyObject *excinfo = _PyXI_ApplyError(session.error);
         if (excinfo != NULL) {
             *p_excinfo = excinfo;

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to