Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 240f86d

Browse files
committed
Close #19266: contextlib.ignore -> contextlib.suppress
Patch by Zero Piraeus.
1 parent 1eb509a commit 240f86d

6 files changed

Lines changed: 50 additions & 31 deletions

File tree

Doc/library/contextlib.rst

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,29 +95,39 @@ Functions and classes provided:
9595
``page.close()`` will be called when the :keyword:`with` block is exited.
9696

9797

98-
.. function:: ignore(*exceptions)
98+
.. function:: suppress(*exceptions)
9999

100-
Return a context manager that ignores the specified exceptions if they
101-
occur in the body of a with-statement.
100+
Return a context manager that suppresses any of the specified exceptions
101+
if they occur in the body of a with statement and then resumes execution
102+
with the first statement following the end of the with statement.
102103

103-
As with any other mechanism that completely suppresses exceptions, it
104-
should only be used to cover very specific errors where silently
105-
ignoring the exception is known to be the right thing to do.
104+
As with any other mechanism that completely suppresses exceptions, this
105+
context manager should be used only to cover very specific errors where
106+
silently continuing with program execution is known to be the right
107+
thing to do.
106108

107109
For example::
108110

109-
from contextlib import ignore
111+
from contextlib import suppress
110112

111-
with ignore(FileNotFoundError):
113+
with suppress(FileNotFoundError):
112114
os.remove('somefile.tmp')
113115

116+
with suppress(FileNotFoundError):
117+
os.remove('someotherfile.tmp')
118+
114119
This code is equivalent to::
115120

116121
try:
117122
os.remove('somefile.tmp')
118123
except FileNotFoundError:
119124
pass
120125

126+
try:
127+
os.remove('someotherfile.tmp')
128+
except FileNotFoundError:
129+
pass
130+
121131
.. versionadded:: 3.4
122132

123133

Doc/whatsnew/3.4.rst

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,14 +221,17 @@ results should be less than 1% and may better match results found elsewhere.
221221
contextlib
222222
----------
223223

224-
The new :class:`contextlib.ignore` context manager helps to clarify the
225-
intent of code that deliberately ignores failures from a particular
226-
operation.
224+
The new :class:`contextlib.suppress` context manager helps to clarify the
225+
intent of code that deliberately suppresses exceptions from a single
226+
statement. (Contributed by Raymond Hettinger in :issue:`15806` and
227+
Zero Piraeus in :issue:`19266`)
228+
227229

228230
The new :class:`contextlib.redirect_stdio` context manager makes it easier
229231
for utility scripts to handle inflexible APIs that don't provide any
230232
options to retrieve their output as a string or direct it to somewhere
231-
other than :data:`sys.stdout`.
233+
other than :data:`sys.stdout`. (Contribute by Raymond Hettinger in
234+
:issue:`15805`)
232235

233236

234237
dis
@@ -283,7 +286,7 @@ result: a bytes object containing the fully formatted message.
283286
A pair of new subclasses of :class:`~email.message.Message` have been added,
284287
along with a new sub-module, :mod:`~email.contentmanager`. All documentation
285288
is currently in the new module, which is being added as part of the new
286-
:term:`provisional <provosional package>` email API. These classes provide a
289+
:term:`provisional <provisional package>` email API. These classes provide a
287290
number of new methods that make extracting content from and inserting content
288291
into email messages much easier. See the :mod:`~email.contentmanager`
289292
documentation for details.

Lib/contextlib.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from functools import wraps
66

77
__all__ = ["contextmanager", "closing", "ContextDecorator", "ExitStack",
8-
"ignore", "redirect_stdout"]
8+
"redirect_stdout", "suppress"]
99

1010

1111
class ContextDecorator(object):
@@ -179,10 +179,10 @@ def __exit__(self, exctype, excinst, exctb):
179179
sys.stdout = self.old_target
180180

181181
@contextmanager
182-
def ignore(*exceptions):
183-
"""Context manager to ignore specified exceptions
182+
def suppress(*exceptions):
183+
"""Context manager to suppress specified exceptions
184184
185-
with ignore(OSError):
185+
with suppress(OSError):
186186
os.remove(somefile)
187187
188188
"""

Lib/test/test_contextlib.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -632,36 +632,36 @@ class Example(object): pass
632632
stack.push(cm)
633633
self.assertIs(stack._exit_callbacks[-1], cm)
634634

635-
class TestIgnore(unittest.TestCase):
635+
class TestRedirectStdout(unittest.TestCase):
636+
637+
def test_redirect_to_string_io(self):
638+
f = io.StringIO()
639+
with redirect_stdout(f):
640+
help(pow)
641+
s = f.getvalue()
642+
self.assertIn('pow', s)
643+
644+
class TestSuppress(unittest.TestCase):
636645

637646
def test_no_exception(self):
638647

639-
with ignore(ValueError):
648+
with suppress(ValueError):
640649
self.assertEqual(pow(2, 5), 32)
641650

642651
def test_exact_exception(self):
643652

644-
with ignore(TypeError):
653+
with suppress(TypeError):
645654
len(5)
646655

647656
def test_multiple_exception_args(self):
648657

649-
with ignore(ZeroDivisionError, TypeError):
658+
with suppress(ZeroDivisionError, TypeError):
650659
len(5)
651660

652661
def test_exception_hierarchy(self):
653662

654-
with ignore(LookupError):
663+
with suppress(LookupError):
655664
'Hello'[50]
656665

657-
class TestRedirectStdout(unittest.TestCase):
658-
659-
def test_redirect_to_string_io(self):
660-
f = io.StringIO()
661-
with redirect_stdout(f):
662-
help(pow)
663-
s = f.getvalue()
664-
self.assertIn('pow', s)
665-
666666
if __name__ == "__main__":
667667
unittest.main()

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,6 +1003,7 @@ Anand B. Pillai
10031003
François Pinard
10041004
Tom Pinckney
10051005
Zach Pincus
1006+
Zero Piraeus
10061007
Michael Piotrowski
10071008
Antoine Pitrou
10081009
Jean-François Piéronne

Misc/NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ Core and Builtins
4242
Library
4343
-------
4444

45+
- Issue #19266: Rename the new-in-3.4 ``contextlib.ignore`` context manager
46+
to ``contextlib.suppress`` in order to be more consistent with existing
47+
descriptions of that operation elsewhere in the language and standard
48+
library documentation (Patch by Zero Piraeus)
49+
4550
- Issue #18891: Completed the new email package (provisional) API additions
4651
by adding new classes EmailMessage, MIMEPart, and ContentManager.
4752

0 commit comments

Comments
 (0)