New submission from Michael Büsch: This adds a whitelist parameter with the name 'unless' to contextlib.suppress, so one can specify exceptions that will not be suppressed.
This is useful for specifying single sub-exceptions that we still want to catch, even of we want to suppress all other siblings. Or it can be used to suppress all exceptions except some very specific ones by suppressing BaseException and whitelisting things like SyntaxError, NameError and the like. Usage looks like this: with suppress(OSError, unless = PermissionError): with open("foobar", "wb") as f: f.write(... I selected the name 'unless' instead of 'whitelist' or such, because I think that pronounces nicely in the 'with' line context. However please feel free to make better suggestions. ---------- components: Library (Lib) files: contextlib-suppress-whitelist.patch keywords: patch messages: 273206 nosy: mb_, ncoghlan, yselivanov priority: normal severity: normal status: open title: contextlib.suppress: Add whitelist argument type: enhancement Added file: http://bugs.python.org/file44168/contextlib-suppress-whitelist.patch _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue27814> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com