Browse Source

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
Thomas Goirand 7 months ago
parent
commit
2459cd8531

+ 6
- 2
ironic_inspector/pxe_filter/dnsmasq.py View File

@@ -20,6 +20,10 @@
20 20
 #     http://www.thekelleys.org.uk/dnsmasq/docs/dnsmasq-man.html
21 21
 
22 22
 
23
+try:
24
+    import errno
25
+except ImportError:
26
+    import os.errno as errno
23 27
 import fcntl
24 28
 import os
25 29
 import time
@@ -206,7 +210,7 @@ def _exclusive_write_or_pass(path, buf):
206 210
                 f.flush()
207 211
                 return True
208 212
             except IOError as e:
209
-                if e.errno == os.errno.EWOULDBLOCK:
213
+                if e.errno == errno.EWOULDBLOCK:
210 214
                     LOG.debug('%s locked; will try again (later)', path)
211 215
                     attempts -= 1
212 216
                     time.sleep(_EXCLUSIVE_WRITE_ATTEMPTS_DELAY)
@@ -261,7 +265,7 @@ def _configure_unknown_hosts():
261 265
         if os.stat(path).st_size == len(wildcard_filter):
262 266
             return
263 267
     except OSError as e:
264
-        if e.errno != os.errno.ENOENT:
268
+        if e.errno != errno.ENOENT:
265 269
             raise
266 270
 
267 271
     if _exclusive_write_or_pass(path, '%s' % wildcard_filter):

+ 7
- 3
ironic_inspector/test/unit/test_dnsmasq_pxe_filter.py View File

@@ -11,6 +11,10 @@
11 11
 # See the License for the specific language governing permissions and
12 12
 # limitations under the License.
13 13
 
14
+try:
15
+    import errno
16
+except ImportError:
17
+    import os.errno as errno
14 18
 import datetime
15 19
 import os
16 20
 
@@ -131,7 +135,7 @@ class TestExclusiveWriteOrPass(test_base.BaseTest):
131 135
 
132 136
     def test_write_would_block(self):
133 137
         err = IOError('Oops!')
134
-        err.errno = os.errno.EWOULDBLOCK
138
+        err.errno = errno.EWOULDBLOCK
135 139
         # lock/unlock paired calls
136 140
         self.mock_fcntl.side_effect = [
137 141
             # first try
@@ -156,7 +160,7 @@ class TestExclusiveWriteOrPass(test_base.BaseTest):
156 160
             'ironic_inspector.pxe_filter.dnsmasq._EXCLUSIVE_WRITE_ATTEMPTS',
157 161
             1))
158 162
         err = IOError('Oops!')
159
-        err.errno = os.errno.EWOULDBLOCK
163
+        err.errno = errno.EWOULDBLOCK
160 164
         self.mock_fcntl.side_effect = [err, None]
161 165
 
162 166
         wrote = dnsmasq._exclusive_write_or_pass(self.path, self.buf)
@@ -180,7 +184,7 @@ class TestExclusiveWriteOrPass(test_base.BaseTest):
180 184
     def test_write_custom_ioerror(self):
181 185
 
182 186
         err = IOError('Oops!')
183
-        err.errno = os.errno.EBADF
187
+        err.errno = errno.EBADF
184 188
         self.mock_fcntl.side_effect = [err, None]
185 189
 
186 190
         self.assertRaisesRegex(

Loading…
Cancel
Save