Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
d77ac11b
Commit
d77ac11b
authored
Oct 17, 2013
by
Nick Coghlan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Close #19266: contextlib.ignore -> contextlib.suppress
Patch by Zero Piraeus.
parent
08a528b7
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
50 additions
and
31 deletions
+50
-31
Doc/library/contextlib.rst
Doc/library/contextlib.rst
+18
-8
Doc/whatsnew/3.4.rst
Doc/whatsnew/3.4.rst
+8
-5
Lib/contextlib.py
Lib/contextlib.py
+4
-4
Lib/test/test_contextlib.py
Lib/test/test_contextlib.py
+14
-14
Misc/ACKS
Misc/ACKS
+1
-0
Misc/NEWS
Misc/NEWS
+5
-0
No files found.
Doc/library/contextlib.rst
View file @
d77ac11b
...
@@ -95,22 +95,27 @@ Functions and classes provided:
...
@@ -95,22 +95,27 @@ Functions and classes provided:
``page.close()`` will be called when the :keyword:`with` block is exited.
``page.close()`` will be called when the :keyword:`with` block is exited.
.. function::
ignore
(*exceptions)
.. function::
suppress
(*exceptions)
Return a context manager that ignores the specified exceptions if they
Return a context manager that suppresses any of the specified exceptions
occur in the body of a with-statement.
if they occur in the body of a with statement and then resumes execution
with the first statement following the end of the with statement.
As with any other mechanism that completely suppresses exceptions, it
As with any other mechanism that completely suppresses exceptions, this
should only be used to cover very specific errors where silently
context manager should be used only to cover very specific errors where
ignoring the exception is known to be the right thing to do.
silently continuing with program execution is known to be the right
thing to do.
For example::
For example::
from contextlib import
ignore
from contextlib import
suppress
with
ignore
(FileNotFoundError):
with
suppress
(FileNotFoundError):
os.remove('somefile.tmp')
os.remove('somefile.tmp')
with suppress(FileNotFoundError):
os.remove('someotherfile.tmp')
This code is equivalent to::
This code is equivalent to::
try:
try:
...
@@ -118,6 +123,11 @@ Functions and classes provided:
...
@@ -118,6 +123,11 @@ Functions and classes provided:
except FileNotFoundError:
except FileNotFoundError:
pass
pass
try:
os.remove('someotherfile.tmp')
except FileNotFoundError:
pass
.. versionadded:: 3.4
.. versionadded:: 3.4
...
...
Doc/whatsnew/3.4.rst
View file @
d77ac11b
...
@@ -221,14 +221,17 @@ results should be less than 1% and may better match results found elsewhere.
...
@@ -221,14 +221,17 @@ results should be less than 1% and may better match results found elsewhere.
contextlib
contextlib
----------
----------
The new :class:`contextlib.ignore` context manager helps to clarify the
The new :class:`contextlib.suppress` context manager helps to clarify the
intent of code that deliberately ignores failures from a particular
intent of code that deliberately suppresses exceptions from a single
operation.
statement. (Contributed by Raymond Hettinger in :issue:`15806` and
Zero Piraeus in :issue:`19266`)
The new :class:`contextlib.redirect_stdio` context manager makes it easier
The new :class:`contextlib.redirect_stdio` context manager makes it easier
for utility scripts to handle inflexible APIs that don't provide any
for utility scripts to handle inflexible APIs that don't provide any
options to retrieve their output as a string or direct it to somewhere
options to retrieve their output as a string or direct it to somewhere
other than :data:`sys.stdout`.
other than :data:`sys.stdout`. (Contribute by Raymond Hettinger in
:issue:`15805`)
dis
dis
...
@@ -283,7 +286,7 @@ result: a bytes object containing the fully formatted message.
...
@@ -283,7 +286,7 @@ result: a bytes object containing the fully formatted message.
A pair of new subclasses of :class:`~email.message.Message` have been added,
A pair of new subclasses of :class:`~email.message.Message` have been added,
along with a new sub-module, :mod:`~email.contentmanager`. All documentation
along with a new sub-module, :mod:`~email.contentmanager`. All documentation
is currently in the new module, which is being added as part of the new
is currently in the new module, which is being added as part of the new
:term:`provisional <prov
o
sional package>` email API. These classes provide a
:term:`provisional <prov
i
sional package>` email API. These classes provide a
number of new methods that make extracting content from and inserting content
number of new methods that make extracting content from and inserting content
into email messages much easier. See the :mod:`~email.contentmanager`
into email messages much easier. See the :mod:`~email.contentmanager`
documentation for details.
documentation for details.
...
...
Lib/contextlib.py
View file @
d77ac11b
...
@@ -5,7 +5,7 @@ from collections import deque
...
@@ -5,7 +5,7 @@ from collections import deque
from
functools
import
wraps
from
functools
import
wraps
__all__
=
[
"contextmanager"
,
"closing"
,
"ContextDecorator"
,
"ExitStack"
,
__all__
=
[
"contextmanager"
,
"closing"
,
"ContextDecorator"
,
"ExitStack"
,
"
ignore"
,
"redirect_stdout
"
]
"
redirect_stdout"
,
"suppress
"
]
class
ContextDecorator
(
object
):
class
ContextDecorator
(
object
):
...
@@ -179,10 +179,10 @@ class redirect_stdout:
...
@@ -179,10 +179,10 @@ class redirect_stdout:
sys
.
stdout
=
self
.
old_target
sys
.
stdout
=
self
.
old_target
@
contextmanager
@
contextmanager
def
ignore
(
*
exceptions
):
def
suppress
(
*
exceptions
):
"""Context manager to
ignore
specified exceptions
"""Context manager to
suppress
specified exceptions
with
ignore
(OSError):
with
suppress
(OSError):
os.remove(somefile)
os.remove(somefile)
"""
"""
...
...
Lib/test/test_contextlib.py
View file @
d77ac11b
...
@@ -632,36 +632,36 @@ class TestExitStack(unittest.TestCase):
...
@@ -632,36 +632,36 @@ class TestExitStack(unittest.TestCase):
stack
.
push
(
cm
)
stack
.
push
(
cm
)
self
.
assertIs
(
stack
.
_exit_callbacks
[
-
1
],
cm
)
self
.
assertIs
(
stack
.
_exit_callbacks
[
-
1
],
cm
)
class
TestIgnore
(
unittest
.
TestCase
):
class
TestRedirectStdout
(
unittest
.
TestCase
):
def
test_redirect_to_string_io
(
self
):
f
=
io
.
StringIO
()
with
redirect_stdout
(
f
):
help
(
pow
)
s
=
f
.
getvalue
()
self
.
assertIn
(
'pow'
,
s
)
class
TestSuppress
(
unittest
.
TestCase
):
def
test_no_exception
(
self
):
def
test_no_exception
(
self
):
with
ignore
(
ValueError
):
with
suppress
(
ValueError
):
self
.
assertEqual
(
pow
(
2
,
5
),
32
)
self
.
assertEqual
(
pow
(
2
,
5
),
32
)
def
test_exact_exception
(
self
):
def
test_exact_exception
(
self
):
with
ignore
(
TypeError
):
with
suppress
(
TypeError
):
len
(
5
)
len
(
5
)
def
test_multiple_exception_args
(
self
):
def
test_multiple_exception_args
(
self
):
with
ignore
(
ZeroDivisionError
,
TypeError
):
with
suppress
(
ZeroDivisionError
,
TypeError
):
len
(
5
)
len
(
5
)
def
test_exception_hierarchy
(
self
):
def
test_exception_hierarchy
(
self
):
with
ignore
(
LookupError
):
with
suppress
(
LookupError
):
'Hello'
[
50
]
'Hello'
[
50
]
class
TestRedirectStdout
(
unittest
.
TestCase
):
def
test_redirect_to_string_io
(
self
):
f
=
io
.
StringIO
()
with
redirect_stdout
(
f
):
help
(
pow
)
s
=
f
.
getvalue
()
self
.
assertIn
(
'pow'
,
s
)
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
unittest
.
main
()
unittest
.
main
()
Misc/ACKS
View file @
d77ac11b
...
@@ -1003,6 +1003,7 @@ Anand B. Pillai
...
@@ -1003,6 +1003,7 @@ Anand B. Pillai
François Pinard
François Pinard
Tom Pinckney
Tom Pinckney
Zach Pincus
Zach Pincus
Zero Piraeus
Michael Piotrowski
Michael Piotrowski
Antoine Pitrou
Antoine Pitrou
Jean-François Piéronne
Jean-François Piéronne
...
...
Misc/NEWS
View file @
d77ac11b
...
@@ -42,6 +42,11 @@ Core and Builtins
...
@@ -42,6 +42,11 @@ Core and Builtins
Library
Library
-------
-------
- Issue #19266: Rename the new-in-3.4 ``contextlib.ignore`` context manager
to ``contextlib.suppress`` in order to be more consistent with existing
descriptions of that operation elsewhere in the language and standard
library documentation (Patch by Zero Piraeus)
- Issue #18891: Completed the new email package (provisional) API additions
- Issue #18891: Completed the new email package (provisional) API additions
by adding new classes EmailMessage, MIMEPart, and ContentManager.
by adding new classes EmailMessage, MIMEPart, and ContentManager.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment