8000 bpo-18748: io.IOBase destructor now logs close() errors in dev mode by vstinner · Pull Request #12786 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-18748: io.IOBase destructor now logs close() errors in dev mode #12786

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 2 commits into from
Apr 12, 2019
Merged
Changes from 1 commit
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
Prev Previous commit
Fix test_io in debug build
  • Loading branch information
vstinner committed Apr 12, 2019
commit e7647da3f182acbc5b2d3d22eba261cecf90e282
31 changes: 25 additions & 6 deletions Lib/test/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ class EmptyStruct(ctypes.Structure):
'--with-memory-sanitizer' in _config_args
)

# Does io.IOBase logs unhandled exceptions on calling close()?
# They are silenced by default in release build.
DESTRUCTOR_LOG_ERRORS = (hasattr(sys, "gettotalrefcount") or sys.flags.dev_mode)


def _default_chunk_size():
"""Get the default TextIOWrapper chunk size"""
with open(__file__, "r", encoding="latin-1") as f:
Expand Down Expand Up @@ -1097,9 +1102,16 @@ def f():
s = s.getvalue().strip()
if s:
# The destructor *may* have printed an unraisable error, check it
self.assertEqual(len(s.splitlines()), 1)
self.assertTrue(s.startswith("Exception OSError: "), s)
self.assertTrue(s.endswith(" ignored"), s)
lines = s.splitlines()
if DESTRUCTOR_LOG_ERRORS:
self.assertEqual(len(lines), 5)
self.assertTrue(lines[0].startswith("Exception ignored in: "), lines)
self.assertEqual(lines[1], "Traceback (most recent call last):", lines)
self.assertEqual(lines[4], 'OSError:', lines)
else:
self.assertEqual(len(lines), 1)
self.assertTrue(lines[-1].startswith("Exception OSError: "), lines)
self.assertTrue(lines[-1].endswith(" ignored"), lines)

def test_repr(self):
raw = self.MockRawIO()
Expand Down Expand Up @@ -2833,9 +2845,16 @@ def f():
s = s.getvalue().strip()
if s:
# The destructor *may* have printed an unraisable error, check it
self.assertEqual(len(s.splitlines()), 1)
self.assertTrue(s.startswith("Exception OSError: "), s)
self.assertTrue(s.endswith(" ignored"), s)
lines = s.splitlines()
if DESTRUCTOR_LOG_ERRORS:
self.assertEqual(len(lines), 5)
self.assertTrue(lines[0].startswith("Exception ignored in: "), lines)
self.assertEqual(lines[1], "Traceback (most recent call last):", lines)
self.assertEqual(lines[4], 'OSError:', lines)
else:
self.assertEqual(len(lines), 1)
self.assertTrue(lines[-1].startswith("Exception OSError: "), lines)
self.assertTrue(lines[-1].endswith(" ignored"), lines)

# Systematic tests of the text I/O API

Expand Down
0