8000 bpo-36829: Document test.support.catch_unraisable_exception() by vstinner · Pull Request #13554 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-36829: Document test.support.catch_unraisable_exception() #13554

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 1 commit into from
May 24, 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
20 changes: 20 additions & 0 deletions Doc/library/test.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1081,6 +1081,26 @@ The :mod:`test.support` module defines the following functions:
:exc:`PermissionError` is raised.


.. function:: catch_unraisable_exception()

Context manager catching unraisable exception using
:func:`sys.unraisablehook`.

Usage::

with support.catch_unraisable_exception() as cm:
# code creating an "unraisable exception"
...

# check the unraisable exception: use cm.unraisable
...

# cm.unraisable attribute no longer exists at this point
# (to break a reference cycle)

.. versionadded:: 3.8


.. function:: find_unused_port(family=socket.AF_INET, socktype=socket.SOCK_STREAM)

Returns an unused port that should be suitable for binding. This is
Expand Down
8 changes: 5 additions & 3 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3043,12 +3043,14 @@ class catch_unraisable_exception:
Usage:

with support.catch_unraisable_exception() as cm:
# code creating an "unraisable exception"
...

# check the expected unraisable exception: use cm.unraisable
# check the unraisable exception: use cm.unraisable
...

# cm.unraisable is None here (to break a reference cycle)
# cm.unraisable attribute no longer exists at this point
# (to break a reference cycle)
"""

def __init__(self):
Expand All @@ -3065,5 +3067,5 @@ def __enter__(self):

def __exit__(self, *exc_info):
# Clear the unraisable exception to explicitly break a reference cycle
self.unraisable = None
del self.unraisable
sys.unraisablehook = self._old_hook
20 changes: 6 additions & 14 deletions Lib/test/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -1098,18 +1098,14 @@ def test_error_through_destructor(self):
# Test that the exception state is not modified by a destructor,
# even if close() fails.
rawio = self.CloseFailureIO()
try:
with support.catch_unraisable_exception() as cm:
with self.assertRaises(AttributeError):
self.tp(rawio).xyzzy
with support.catch_unraisable_exception() as cm:
with self.assertRaises(AttributeError):
self.tp(rawio).xyzzy

if not IOBASE_EMITS_UNRAISABLE:
self.assertIsNone(cm.unraisable)
elif cm.unraisable is not None:
self.assertEqual(cm.unraisable.exc_type, OSError)
finally:
# Explicitly break reference cycle
cm = None

def test_repr(self):
raw = self.MockRawIO()
Expand Down Expand Up @@ -2854,18 +2850,14 @@ def test_error_through_destructor(self):
# Test that the exception state is not modified by a destructor,
# even if close() fails.
rawio = self.CloseFailureIO()
try:
with support.catch_unraisable_exception() as cm:
with self.assertRaises(AttributeError):
self.TextIOWrapper(rawio).xyzzy
with support.catch_unraisable_exception() as cm:
with self.assertRaises(AttributeError):
self.TextIOWrapper(rawio).xyzzy

if not IOBASE_EMITS_UNRAISABLE:
self.assertIsNone(cm.unraisable)
elif cm.unraisable is not None:
self.assertEqual(cm.unraisable.exc_type, OSError)
finally:
# Explicitly break reference cycle
cm = None

# Systematic tests of the text I/O API

Expand Down
0