[virt-tools-list] [virt-bootstrap] [PATCH v3 01/12] Python 3/2 compatibility: Convert Byte-str to Str

Radostin Stoyanov rstoyanov1 at gmail.com
Thu Jul 20 11:29:36 UTC 2017


Encoded Unicode in Python 3 is represented as binary data. The
difference with Python2 is that any attempt to mix text and data in
Python 3.0 raises TypeError, whereas if you were to mix Unicode and
8-bit strings in Python 2.x, it would work if the 8-bit string happened
to contain only 7-bit (ASCII) bytes, but you would get
UnicodeDecodeError if it contained non-ASCII values.

Reference:
https://docs.python.org/release/3.0.1/whatsnew/3.0.html#text-vs-data-instead-of-unicode-vs-8-bit

Example:

```python

>>> b'foo.bar'.split('.')
['foo', 'bar']
```

```python

>>> b'foo.bar'.split('.')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: a bytes-like object is required, not 'str'

>>> b'foo.bar'.split(b'.')
[b'foo', b'bar']
```
---
 src/virtBootstrap/utils.py          | 15 ++++++++-------
 src/virtBootstrap/virt_bootstrap.py |  2 +-
 2 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/src/virtBootstrap/utils.py b/src/virtBootstrap/utils.py
index bd95d35..daabd26 100644
--- a/src/virtBootstrap/utils.py
+++ b/src/virtBootstrap/utils.py
@@ -80,9 +80,9 @@ def execute(cmd):
     output, err = proc.communicate()
 
     if output:
-        logger.debug("Stdout:\n%s", output)
+        logger.debug("Stdout:\n%s", output.decode('utf-8'))
     if err:
-        logger.debug("Stderr:\n%s", err)
+        logger.debug("Stderr:\n%s", err.decode('utf-8'))
 
     if proc.returncode != 0:
         raise CalledProcessError(proc.returncode, cmd_str)
@@ -163,8 +163,8 @@ def get_mime_type(path):
     """
         Get the mime type of a file.
     """
-    return Popen(["/usr/bin/file", "--mime-type", path],
-                 stdout=PIPE).communicate()[0].split()[1]
+    return (Popen(["/usr/bin/file", "--mime-type", path], stdout=PIPE)
+            .stdout.read().decode('utf-8').split()[1])
 
 
 def create_qcow2(tar_file, layer_file, backing_file=None, size=DEF_QCOW2_SIZE):
@@ -269,8 +269,9 @@ def get_image_details(src, raw=False,
     proc = Popen(cmd, stdout=PIPE, stderr=PIPE)
     output, error = proc.communicate()
     if error:
-        raise ValueError("Image could not be retrieved:", error)
-    return json.loads(output)
+        raise ValueError("Image could not be retrieved:",
+                         error.decode('utf-8'))
+    return json.loads(output.decode('utf-8'))
 
 
 def is_new_layer_message(line):
@@ -334,7 +335,7 @@ def write_progress(prog):
     # Get terminal width
     try:
         terminal_width = int(Popen(["stty", "size"], stdout=PIPE).stdout
-                             .read().split()[1])
+                             .read().decode('utf-8').split()[1])
     except Exception:
         terminal_width = 80
     # Prepare message
diff --git a/src/virtBootstrap/virt_bootstrap.py b/src/virtBootstrap/virt_bootstrap.py
index 85aca33..c66cc92 100755
--- a/src/virtBootstrap/virt_bootstrap.py
+++ b/src/virtBootstrap/virt_bootstrap.py
@@ -77,7 +77,7 @@ def set_root_password(rootfs, password):
     users = 'root:%s' % password
     args = ['chpasswd', '-R', rootfs]
     chpasswd = Popen(args, stdin=PIPE)
-    chpasswd.communicate(input=users)
+    chpasswd.communicate(input=users.encode('utf-8'))
     if chpasswd.returncode != 0:
         raise CalledProcessError(chpasswd.returncode, cmd=args, output=None)
 
-- 
2.9.4




More information about the virt-tools-list mailing list