Skip to content

Commit 9e3dd1a

Browse files
committed
gh-111330: Fix Gc of _pyio.BytesIO with exports
Swap out the buffer rather than using `.clear()` which will raise an exception if there are epxorts.
1 parent bbf7786 commit 9e3dd1a

3 files changed

Lines changed: 7 additions & 3 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: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -493,9 +493,11 @@ def test_getbuffer_gc_collect(self):
493493
# Create a reference loop.
494494
a = [buf]
495495
a.append(a)
496-
# The Python implementation emits an unraisable exception.
497-
with support.catch_unraisable_exception():
496+
497+
# gh-111330: _pyio GC with exports should pass.
498+
with support.catch_unraisable_exception() as cm:
498499
del memio
500+
self.assertIsNone(cm.unraisable)
499501
del buf
500502
del a
501503
# 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)