Skip to content

Commit 88e2f31

Browse files
committed
Test MemoryError on locale encoding C functions
Fix also two typos.
1 parent d19febb commit 88e2f31

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

‎Lib/test/test_codecs.py‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4203,7 +4203,7 @@ def check_decode_strings(self, errors):
42034203
errmsg = f"decode error: pos={error_pos}"
42044204
self.assertEqual(str(cm.exception), errmsg)
42054205
else:
4206-
if errors == ("strict", "surrogateescape"):
4206+
if errors in ("strict", "surrogateescape"):
42074207
decoded = self.decode_locale_surrogateescape(encoded)
42084208
self.assertEqual(decoded, expected)
42094209

@@ -4233,6 +4233,24 @@ def test_decode_unsupported_error_handler(self):
42334233
self.decode_locale(b'', 'backslashreplace')
42344234
self.assertEqual(str(cm.exception), 'unsupported error handler')
42354235

4236+
def test_memory_error(self):
4237+
# Make sure that MemoryError is handled properly
4238+
with self.assertRaises(MemoryError):
4239+
with support.inject_memory_error_cm():
4240+
self.decode_locale(b'short ascii string', 0, 'strict')
4241+
4242+
with self.assertRaises(MemoryError):
4243+
with support.inject_memory_error_cm():
4244+
self.decode_locale_surrogateescape(b'short ascii string')
4245+
4246+
with self.assertRaises(MemoryError):
4247+
with support.inject_memory_error_cm():
4248+
self.encode_locale('short string', 0, 'strict')
4249+
4250+
with self.assertRaises(MemoryError):
4251+
with support.inject_memory_error_cm():
4252+
self.encode_locale_surrogateescape('short string')
4253+
42364254

42374255
class Rot13Test(unittest.TestCase):
42384256
"""Test the educational ROT-13 codec."""

‎Modules/_testlimitedcapi/codec.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ decode_locale(PyObject *Py_UNUSED(module), PyObject *arg)
3131
size_t size = size_canary;
3232
wchar_t *wstr = Py_DecodeLocale(str, &size);
3333

34-
if (str == NULL) {
34+
if (wstr == NULL) {
3535
if (size == (size_t)-1) {
3636
PyErr_NoMemory();
3737
}

0 commit comments

Comments
 (0)