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(); }