diff -r 484ce82b7873 Doc/library/contextlib.rst --- a/Doc/library/contextlib.rst Tue Oct 15 12:05:57 2013 +0300 +++ b/Doc/library/contextlib.rst Tue Oct 15 10:13:45 2013 -0300 @@ -95,20 +95,21 @@ ``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 - occur in the body of a with-statement. + Return a context manager that suppresses any one of the specified exceptions + if it occurs in the body of a with-statement (and then immediately exits the + with-statement). - As with any other mechanism that completely suppresses exceptions, it - should only be used to cover very specific errors where silently - ignoring the exception is known to be the right thing to do. + As with any other mechanism that completely suppresses exceptions, it should + only be used to cover very specific errors where silently continuing with + program execution is known to be the right thing to do. For example:: - from contextlib import ignore + from contextlib import suppress - with ignore(FileNotFoundError): + with suppress(FileNotFoundError): os.remove('somefile.tmp') This code is equivalent to:: diff -r 484ce82b7873 Doc/whatsnew/3.4.rst --- a/Doc/whatsnew/3.4.rst Tue Oct 15 12:05:57 2013 +0300 +++ b/Doc/whatsnew/3.4.rst Tue Oct 15 10:13:45 2013 -0300 @@ -221,8 +221,8 @@ contextlib ---------- -The new :class:`contextlib.ignore` context manager helps to clarify the -intent of code that deliberately ignores failures from a particular +The new :class:`contextlib.suppress` context manager helps to clarify the +intent of code that deliberately suppresses exceptions from a particular operation. The new :class:`contextlib.redirect_stdio` context manager makes it easier diff -r 484ce82b7873 Lib/contextlib.py --- a/Lib/contextlib.py Tue Oct 15 12:05:57 2013 +0300 +++ b/Lib/contextlib.py Tue Oct 15 10:13:45 2013 -0300 @@ -5,7 +5,7 @@ from functools import wraps __all__ = ["contextmanager", "closing", "ContextDecorator", "ExitStack", - "ignore", "redirect_stdout"] + "redirect_stdout", "suppress"] class ContextDecorator(object): @@ -179,10 +179,10 @@ sys.stdout = self.old_target @contextmanager -def ignore(*exceptions): - """Context manager to ignore specified exceptions +def suppress(*exceptions): + """Context manager to suppress specified exceptions - with ignore(OSError): + with suppress(OSError): os.remove(somefile) """ diff -r 484ce82b7873 Lib/test/test_contextlib.py --- a/Lib/test/test_contextlib.py Tue Oct 15 12:05:57 2013 +0300 +++ b/Lib/test/test_contextlib.py Tue Oct 15 10:13:45 2013 -0300 @@ -632,28 +632,6 @@ stack.push(cm) self.assertIs(stack._exit_callbacks[-1], cm) -class TestIgnore(unittest.TestCase): - - def test_no_exception(self): - - with ignore(ValueError): - self.assertEqual(pow(2, 5), 32) - - def test_exact_exception(self): - - with ignore(TypeError): - len(5) - - def test_multiple_exception_args(self): - - with ignore(ZeroDivisionError, TypeError): - len(5) - - def test_exception_hierarchy(self): - - with ignore(LookupError): - 'Hello'[50] - class TestRedirectStdout(unittest.TestCase): def test_redirect_to_string_io(self): @@ -663,5 +641,27 @@ s = f.getvalue() self.assertIn('pow', s) +class TestSuppress(unittest.TestCase): + + def test_no_exception(self): + + with suppress(ValueError): + self.assertEqual(pow(2, 5), 32) + + def test_exact_exception(self): + + with suppress(TypeError): + len(5) + + def test_multiple_exception_args(self): + + with suppress(ZeroDivisionError, TypeError): + len(5) + + def test_exception_hierarchy(self): + + with suppress(LookupError): + 'Hello'[50] + if __name__ == "__main__": unittest.main()