Skip to content

Commit 4df0841

Browse files
generatedunixname1734921407115435meta-codesync[bot]
authored andcommitted
Sync pre-release CPython main branch from GitHub (2026-08-08)
Summary: Imported python/cpython `3.16.0a0` from upstream rev [`998b890`](https://www.github.com/python/cpython/commit/998b89020456db591be41e6529b04f4bc8c8181f) (committed 2026-08-08 15:11:23+00:00). # Commit Info - Base: (`3.16.0a0`) - [`8ed1479`](https://www.github.com/python/cpython/commit/8ed1479619a3487af72a7c44bf61c9711370d4da) (commit date: 2026-08-08 01:42:23+00:00) - Imported: (`3.16.0a0`) - [`998b890`](https://www.github.com/python/cpython/commit/998b89020456db591be41e6529b04f4bc8c8181f) (commit date: 2026-08-08 15:11:23+00:00) # Noteworthy file changes - Low-signal files (1 added) (NEWS.d, docs, .github) Complete list of added/removed files: https://www.internalfb.com/intern/everpaste/?color=0&handle=GOf0tS00AVBSZw0IAJdT_nZo3IRxbr0LAAAz Differential Revision: D115339937 fbshipit-source-id: c25a95535c19e6a8fd81f4bab56536b99295e39d
1 parent e2efe7d commit 4df0841

3 files changed

Lines changed: 27 additions & 4 deletions

File tree

Lib/_pyio.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -921,7 +921,7 @@ def getbuffer(self):
921921

922922
def close(self):
923923
if self._buffer is not None:
924-
self._buffer.clear()
924+
self._buffer = bytearray()
925925
super().close()
926926

927927
def read(self, size=-1):

Lib/test/test_io/test_memoryio.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,9 @@ def test_getbuffer(self):
457457
# raises a BufferError.
458458
self.assertRaises(BufferError, memio.write, b'x' * 100)
459459
self.assertRaises(BufferError, memio.truncate)
460-
self.assertRaises(BufferError, memio.close)
460+
# gh-111049: _io.BytesIO detach on close would lead to corruption.
461+
if self.ioclass is io.BytesIO:
462+
self.assertRaises(BufferError, memio.close)
461463
self.assertFalse(memio.closed)
462464
# Mutating the buffer updates the BytesIO
463465
buf[3:6] = b"abc"
@@ -471,6 +473,23 @@ def test_getbuffer(self):
471473
memio.close()
472474
self.assertRaises(ValueError, memio.getbuffer)
473475

476+
def test_getbuffer_delete(self):
477+
# gh-111330: _pyio .close() works and the buffer stays working
478+
if self.ioclass is io.BytesIO:
479+
# gh-111049: _io.BytesIO detach on close would lead to corruption.
480+
# gh-111331: It would be nice to support this.
481+
self.skipTest("io.BytesIO does not support, gh-111049")
482+
483+
memio = self.ioclass(b"1234567890")
484+
buf = memio.getbuffer()
485+
self.assertEqual(bytes(buf), b"1234567890")
486+
memio.close()
487+
self.assertTrue(memio.closed)
488+
self.assertEqual(bytes(buf), b"1234567890")
489+
buf[3:6] = b"abc"
490+
self.assertEqual(bytes(buf), b"123abc7890")
491+
self.assertRaises(ValueError, memio.getbuffer)
492+
474493
def test_getbuffer_empty(self):
475494
memio = self.ioclass()
476495
buf = memio.getbuffer()
@@ -493,9 +512,11 @@ def test_getbuffer_gc_collect(self):
493512
# Create a reference loop.
494513
a = [buf]
495514
a.append(a)
496-
# The Python implementation emits an unraisable exception.
497-
with support.catch_unraisable_exception():
515+
516+
# gh-111330: _pyio GC with exports should pass.
517+
with support.catch_unraisable_exception() as cm:
498518
del memio
519+
self.assertIsNone(cm.unraisable)
499520
del buf
500521
del a
501522
# The C implementation emits an unraisable exception.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Update pure-Python :class:`io.BytesIO` to close cleanly when the data has an
2+
export such as a :class:`memoryview`.

0 commit comments

Comments
 (0)