From 2459cd853115f4ae2ea1e79f46cb98f76278491f Mon Sep 17 00:00:00 2001 From: Thomas Goirand Date: Thu, 30 Aug 2018 10:20:02 +0200 Subject: [PATCH] Python 3.7: fix os.errno -> errno In Python 3.7, there's no longer a os.errno, it's now available directly from the root instead. This patch therefore, tries to "import errno", and fallsback to "import os.errno as errno" if it fails. Then in the content of the code, we simply use the errno module directly. Change-Id: Ibf385ab32a8098e936c019303633de38a848076c --- ironic_inspector/pxe_filter/dnsmasq.py | 8 ++++++-- ironic_inspector/test/unit/test_dnsmasq_pxe_filter.py | 10 +++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/ironic_inspector/pxe_filter/dnsmasq.py b/ironic_inspector/pxe_filter/dnsmasq.py index 5c7c56abd..d96d88706 100644 --- a/ironic_inspector/pxe_filter/dnsmasq.py +++ b/ironic_inspector/pxe_filter/dnsmasq.py @@ -20,6 +20,10 @@ # http://www.thekelleys.org.uk/dnsmasq/docs/dnsmasq-man.html +try: + import errno +except ImportError: + import os.errno as errno import fcntl import os import time @@ -206,7 +210,7 @@ def _exclusive_write_or_pass(path, buf): f.flush() return True except IOError as e: - if e.errno == os.errno.EWOULDBLOCK: + if e.errno == errno.EWOULDBLOCK: LOG.debug('%s locked; will try again (later)', path) attempts -= 1 time.sleep(_EXCLUSIVE_WRITE_ATTEMPTS_DELAY) @@ -261,7 +265,7 @@ def _configure_unknown_hosts(): if os.stat(path).st_size == len(wildcard_filter): return except OSError as e: - if e.errno != os.errno.ENOENT: + if e.errno != errno.ENOENT: raise if _exclusive_write_or_pass(path, '%s' % wildcard_filter): diff --git a/ironic_inspector/test/unit/test_dnsmasq_pxe_filter.py b/ironic_inspector/test/unit/test_dnsmasq_pxe_filter.py index fdfbda8a0..2b9511b0e 100644 --- a/ironic_inspector/test/unit/test_dnsmasq_pxe_filter.py +++ b/ironic_inspector/test/unit/test_dnsmasq_pxe_filter.py @@ -11,6 +11,10 @@ # See the License for the specific language governing permissions and # limitations under the License. +try: + import errno +except ImportError: + import os.errno as errno import datetime import os @@ -131,7 +135,7 @@ class TestExclusiveWriteOrPass(test_base.BaseTest): def test_write_would_block(self): err = IOError('Oops!') - err.errno = os.errno.EWOULDBLOCK + err.errno = errno.EWOULDBLOCK # lock/unlock paired calls self.mock_fcntl.side_effect = [ # first try @@ -156,7 +160,7 @@ class TestExclusiveWriteOrPass(test_base.BaseTest): 'ironic_inspector.pxe_filter.dnsmasq._EXCLUSIVE_WRITE_ATTEMPTS', 1)) err = IOError('Oops!') - err.errno = os.errno.EWOULDBLOCK + err.errno = errno.EWOULDBLOCK self.mock_fcntl.side_effect = [err, None] wrote = dnsmasq._exclusive_write_or_pass(self.path, self.buf) @@ -180,7 +184,7 @@ class TestExclusiveWriteOrPass(test_base.BaseTest): def test_write_custom_ioerror(self): err = IOError('Oops!') - err.errno = os.errno.EBADF + err.errno = errno.EBADF self.mock_fcntl.side_effect = [err, None] self.assertRaisesRegex(