mirror of
https://github.com/python/cpython.git
synced 2025-10-09 16:34:44 +00:00
bpo-37054, _pyio: Fix BytesIO and TextIOWrapper __del__() (GH-13601)
Fix destructor _pyio.BytesIO and _pyio.TextIOWrapper: initialize their _buffer attribute as soon as possible (in the class body), because it's used by __del__() which calls close().
This commit is contained in:
parent
df9b032f47
commit
a3568417c4
2 changed files with 13 additions and 1 deletions
11
Lib/_pyio.py
11
Lib/_pyio.py
|
@ -873,6 +873,10 @@ class BytesIO(BufferedIOBase):
|
||||||
|
|
||||||
"""Buffered I/O implementation using an in-memory bytes buffer."""
|
"""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):
|
def __init__(self, initial_bytes=None):
|
||||||
buf = bytearray()
|
buf = bytearray()
|
||||||
if initial_bytes is not None:
|
if initial_bytes is not None:
|
||||||
|
@ -900,7 +904,8 @@ class BytesIO(BufferedIOBase):
|
||||||
return memoryview(self._buffer)
|
return memoryview(self._buffer)
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
self._buffer.clear()
|
if self._buffer is not None:
|
||||||
|
self._buffer.clear()
|
||||||
super().close()
|
super().close()
|
||||||
|
|
||||||
def read(self, size=-1):
|
def read(self, size=-1):
|
||||||
|
@ -1970,6 +1975,10 @@ class TextIOWrapper(TextIOBase):
|
||||||
|
|
||||||
_CHUNK_SIZE = 2048
|
_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
|
# The write_through argument has no effect here since this
|
||||||
# implementation always writes through. The argument is present only
|
# implementation always writes through. The argument is present only
|
||||||
# so that the signature can match the signature of the C version.
|
# so that the signature can match the signature of the C version.
|
||||||
|
|
|
@ -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()``.
|
Loading…
Add table
Add a link
Reference in a new issue