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
30 changes: 30 additions & 0 deletions Lib/test/test_io/test_memoryio.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import unittest
from test import support
from test.support import import_helper

import gc
import io
Expand Down Expand Up @@ -753,6 +754,35 @@ def __buffer__(self, flags):
self.assertEqual(memio.getvalue(), b"01AAA56789")
self.assertEqual(memio.tell(), 5)

def test_memory_error(self):
# gh-157242: io.BytesIO() must not close the file on MemoryError
_testcapi = import_helper.import_module('_testcapi')

# write()
stream = self.ioclass()
stream.write(self.buftype('abc'))
with self.assertRaises(MemoryError):
try:
data = self.buftype('def')
_testcapi.set_nomemory(0)
stream.write(data)
finally:
_testcapi.remove_mem_hooks()
stream.write(self.buftype('123'))
self.assertEqual(stream.getvalue(), self.buftype('abc123'))

# truncate()
data = self.buftype('x' * 100)
stream = self.ioclass()
stream.write(data)
with self.assertRaises(MemoryError):
try:
_testcapi.set_nomemory(0)
stream.truncate(5)
finally:
_testcapi.remove_mem_hooks()
self.assertEqual(stream.getvalue(), data)


class TextIOTestMixin:

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:class:`io.BytesIO` is no longer closed on ``write()`` and ``truncate()``
failure (:exc:`MemoryError`). Patch by Victor Stinner.
8 changes: 6 additions & 2 deletions Modules/_io/bytesio.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "Python.h"
#include "pycore_bytesobject.h" // _PyBytes_ResizeKeepOnError()
#include "pycore_critical_section.h" // Py_BEGIN_CRITICAL_SECTION()
#include "pycore_object.h"
#include "pycore_pyatomic_ft_wrappers.h"
Expand Down Expand Up @@ -108,7 +109,7 @@ resize_unshared_buffer_lock_held(bytesio *self, Py_ssize_t size)
Callers must detach first. */
assert(!self->buf_shared);
#endif
int ret = _PyBytes_Resize(&self->buf, size);
int ret = _PyBytes_ResizeKeepOnError(&self->buf, size);
if (ret == 0) {
clear_shared_buf(self);
}
Expand Down Expand Up @@ -758,9 +759,12 @@ _io_BytesIO_truncate_impl(bytesio *self, PyObject *size)
}

if (new_size < self->string_size) {
Py_ssize_t old_string_size = self->string_size;
self->string_size = new_size;
if (resize_buffer_lock_held(self, new_size) < 0)
if (resize_buffer_lock_held(self, new_size) < 0) {
self->string_size = old_string_size;
return NULL;
}
}

return PyLong_FromSsize_t(new_size);
Expand Down
Loading