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
12 changes: 12 additions & 0 deletions Lib/test/test_memoryio.py
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,18 @@ def test_cow_mutable(self):
memio = self.ioclass(ba)
self.assertEqual(sys.getrefcount(ba), old_rc)

def test_write_with_export(self):
memio = self.ioclass(b"abcd")
memio.seek(2)
with memio.getbuffer() as view:
self.assertRaises(BufferError, memio.__init__, b"replacement")
self.assertEqual(memio.tell(), 2)
self.assertEqual(memio.getvalue(), b"abcd")
self.assertEqual(bytes(view), b"abcd")
memio.write(b"X")
self.assertEqual(memio.getvalue(), b"abXd")


class CStringIOTest(PyStringIOTest):
ioclass = io.StringIO
UnsupportedOperation = io.UnsupportedOperation
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Preserve the position and contents of :class:`io.BytesIO` when
:meth:`!BytesIO.__init__` fails because a buffer is exported.
9 changes: 5 additions & 4 deletions Modules/_io/bytesio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1078,15 +1078,16 @@ static int
_io_BytesIO___init___impl(bytesio *self, PyObject *initvalue)
/*[clinic end generated code: output=65c0c51e24c5b621 input=3da5a74ee4c4f1ac]*/
{
/* In case, __init__ is called multiple times. */
self->string_size = 0;
self->pos = 0;

if (FT_ATOMIC_LOAD_SSIZE_RELAXED(self->exports) > 0) {
PyErr_SetString(PyExc_BufferError,
"Existing exports of data: object cannot be re-sized");
return -1;
}

/* In case, __init__ is called multiple times. */
self->string_size = 0;
self->pos = 0;

if (initvalue && initvalue != Py_None) {
if (PyBytes_CheckExact(initvalue)) {
Py_XSETREF(self->buf, Py_NewRef(initvalue));
Expand Down
Loading