From adfec525327956e2fa2120f1592be3cde9cec9e7 Mon Sep 17 00:00:00 2001 From: Vincent Bernat Date: Sat, 17 Jul 2021 15:00:54 +0200 Subject: [PATCH] i3-companion: use contextlib to simplify a bit some code --- bin/i3-companion | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/bin/i3-companion b/bin/i3-companion index 06caba7..d774428 100755 --- a/bin/i3-companion +++ b/bin/i3-companion @@ -5,6 +5,7 @@ import argparse import asyncio import collections +import contextlib import errno import functools import glob @@ -12,7 +13,6 @@ import html import logging import logging.handlers import os -import re import shlex import subprocess import sys @@ -177,12 +177,10 @@ def debounce(sleep, *, unless=None): def decorator(fn): async def worker(): while True: - try: + with contextlib.suppress(asyncio.TimeoutError): # Wait for an urgent work or until sleep is elapsed await asyncio.wait_for(workers[fn].urgent.wait(), timeout=sleep) logger.debug("urgent work received for %s", fn) - except asyncio.TimeoutError: - pass args, kwargs = workers[fn].queue workers[fn].queue = None workers[fn].urgent.clear() @@ -533,8 +531,7 @@ async def output_update(i3, event): """React to a XRandR change.""" # Grab current setup. Synchronous, but it's short enough - d = display.Display() - try: + with contextlib.closing(display.Display()) as d: screen = d.screen() window = screen.root.create_window(0, 0, 1, 1, 1, screen.root_depth) screen_resources = randr.get_screen_resources_current(window) @@ -557,8 +554,6 @@ async def output_update(i3, event): crtc_info.y, ) ) - finally: - d.close() # Compare to current setup if current_setup == output_update.last_setup: