util/ctx: extract suppress_oserror()
Extract the `suppress_oserror()` function from the ObjectManager and make it available as utility for other code as well. This also adds a bunch of tests that verify it works as expected.
This commit is contained in:
parent
6e02488a9f
commit
8a195d7502
3 changed files with 80 additions and 15 deletions
35
osbuild/util/ctx.py
Normal file
35
osbuild/util/ctx.py
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
"""ContextManager Utilities
|
||||
|
||||
This module implements helpers around python context-managers, with-statements,
|
||||
and RAII. It is meant as a supplement to `contextlib` from the python standard
|
||||
library.
|
||||
"""
|
||||
|
||||
import contextlib
|
||||
|
||||
|
||||
__all__ = [
|
||||
"suppress_oserror",
|
||||
]
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def suppress_oserror(*errnos):
|
||||
"""Suppress OSError Exceptions
|
||||
|
||||
This is an extension to `contextlib.suppress()` from the python standard
|
||||
library. It catches any `OSError` exceptions and suppresses them. However,
|
||||
it only catches the exceptions that match the specified error numbers.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
errnos
|
||||
A list of error numbers to match on. If none are specified, this
|
||||
function has no effect.
|
||||
"""
|
||||
|
||||
try:
|
||||
yield
|
||||
except OSError as e:
|
||||
if e.errno not in errnos:
|
||||
raise e
|
||||
Loading…
Add table
Add a link
Reference in a new issue