[Libguestfs] [PATCH] v2v: rhv-upload-plugin: Use BrokenPipeError

Nir Soffer nirsof at gmail.com
Tue Aug 28 20:50:56 UTC 2018


With python 3, we have a nicer way to handle socket.error with errno set
to EPIPE (or ESHUTDOWN).

This is also more correct since in some cases (that I could not
reproduce yet with v2v), using e[0] with BrokenPipeError will fail with:

>>> OSError(errno.EPIPE, "Broken pipe")[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'BrokenPipeError' object is not subscriptable

For python 2 e[0] seems to work, but is leftover from historic python
version that used to raise a tuple instead of socket.error instance.
In python 2.7 library code e.args[0] is used. If we ever port this to
python 2 this is the best form.
---
 v2v/rhv-upload-plugin.py | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)

diff --git a/v2v/rhv-upload-plugin.py b/v2v/rhv-upload-plugin.py
index b5dd5521d..5cd6d5cab 100644
--- a/v2v/rhv-upload-plugin.py
+++ b/v2v/rhv-upload-plugin.py
@@ -17,7 +17,6 @@
 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
 import builtins
-import errno
 import json
 import logging
 import socket
@@ -361,9 +360,8 @@ def pwrite(h, buf, offset):
 
     try:
         http.send(buf)
-    except socket.error as e:
-        if e[0] != errno.EPIPE:
-            raise
+    except BrokenPipeError:
+        pass
 
     r = http.getresponse()
     if r.status != 200:
@@ -425,9 +423,8 @@ def emulate_zero(h, count, offset):
                 http.send(buf)
                 count -= len(buf)
             http.send(buffer(buf, 0, count))
-        except socket.error as e:
-            if e[0] != errno.EPIPE:
-                raise
+        except BrokenPipeError:
+            pass
 
         r = http.getresponse()
         if r.status != 200:
-- 
2.17.1




More information about the Libguestfs mailing list