Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion Lib/test/test_codecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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."""
Expand Down
2 changes: 1 addition & 1 deletion Modules/_testlimitedcapi/codec.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Loading