[libvirt] [RFC PATCH v2 7/8] python3: Open files in text instead of binary mode

Andrea Bolognani posted 8 patches 7 years, 2 months ago
[libvirt] [RFC PATCH v2 7/8] python3: Open files in text instead of binary mode
Posted by Andrea Bolognani 7 years, 2 months ago
We use text operations on the contents after reading them.

Signed-off-by: Andrea Bolognani <abologna@redhat.com>
---
This seems reasonable and works correctly AFAICT, but I'm
not 100% confident it's the right way to address the issue.

 src/esx/esx_vi_generator.py        | 4 ++--
 src/hyperv/hyperv_wmi_generator.py | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/src/esx/esx_vi_generator.py b/src/esx/esx_vi_generator.py
index 2f3c88812d..6ce017d794 100755
--- a/src/esx/esx_vi_generator.py
+++ b/src/esx/esx_vi_generator.py
@@ -1327,7 +1327,7 @@ def open_and_print(filename):
     else:
         print("  GEN      " + filename)
 
-    return open(filename, "wb")
+    return open(filename, "wt")
 
 
 
@@ -1435,7 +1435,7 @@ block = None
 
 
 # parse input file
-for line in file(input_filename, "rb").readlines():
+for line in open(input_filename, "rt").readlines():
     number += 1
 
     if "#" in line:
diff --git a/src/hyperv/hyperv_wmi_generator.py b/src/hyperv/hyperv_wmi_generator.py
index 1cf16a7836..7dc11ba905 100755
--- a/src/hyperv/hyperv_wmi_generator.py
+++ b/src/hyperv/hyperv_wmi_generator.py
@@ -396,7 +396,7 @@ def open_and_print(filename):
     else:
         print("  GEN      " + filename)
 
-    return open(filename, "wb")
+    return open(filename, "wt")
 
 
 
@@ -468,7 +468,7 @@ def main():
     number = 0
     block = None
 
-    for line in file(input_filename, "rb").readlines():
+    for line in open(input_filename, "rt").readlines():
         number += 1
 
         if "#" in line:
-- 
2.14.3

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [RFC PATCH v2 7/8] python3: Open files in text instead of binary mode
Posted by Daniel P. Berrangé 7 years, 2 months ago
On Thu, Mar 15, 2018 at 05:11:09PM +0100, Andrea Bolognani wrote:
> We use text operations on the contents after reading them.
> 
> Signed-off-by: Andrea Bolognani <abologna@redhat.com>
> ---
> This seems reasonable and works correctly AFAICT, but I'm
> not 100% confident it's the right way to address the issue.

So in Python3 if you open in binary mode, it always gives
you back "bytes", where as in text mode it gives you back
"str". In python2 is always gives you back "str", which
is actually equivalent to python3's "bytes" type. What
fun.

None the less, I think this change is likely correct.

> 
>  src/esx/esx_vi_generator.py        | 4 ++--
>  src/hyperv/hyperv_wmi_generator.py | 4 ++--
>  2 files changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/src/esx/esx_vi_generator.py b/src/esx/esx_vi_generator.py
> index 2f3c88812d..6ce017d794 100755
> --- a/src/esx/esx_vi_generator.py
> +++ b/src/esx/esx_vi_generator.py
> @@ -1327,7 +1327,7 @@ def open_and_print(filename):
>      else:
>          print("  GEN      " + filename)
>  
> -    return open(filename, "wb")
> +    return open(filename, "wt")
>  
>  
>  
> @@ -1435,7 +1435,7 @@ block = None
>  
>  
>  # parse input file
> -for line in file(input_filename, "rb").readlines():
> +for line in open(input_filename, "rt").readlines():
>      number += 1
>  
>      if "#" in line:
> diff --git a/src/hyperv/hyperv_wmi_generator.py b/src/hyperv/hyperv_wmi_generator.py
> index 1cf16a7836..7dc11ba905 100755
> --- a/src/hyperv/hyperv_wmi_generator.py
> +++ b/src/hyperv/hyperv_wmi_generator.py
> @@ -396,7 +396,7 @@ def open_and_print(filename):
>      else:
>          print("  GEN      " + filename)
>  
> -    return open(filename, "wb")
> +    return open(filename, "wt")
>  
>  
>  
> @@ -468,7 +468,7 @@ def main():
>      number = 0
>      block = None
>  
> -    for line in file(input_filename, "rb").readlines():
> +    for line in open(input_filename, "rt").readlines():
>          number += 1
>  
>          if "#" in line:
> -- 
> 2.14.3
> 
> --
> libvir-list mailing list
> libvir-list@redhat.com
> https://www.redhat.com/mailman/listinfo/libvir-list

Regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [RFC PATCH v2 7/8] python3: Open files in text instead of binary mode
Posted by Daniel P. Berrangé 7 years, 2 months ago
On Thu, Mar 15, 2018 at 04:28:56PM +0000, Daniel P. Berrangé wrote:
> On Thu, Mar 15, 2018 at 05:11:09PM +0100, Andrea Bolognani wrote:
> > We use text operations on the contents after reading them.
> > 
> > Signed-off-by: Andrea Bolognani <abologna@redhat.com>
> > ---
> > This seems reasonable and works correctly AFAICT, but I'm
> > not 100% confident it's the right way to address the issue.
> 
> So in Python3 if you open in binary mode, it always gives
> you back "bytes", where as in text mode it gives you back
> "str". In python2 is always gives you back "str", which
> is actually equivalent to python3's "bytes" type. What
> fun.
> 
> None the less, I think this change is likely correct.

Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>


> 
> > 
> >  src/esx/esx_vi_generator.py        | 4 ++--
> >  src/hyperv/hyperv_wmi_generator.py | 4 ++--
> >  2 files changed, 4 insertions(+), 4 deletions(-)
> > 
> > diff --git a/src/esx/esx_vi_generator.py b/src/esx/esx_vi_generator.py
> > index 2f3c88812d..6ce017d794 100755
> > --- a/src/esx/esx_vi_generator.py
> > +++ b/src/esx/esx_vi_generator.py
> > @@ -1327,7 +1327,7 @@ def open_and_print(filename):
> >      else:
> >          print("  GEN      " + filename)
> >  
> > -    return open(filename, "wb")
> > +    return open(filename, "wt")
> >  
> >  
> >  
> > @@ -1435,7 +1435,7 @@ block = None
> >  
> >  
> >  # parse input file
> > -for line in file(input_filename, "rb").readlines():
> > +for line in open(input_filename, "rt").readlines():
> >      number += 1
> >  
> >      if "#" in line:
> > diff --git a/src/hyperv/hyperv_wmi_generator.py b/src/hyperv/hyperv_wmi_generator.py
> > index 1cf16a7836..7dc11ba905 100755
> > --- a/src/hyperv/hyperv_wmi_generator.py
> > +++ b/src/hyperv/hyperv_wmi_generator.py
> > @@ -396,7 +396,7 @@ def open_and_print(filename):
> >      else:
> >          print("  GEN      " + filename)
> >  
> > -    return open(filename, "wb")
> > +    return open(filename, "wt")
> >  
> >  
> >  
> > @@ -468,7 +468,7 @@ def main():
> >      number = 0
> >      block = None
> >  
> > -    for line in file(input_filename, "rb").readlines():
> > +    for line in open(input_filename, "rt").readlines():
> >          number += 1
> >  
> >          if "#" in line:
> > -- 
> > 2.14.3
> > 
> > --
> > libvir-list mailing list
> > libvir-list@redhat.com
> > https://www.redhat.com/mailman/listinfo/libvir-list
> 
> Regards,
> Daniel
> -- 
> |: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
> |: https://libvirt.org         -o-            https://fstop138.berrange.com :|
> |: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
> 
> --
> libvir-list mailing list
> libvir-list@redhat.com
> https://www.redhat.com/mailman/listinfo/libvir-list

Regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list