Petr Horáček has uploaded a new change for review. Change subject: utils: suppress ......................................................................
utils: suppress Since suppress() function is available only in Python 3, add our own implementation to vdsm/utils.py. Change-Id: I76d5b0658435e59701ac4d845827e86026085c07 Signed-off-by: Petr Horáček <[email protected]> --- M lib/vdsm/utils.py M tests/utilsTests.py M vdsm_hooks/ovs/ovs_before_network_setup.py M vdsm_hooks/ovs/ovs_before_network_setup_ip.py M vdsm_hooks/ovs/ovs_before_network_setup_libvirt.py M vdsm_hooks/ovs/ovs_before_network_setup_ovs.py M vdsm_hooks/ovs/ovs_utils.py 7 files changed, 27 insertions(+), 18 deletions(-) git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/65/55065/1 diff --git a/lib/vdsm/utils.py b/lib/vdsm/utils.py index 13a8bc1..d8eb609 100644 --- a/lib/vdsm/utils.py +++ b/lib/vdsm/utils.py @@ -916,3 +916,14 @@ return func(inst, *args, **kwargs) return wrapper + + +@contextmanager +def suppress(exception=Exception): + """ Python 3 suppress context manager. + https://docs.python.org/3/library/contextlib.html#contextlib.suppress + """ + try: + yield + except exception: + pass diff --git a/tests/utilsTests.py b/tests/utilsTests.py index c8b4d17..f1edc74 100644 --- a/tests/utilsTests.py +++ b/tests/utilsTests.py @@ -341,6 +341,15 @@ def test_unique(self, iterable, unique_items): self.assertEquals(utils.unique(iterable,), unique_items) + def test_suppress(self): + with self.assertNotRaises(): + with utils.suppress(Exception): + raise Exception() + + with self.assertRaises(AttributeError): + with utils.suppress(OSError): + raise AttributeError() + class AsyncProcessOperationTests(TestCaseBase): def _echo(self, text): diff --git a/vdsm_hooks/ovs/ovs_before_network_setup.py b/vdsm_hooks/ovs/ovs_before_network_setup.py index 7cfbb22..fcb4d91 100755 --- a/vdsm_hooks/ovs/ovs_before_network_setup.py +++ b/vdsm_hooks/ovs/ovs_before_network_setup.py @@ -36,11 +36,12 @@ from vdsm.ipwrapper import linkSet from vdsm.netconfpersistence import RunningConfig from vdsm.network.configurators import libvirt +from vdsm.utils import suppress from hooking import execCmd import hooking -from ovs_utils import (is_ovs_network, is_ovs_bond, iter_ovs_nets, suppress, +from ovs_utils import (is_ovs_network, is_ovs_bond, iter_ovs_nets, destroy_ovs_bridge, ovs_bridge_exists, EXT_IP, EXT_OVS_VSCTL, BRIDGE_NAME, INIT_CONFIG_FILE) from ovs_setup_ovs import configure_ovs, prepare_ovs diff --git a/vdsm_hooks/ovs/ovs_before_network_setup_ip.py b/vdsm_hooks/ovs/ovs_before_network_setup_ip.py index acfc04d..a4a965f 100644 --- a/vdsm_hooks/ovs/ovs_before_network_setup_ip.py +++ b/vdsm_hooks/ovs/ovs_before_network_setup_ip.py @@ -27,8 +27,9 @@ from vdsm.network.configurators.iproute2 import Iproute2 from vdsm.network.models import NetDevice, IPv4, IPv6 from vdsm.network.sourceroute import DynamicSourceRoute +from vdsm.utils import suppress -from ovs_utils import suppress, BRIDGE_NAME +from ovs_utils import BRIDGE_NAME import ovs_utils log = partial(ovs_utils.log, tag='ovs_before_network_setup_ip: ') diff --git a/vdsm_hooks/ovs/ovs_before_network_setup_libvirt.py b/vdsm_hooks/ovs/ovs_before_network_setup_libvirt.py index 99fe0cd..425abdd 100644 --- a/vdsm_hooks/ovs/ovs_before_network_setup_libvirt.py +++ b/vdsm_hooks/ovs/ovs_before_network_setup_libvirt.py @@ -21,8 +21,7 @@ import six from vdsm.network.configurators import libvirt - -from ovs_utils import suppress +from vdsm.utils import suppress def prepare_libvirt(nets, running_config): diff --git a/vdsm_hooks/ovs/ovs_before_network_setup_ovs.py b/vdsm_hooks/ovs/ovs_before_network_setup_ovs.py index 3bf1808..8b995f3 100644 --- a/vdsm_hooks/ovs/ovs_before_network_setup_ovs.py +++ b/vdsm_hooks/ovs/ovs_before_network_setup_ovs.py @@ -23,11 +23,12 @@ from vdsm.netinfo.cache import CachingNetInfo from vdsm.network.configurators import libvirt +from vdsm.utils import suppress import hooking from ovs_utils import (get_bond_options, is_ovs_bond, iter_ovs_nets, - iter_ovs_bonds, rget, suppress, destroy_ovs_bridge, + iter_ovs_bonds, rget, destroy_ovs_bridge, BRIDGE_NAME, EXT_OVS_VSCTL) import ovs_utils diff --git a/vdsm_hooks/ovs/ovs_utils.py b/vdsm_hooks/ovs/ovs_utils.py index ed2ce01..c703689 100644 --- a/vdsm_hooks/ovs/ovs_utils.py +++ b/vdsm_hooks/ovs/ovs_utils.py @@ -17,8 +17,6 @@ # # Refer to the README and COPYING files for full details of the license # -from contextlib import contextmanager - import six from hooking import execCmd @@ -99,17 +97,6 @@ for bond, attrs in six.iteritems(bondings): if is_ovs_bond(attrs): yield bond, attrs - - -@contextmanager -def suppress(exception=Exception): - """ Python 3 suppress context manager. - https://docs.python.org/3/library/contextlib.html#contextlib.suppress - """ - try: - yield - except exception: - pass def destroy_ovs_bridge(): -- To view, visit https://gerrit.ovirt.org/55065 To unsubscribe, visit https://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I76d5b0658435e59701ac4d845827e86026085c07 Gerrit-PatchSet: 1 Gerrit-Project: vdsm Gerrit-Branch: master Gerrit-Owner: Petr Horáček <[email protected]> _______________________________________________ vdsm-patches mailing list [email protected] https://lists.fedorahosted.org/mailman/listinfo/vdsm-patches
