app: use suppress() in simple cases

This commit is contained in:
Rusty Bird 2021-02-10 12:58:01 +00:00
parent 7159f206a5
commit 9b6d082673
No known key found for this signature in database
GPG Key ID: 469D78F47AAF2ADF

View File

@ -33,6 +33,7 @@ import tempfile
import time import time
import traceback import traceback
import uuid import uuid
from contextlib import suppress
import asyncio import asyncio
import jinja2 import jinja2
@ -280,11 +281,9 @@ class QubesHost:
self.app.log.debug('QubesHost: no_cpus={} memory_total={}'.format( self.app.log.debug('QubesHost: no_cpus={} memory_total={}'.format(
self.no_cpus, self.memory_total)) self.no_cpus, self.memory_total))
try: with suppress(NotImplementedError):
self.app.log.debug('QubesHost: xen_free_memory={}'.format( self.app.log.debug('QubesHost: xen_free_memory={}'.format(
self.get_free_xen_memory())) self.get_free_xen_memory()))
except NotImplementedError:
pass
@property @property
def memory_total(self): def memory_total(self):
@ -1324,10 +1323,8 @@ class Qubes(qubes.PropertyHolder):
""" """
# first search for index, verbatim # first search for index, verbatim
try: with suppress(KeyError):
return self.labels[label] return self.labels[label]
except KeyError:
pass
# then search for name # then search for name
for i in self.labels.values(): for i in self.labels.values():
@ -1335,10 +1332,8 @@ class Qubes(qubes.PropertyHolder):
return i return i
# last call, if label is a number represented as str, search in indices # last call, if label is a number represented as str, search in indices
try: with suppress(KeyError, ValueError):
return self.labels[int(label)] return self.labels[int(label)]
except (KeyError, ValueError):
pass
raise qubes.exc.QubesLabelNotFoundError(label) raise qubes.exc.QubesLabelNotFoundError(label)
@ -1477,7 +1472,7 @@ class Qubes(qubes.PropertyHolder):
# allow removed VM to reference itself # allow removed VM to reference itself
continue continue
for prop in obj.property_list(): for prop in obj.property_list():
try: with suppress(AttributeError):
if isinstance(prop, qubes.vm.VMProperty) and \ if isinstance(prop, qubes.vm.VMProperty) and \
getattr(obj, prop.__name__) == vm: getattr(obj, prop.__name__) == vm:
self.log.error( self.log.error(
@ -1489,8 +1484,6 @@ class Qubes(qubes.PropertyHolder):
'see /var/log/qubes/qubes.log in dom0 for ' 'see /var/log/qubes/qubes.log in dom0 for '
'details'.format( 'details'.format(
vm.name)) vm.name))
except AttributeError:
pass
assignments = vm.get_provided_assignments() assignments = vm.get_provided_assignments()
if assignments: if assignments:
@ -1512,11 +1505,9 @@ class Qubes(qubes.PropertyHolder):
'updatevm', 'updatevm',
'default_template', 'default_template',
): ):
try: with suppress(AttributeError):
if getattr(self, propname) == vm: if getattr(self, propname) == vm:
delattr(self, propname) delattr(self, propname)
except AttributeError:
pass
@qubes.events.handler('property-pre-set:clockvm') @qubes.events.handler('property-pre-set:clockvm')
def on_property_pre_set_clockvm(self, event, name, newvalue, oldvalue=None): def on_property_pre_set_clockvm(self, event, name, newvalue, oldvalue=None):