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
This commit is contained in:
Thomas Goirand 2018-08-30 10:20:02 +02:00
parent 56138d8f06
commit 2459cd8531
2 changed files with 13 additions and 5 deletions

View File

@ -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):

View File

@ -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(