<div dir="ltr"><div class="gmail_quote"><div dir="ltr">On Tue, Aug 28, 2018 at 11:55 PM Richard W.M. Jones <<a href="mailto:rjones@redhat.com">rjones@redhat.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On Tue, Aug 28, 2018 at 11:50:56PM +0300, Nir Soffer wrote:<br>
> With python 3, we have a nicer way to handle socket.error with errno set<br>
> to EPIPE (or ESHUTDOWN).<br>
> <br>
> This is also more correct since in some cases (that I could not<br>
> reproduce yet with v2v), using e[0] with BrokenPipeError will fail with:<br>
> <br>
> >>> OSError(errno.EPIPE, "Broken pipe")[0]<br>
> Traceback (most recent call last):<br>
>   File "<stdin>", line 1, in <module><br>
> TypeError: 'BrokenPipeError' object is not subscriptable<br>
> <br>
> For python 2 e[0] seems to work, but is leftover from historic python<br>
> version that used to raise a tuple instead of socket.error instance.<br>
> In python 2.7 library code e.args[0] is used. If we ever port this to<br>
> python 2 this is the best form.<br>
<br>
Unfortunately we do have to port this to Python 2, at least while RHEL 7<br>
is a thing we have to deal with:<br>
<br>
<a href="https://github.com/libguestfs/libguestfs/commit/866d303bce0fee88b118768d1b7b36c87da6200b" rel="noreferrer" target="_blank">https://github.com/libguestfs/libguestfs/commit/866d303bce0fee88b118768d1b7b36c87da6200b</a><br>
<br>
Do you know what exactly will work with Python 2?<br></blockquote><div><br></div><div>Yes - this should be equivalent of "except BrokenPipeError":</div><div><br></div><div>    expect socket.error as e:</div><div>        if e.args[0] not in (errno.EPIPE, errno.ESHUTDOWN):</div><div>            raise</div><div><br></div><div>See this for reference:</div><div><a href="https://github.com/python/cpython/blob/491740f116755e220135e596ec802ea3a0f65596/Lib/asyncore.py#L115">https://github.com/python/cpython/blob/491740f116755e220135e596ec802ea3a0f65596/Lib/asyncore.py#L115</a></div><div><br></div><div><div>This also works for hybrid code based that works on both python 2 and 3, see:</div><div><a href="https://gerrit.ovirt.org/c/93278/">https://gerrit.ovirt.org/c/93278/</a><br></div><br class="inbox-inbox-Apple-interchange-newline"></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
As for upstream / Python 3, the patch looks fine so I'll push it shortly.<br>
<br>
Rich.<br>
<br>
>  v2v/rhv-upload-plugin.py | 11 ++++-------<br>
>  1 file changed, 4 insertions(+), 7 deletions(-)<br>
> <br>
> diff --git a/v2v/rhv-upload-plugin.py b/v2v/rhv-upload-plugin.py<br>
> index b5dd5521d..5cd6d5cab 100644<br>
> --- a/v2v/rhv-upload-plugin.py<br>
> +++ b/v2v/rhv-upload-plugin.py<br>
> @@ -17,7 +17,6 @@<br>
>  # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.<br>
>  <br>
>  import builtins<br>
> -import errno<br>
>  import json<br>
>  import logging<br>
>  import socket<br>
> @@ -361,9 +360,8 @@ def pwrite(h, buf, offset):<br>
>  <br>
>      try:<br>
>          http.send(buf)<br>
> -    except socket.error as e:<br>
> -        if e[0] != errno.EPIPE:<br>
> -            raise<br>
> +    except BrokenPipeError:<br>
> +        pass<br>
>  <br>
>      r = http.getresponse()<br>
>      if r.status != 200:<br>
> @@ -425,9 +423,8 @@ def emulate_zero(h, count, offset):<br>
>                  http.send(buf)<br>
>                  count -= len(buf)<br>
>              http.send(buffer(buf, 0, count))<br>
> -        except socket.error as e:<br>
> -            if e[0] != errno.EPIPE:<br>
> -                raise<br>
> +        except BrokenPipeError:<br>
> +            pass<br>
>  <br>
>          r = http.getresponse()<br>
>          if r.status != 200:<br>
> -- <br>
> 2.17.1<br>
<br>
-- <br>
Richard Jones, Virtualization Group, Red Hat <a href="http://people.redhat.com/~rjones" rel="noreferrer" target="_blank">http://people.redhat.com/~rjones</a><br>
Read my programming and virtualization blog: <a href="http://rwmj.wordpress.com" rel="noreferrer" target="_blank">http://rwmj.wordpress.com</a><br>
libguestfs lets you edit virtual machines.  Supports shell scripting,<br>
bindings from many languages.  <a href="http://libguestfs.org" rel="noreferrer" target="_blank">http://libguestfs.org</a><br>
</blockquote></div></div>