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 # http://www.thekelleys.org.uk/dnsmasq/docs/dnsmasq-man.html
try:
import errno
except ImportError:
import os.errno as errno
import fcntl import fcntl
import os import os
import time import time
@ -206,7 +210,7 @@ def _exclusive_write_or_pass(path, buf):
f.flush() f.flush()
return True return True
except IOError as e: except IOError as e:
if e.errno == os.errno.EWOULDBLOCK: if e.errno == errno.EWOULDBLOCK:
LOG.debug('%s locked; will try again (later)', path) LOG.debug('%s locked; will try again (later)', path)
attempts -= 1 attempts -= 1
time.sleep(_EXCLUSIVE_WRITE_ATTEMPTS_DELAY) time.sleep(_EXCLUSIVE_WRITE_ATTEMPTS_DELAY)
@ -261,7 +265,7 @@ def _configure_unknown_hosts():
if os.stat(path).st_size == len(wildcard_filter): if os.stat(path).st_size == len(wildcard_filter):
return return
except OSError as e: except OSError as e:
if e.errno != os.errno.ENOENT: if e.errno != errno.ENOENT:
raise raise
if _exclusive_write_or_pass(path, '%s' % wildcard_filter): 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 # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
try:
import errno
except ImportError:
import os.errno as errno
import datetime import datetime
import os import os
@ -131,7 +135,7 @@ class TestExclusiveWriteOrPass(test_base.BaseTest):
def test_write_would_block(self): def test_write_would_block(self):
err = IOError('Oops!') err = IOError('Oops!')
err.errno = os.errno.EWOULDBLOCK err.errno = errno.EWOULDBLOCK
# lock/unlock paired calls # lock/unlock paired calls
self.mock_fcntl.side_effect = [ self.mock_fcntl.side_effect = [
# first try # first try
@ -156,7 +160,7 @@ class TestExclusiveWriteOrPass(test_base.BaseTest):
'ironic_inspector.pxe_filter.dnsmasq._EXCLUSIVE_WRITE_ATTEMPTS', 'ironic_inspector.pxe_filter.dnsmasq._EXCLUSIVE_WRITE_ATTEMPTS',
1)) 1))
err = IOError('Oops!') err = IOError('Oops!')
err.errno = os.errno.EWOULDBLOCK err.errno = errno.EWOULDBLOCK
self.mock_fcntl.side_effect = [err, None] self.mock_fcntl.side_effect = [err, None]
wrote = dnsmasq._exclusive_write_or_pass(self.path, self.buf) 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): def test_write_custom_ioerror(self):
err = IOError('Oops!') err = IOError('Oops!')
err.errno = os.errno.EBADF err.errno = errno.EBADF
self.mock_fcntl.side_effect = [err, None] self.mock_fcntl.side_effect = [err, None]
self.assertRaisesRegex( self.assertRaisesRegex(