Ejemplo n.º 1
0
def test_log_and_reraise():
    """Test log and reraise context manager."""
    log_msg = None

    def fn(msg):
        nonlocal log_msg
        log_msg = msg

    with pytest.raises(ValueError):
        with exception_log_and_reraise(fn, "oops"):
            raise ValueError()

    assert log_msg == "oops"
Ejemplo n.º 2
0
def lock_file(file_descriptor: IO[bytes], logger: Logger = _default_logger):
    """Lock file in context manager.

    :param file_descriptor: file descriptio of file to lock.
    """
    with exception_log_and_reraise(
        logger.error, f"Couldn't acquire lock for file {file_descriptor.name}: {{}}",
    ):
        file_lock.lock(file_descriptor, file_lock.LOCK_EX)

    try:
        yield
    finally:
        file_lock.unlock(file_descriptor)