https://github.com/python/cpython/commit/d95b7ae6dc8201a1793e2bf09bfa03ca0bc97fa4
commit: d95b7ae6dc8201a1793e2bf09bfa03ca0bc97fa4
branch: main
author: Victor Stinner <[email protected]>
committer: vstinner <[email protected]>
date: 2026-10-05T04:41:29+02:00
summary:

Test MemoryError on locale encoding C functions (#158808)

Fix also two typos.

files:
M Lib/test/test_codecs.py
M Modules/_testlimitedcapi/codec.c

diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py
index 2715ad6d7b3f8a..37ac57063c3539 100644
--- a/Lib/test/test_codecs.py
+++ b/Lib/test/test_codecs.py
@@ -4203,7 +4203,7 @@ def check_decode_strings(self, errors):
                     errmsg = f"decode error: pos={error_pos}"
                     self.assertEqual(str(cm.exception), errmsg)
                 else:
-                    if errors == ("strict", "surrogateescape"):
+                    if errors in ("strict", "surrogateescape"):
                         decoded = self.decode_locale_surrogateescape(encoded)
                         self.assertEqual(decoded, expected)
 
@@ -4233,6 +4233,24 @@ def test_decode_unsupported_error_handler(self):
             self.decode_locale(b'', 'backslashreplace')
         self.assertEqual(str(cm.exception), 'unsupported error handler')
 
+    def test_memory_error(self):
+        # Make sure that MemoryError is handled properly
+        with self.assertRaises(MemoryError):
+            with support.inject_memory_error_cm():
+                self.decode_locale(b'short ascii string', 0, 'strict')
+
+        with self.assertRaises(MemoryError):
+            with support.inject_memory_error_cm():
+                self.decode_locale_surrogateescape(b'short ascii string')
+
+        with self.assertRaises(MemoryError):
+            with support.inject_memory_error_cm():
+                self.encode_locale('short string', 0, 'strict')
+
+        with self.assertRaises(MemoryError):
+            with support.inject_memory_error_cm():
+                self.encode_locale_surrogateescape('short string')
+
 
 class Rot13Test(unittest.TestCase):
     """Test the educational ROT-13 codec."""
diff --git a/Modules/_testlimitedcapi/codec.c b/Modules/_testlimitedcapi/codec.c
index 61e5d4708c71d6..e9c8fed198ebfe 100644
--- a/Modules/_testlimitedcapi/codec.c
+++ b/Modules/_testlimitedcapi/codec.c
@@ -31,7 +31,7 @@ decode_locale(PyObject *Py_UNUSED(module), PyObject *arg)
     size_t size = size_canary;
     wchar_t *wstr = Py_DecodeLocale(str, &size);
 
-    if (str == NULL) {
+    if (wstr == NULL) {
         if (size == (size_t)-1) {
             PyErr_NoMemory();
         }

_______________________________________________
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