[issue15806] Add context manager for the "try: ... except: pass" pattern
Raymond Hettinger
report at bugs.python.org
Wed Aug 29 07:35:27 CEST 2012
Raymond Hettinger added the comment:
Hmm, the __exit__ method was doing exact matches by exception type, so KeyError wouldn't match LookupError or Exception.
There are probably a number of ways to fix this, but it may be easiest to use the builtin exception catching mechanisms:
class Ignore:
''' Context manager to ignore particular exceptions'''
def __init__(self, *ignored_exceptions):
self.ignored_exceptions = ignored_exceptions
def __enter__(self):
return self
def __exit__(self, exctype, excinst, exctb):
if exctype is not None:
try:
raise
except self.ignored_exceptions:
return True
----------
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue15806>
_______________________________________
More information about the Python-bugs-list
mailing list