diff mbox series

[2/5] tools/python: fix Python3.4 TypeError in format string

Message ID 20210705191027.15107-3-andrew.cooper3@citrix.com (mailing list archive)
State New
Headers show
Series tools/migration: Py3 fixes, and page type helpers | expand

Commit Message

Andrew Cooper July 5, 2021, 7:10 p.m. UTC
From: Olaf Hering <olaf@aepfle.de>

Using the first element of a tuple for a format specifier fails with
python3.4 as included in SLE12:
    b = b"string/%x" % (i, )
TypeError: unsupported operand type(s) for %: 'bytes' and 'tuple'

It happens to work with python 2.7 and 3.6.
To support older Py3, format as strings and explicitly encode as ASCII.

Signed-off-by: Olaf Hering <olaf@aepfle.de>
Reviewed-by: Andrew Cooper <andrew.cooper3@citrix.com>
---
 tools/python/scripts/convert-legacy-stream | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

Comments

Marek Marczykowski-Górecki July 5, 2021, 7:51 p.m. UTC | #1
On Mon, Jul 05, 2021 at 08:10:24PM +0100, Andrew Cooper wrote:
> From: Olaf Hering <olaf@aepfle.de>
> 
> Using the first element of a tuple for a format specifier fails with
> python3.4 as included in SLE12:
>     b = b"string/%x" % (i, )
> TypeError: unsupported operand type(s) for %: 'bytes' and 'tuple'
> 
> It happens to work with python 2.7 and 3.6.
> To support older Py3, format as strings and explicitly encode as ASCII.
> 
> Signed-off-by: Olaf Hering <olaf@aepfle.de>
> Reviewed-by: Andrew Cooper <andrew.cooper3@citrix.com>

Acked-by: Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>

> ---
>  tools/python/scripts/convert-legacy-stream | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/tools/python/scripts/convert-legacy-stream b/tools/python/scripts/convert-legacy-stream
> index 227e1b5c3fd3..7fe375a66810 100755
> --- a/tools/python/scripts/convert-legacy-stream
> +++ b/tools/python/scripts/convert-legacy-stream
> @@ -346,9 +346,9 @@ def read_libxl_toolstack(vm, data):
>          if bytearray(name)[-1] != 0:
>              raise StreamError("physmap name not NUL terminated")
>  
> -        root = b"physmap/%x" % (phys, )
> -        kv = [root + b"/start_addr", b"%x" % (start, ),
> -              root + b"/size",       b"%x" % (size, ),
> +        root = ("physmap/%x" % (phys, )).encode('ascii')
> +        kv = [root + b"/start_addr", ("%x" % (start, )).encode('ascii'),
> +              root + b"/size",       ("%x" % (size, )).encode('ascii'),
>                root + b"/name",       name[:-1]]
>  
>          for key, val in zip(kv[0::2], kv[1::2]):
> -- 
> 2.11.0
>
diff mbox series

Patch

diff --git a/tools/python/scripts/convert-legacy-stream b/tools/python/scripts/convert-legacy-stream
index 227e1b5c3fd3..7fe375a66810 100755
--- a/tools/python/scripts/convert-legacy-stream
+++ b/tools/python/scripts/convert-legacy-stream
@@ -346,9 +346,9 @@  def read_libxl_toolstack(vm, data):
         if bytearray(name)[-1] != 0:
             raise StreamError("physmap name not NUL terminated")
 
-        root = b"physmap/%x" % (phys, )
-        kv = [root + b"/start_addr", b"%x" % (start, ),
-              root + b"/size",       b"%x" % (size, ),
+        root = ("physmap/%x" % (phys, )).encode('ascii')
+        kv = [root + b"/start_addr", ("%x" % (start, )).encode('ascii'),
+              root + b"/size",       ("%x" % (size, )).encode('ascii'),
               root + b"/name",       name[:-1]]
 
         for key, val in zip(kv[0::2], kv[1::2]):