8000 [3.8] bpo-37223: test_io: silence destructor errors by vstinner · Pull Request #14031 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

[3.8] bpo-37223: test_io: silence destructor errors #14031

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 12, 2019
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
10 changes: 10 additions & 0 deletions Lib/_pyio.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,16 @@ def close(self):

def __del__(self):
"""Destructor. Calls close()."""
try:
closed = self.closed
except Exception:
# If getting closed fails, then the object is probably
# in an unusable state, so ignore.
return

if closed:
return

if _IOBASE_EMITS_UNRAISABLE:
self.close()
else:
Expand Down
15 changes: 15 additions & 0 deletions Lib/test/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,10 @@ def readable(self):
def seekable(self):
return True

def seek(self, pos, whence=0):
# naive implementation, enough for tests
return 0

def writable(self):
return True

Expand Down Expand Up @@ -1486,6 +1490,9 @@ def test_misbehaved_io(self):
self.assertRaises(OSError, bufio.seek, 0)
self.assertRaises(OSError, bufio.tell)

# Silence destructor error
bufio.close = lambda: None

def test_no_extraneous_read(self):
# Issue #9550; when the raw IO object has satisfied the read request,
# we should not issue any additional reads, otherwise it may block
Expand Down Expand Up @@ -1834,6 +1841,9 @@ def test_misbehaved_io(self):
self.assertRaises(OSError, bufio.tell)
self.assertRaises(OSError, bufio.write, b"abcdef")

# Silence destructor error
bufio.close = lambda: None

def test_max_buffer_size_removal(self):
with self.assertRaises(TypeError):
self.tp(self.MockRawIO(), 8, 12)
Expand Down Expand Up @@ -2060,6 +2070,11 @@ def writer_close():

# Silence destructor error
writer.close = lambda: None
writer = None

with support.catch_unraisable_exception():
pair = None
support.gc_collect()

def test_reader_writer_close_error_on_close(self):
def reader_close():
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:class:`_pyio.IOBase` destructor now does nothing if getting the ``closed``
attribute fails to better mimick :class:`_io.IOBase` finalizer.
0