"""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:
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):
_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.
--- /dev/null
+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()``.