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
11 changes: 10 additions & 1 deletion Lib/_pyio.py
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,10 @@ class BytesIO(BufferedIOBase):

"""Buffered I/O implementation using an in-memory bytes buffer."""

# Initialize _buffer as soon as possible since it's used by __del__()
# which calls close()
_buffer = None

def __init__(self, initial_bytes=None):
buf = bytearray()
if initial_bytes is not None:
Expand Down Expand Up @@ -900,7 +904,8 @@ def getbuffer(self):
return memoryview(self._buffer)

def close(self):
self._buffer.clear()
if self._buffer is not None:
self._buffer.clear()
super().close()

def read(self, size=-1):
Expand Down Expand Up @@ -1970,6 +1975,10 @@ class TextIOWrapper(TextIOBase):

_CHUNK_SIZE = 2048

# Initialize _buffer as soon as possible since it's used by __del__()
# which calls close()
_buffer = None

# The write_through argument has no effect here since this
# implementation always writes through. The argument is present only
# so that the signature can match the signature of the C version.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix destructor :class:`_pyio.BytesIO` and :class:`_pyio.TextIOWrapper`:
initialize their ``_buffer`` attribute as soon as possible (in the class
body), because it's used by ``__del__()`` which calls ``close()``.