GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — bpo-18748: io.IOBase destructor now logs close() errors in dev mode by vstinner · Pull Request #12786 · python/cpython

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.

Sign up for GitHub

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
vstinner merged 2 commits into python:master from vstinner:log_io_err_devmode
Apr 12, 2019

Uh oh!

There was an error while loading. Please reload this page.

Merged

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

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
2 commits
Select commit Hold shift + click to select a range
b029db6
bpo-18748: io.IOBase destructor logs close() exceptions
vstinner Apr 11, 2019
e7647da
Fix test_io in debug build
vstinner Apr 12, 2019
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

4 changes: 3 additions & 1 deletion 4 Doc/using/cmdline.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ Miscellaneous options
* Enable :ref:`asyncio debug mode <asyncio-debug-mode>`.
* Set the :attr:`~sys.flags.dev_mode` attribute of :attr:`sys.flags` to
``True``
* :class:`io.IOBase` destructor logs ``close()`` exceptions.

* ``-X utf8`` enables UTF-8 mode for operating system interfaces, overriding
the default locale-aware mode. ``-X utf8=0`` explicitly disables UTF-8
Expand Down Expand Up @@ -465,7 +466,8 @@ Miscellaneous options
The ``-X importtime``, ``-X dev`` and ``-X utf8`` options.

.. versionadded:: 3.8
The ``-X pycache_prefix`` option.
The ``-X pycache_prefix`` option. The ``-X dev`` option now logs
``close()`` exceptions in :class:`io.IOBase` destructor.


Options you shouldn't use
Expand Down
31 changes: 25 additions & 6 deletions 31 Lib/test/test_io.py
Show comments
View file
Open in desktop
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
3 changes: 3 additions & 0 deletions 3 Misc/NEWS.d/next/Library/2019-04-11-16-09-42.bpo-18748.QW7upB.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
In development mode (:option:`-X` ``dev``) and in debug build, the
:class:`io.IOBase` destructor now logs ``close()`` exceptions. These exceptions
are silent by default in release mode.
18 changes: 15 additions & 3 deletions 18 Modules/_io/iobase.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,22 @@ iobase_finalize(PyObject *self)
/* Silencing I/O errors is bad, but printing spurious tracebacks is
equally as bad, and potentially more frequent (because of
shutdown issues). */
if (res == NULL)
PyErr_Clear();
else
if (res == NULL) {
#ifndef Py_DEBUG
const _PyCoreConfig *config = &_PyInterpreterState_GET_UNSAFE()->core_config;
if (config->dev_mode) {
PyErr_WriteUnraisable(self);
}
else {
PyErr_Clear();
}
#else
PyErr_WriteUnraisable(self);
#endif
}
else {
Py_DECREF(res);
}
}

/* Restore the saved exception. */
Expand Down
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.