From 9b6d082673770710be661ba494c7c4b67d88820e Mon Sep 17 00:00:00 2001 From: Rusty Bird Date: Wed, 10 Feb 2021 12:58:01 +0000 Subject: [PATCH] app: use suppress() in simple cases --- qubes/app.py | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/qubes/app.py b/qubes/app.py index 5f0cd97c..f6cc3a92 100644 --- a/qubes/app.py +++ b/qubes/app.py @@ -33,6 +33,7 @@ import tempfile import time import traceback import uuid +from contextlib import suppress import asyncio import jinja2 @@ -280,11 +281,9 @@ class QubesHost: self.app.log.debug('QubesHost: no_cpus={} memory_total={}'.format( self.no_cpus, self.memory_total)) - try: + with suppress(NotImplementedError): self.app.log.debug('QubesHost: xen_free_memory={}'.format( self.get_free_xen_memory())) - except NotImplementedError: - pass @property def memory_total(self): @@ -1324,10 +1323,8 @@ class Qubes(qubes.PropertyHolder): """ # first search for index, verbatim - try: + with suppress(KeyError): return self.labels[label] - except KeyError: - pass # then search for name for i in self.labels.values(): @@ -1335,10 +1332,8 @@ class Qubes(qubes.PropertyHolder): return i # last call, if label is a number represented as str, search in indices - try: + with suppress(KeyError, ValueError): return self.labels[int(label)] - except (KeyError, ValueError): - pass raise qubes.exc.QubesLabelNotFoundError(label) @@ -1477,7 +1472,7 @@ class Qubes(qubes.PropertyHolder): # allow removed VM to reference itself continue for prop in obj.property_list(): - try: + with suppress(AttributeError): if isinstance(prop, qubes.vm.VMProperty) and \ getattr(obj, prop.__name__) == vm: self.log.error( @@ -1489,8 +1484,6 @@ class Qubes(qubes.PropertyHolder): 'see /var/log/qubes/qubes.log in dom0 for ' 'details'.format( vm.name)) - except AttributeError: - pass assignments = vm.get_provided_assignments() if assignments: @@ -1512,11 +1505,9 @@ class Qubes(qubes.PropertyHolder): 'updatevm', 'default_template', ): - try: + with suppress(AttributeError): if getattr(self, propname) == vm: delattr(self, propname) - except AttributeError: - pass @qubes.events.handler('property-pre-set:clockvm') def on_property_pre_set_clockvm(self, event, name, newvalue, oldvalue=None):