[Spice-devel] [PATCH] Make spice_codegen.py work on both Python 2 and 3

Christophe Fergeau cfergeau at redhat.com
Tue Mar 31 08:57:59 PDT 2015


Hey,

On Thu, Mar 26, 2015 at 10:56:53AM -0500, Alexander Wauck wrote:
> This *should* be enough to make spice_codegen.py work on both Python 2
> and Python 3.  I have not tested with Python 2.5 or 2.6, though, nor
> have I done particularly thorough testing in general.  I would
> appreciate getting some more knowledgeable eyes on this.

From a quick glance, the changes look sane to me (split patch would have
been even more straightforward to review ;)

> 
> (Sorry for the long diff, but I had to use six.py for the
>  2/3 compatibility.  Should that be something the user is
>  required to install instead of us bundling it?)

First time I hear about six.py, so I don't know about that :(
One comment below:

> diff --git a/python_modules/codegen.py b/python_modules/codegen.py
> index 009cf95..4ff4c55 100644
> --- a/python_modules/codegen.py
> +++ b/python_modules/codegen.py
> @@ -119,21 +120,24 @@ class CodeWriter:
>          return writer
>  
>      def write(self, s):
> -        # Ensure its a string
> -        s = str(s)
> +        # Ensure its a unicode string
> +        if six.PY2:
> +            s = unicode(s)
> +        else:
> +            s = str(s)

Why do we need a unicode string here?

I also had to use this patch or the file generation would first error
out, and then would contain some *ptr += 1.0; rather than *ptr += 1;
With these changes, the files are the same when generated with python2
or python3 on my system (python3-3.4.2-4.fc22.x86_64)

diff --git a/python_modules/demarshal.py b/python_modules/demarshal.py
index f6047b0..8afd9f5 100644
--- a/python_modules/demarshal.py
+++ b/python_modules/demarshal.py
@@ -68,7 +68,7 @@ def write_parser_helpers(writer):
             scope = writer.function("SPICE_GNUC_UNUSED consume_%s" % type, ctype, "uint8_t **ptr", True)
             scope.variable_def(ctype, "val")
             writer.assign("val", "read_%s(*ptr)" % type)
-            writer.increment("*ptr", size / 8)
+            writer.increment("*ptr", int(size / 8))
             writer.statement("return val")
             writer.end_block()
 
diff --git a/python_modules/ptypes.py b/python_modules/ptypes.py
index 7ddc72a..1608485 100644
--- a/python_modules/ptypes.py
+++ b/python_modules/ptypes.py
@@ -168,7 +168,7 @@ class IntegerType(Type):
         return self.name + "_t"
 
     def get_fixed_nw_size(self):
-        return self.bits / 8
+        return int(self.bits / 8)
 
     def is_primitive(self):
         return True
@@ -245,7 +245,7 @@ class EnumBaseType(Type):
         return True
 
     def get_fixed_nw_size(self):
-        return self.bits / 8
+        return int(self.bits / 8)
 
     # generates a value-name table suitable for use with the wireshark protocol
     # dissector

Christophe
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.freedesktop.org/archives/spice-devel/attachments/20150331/b6516a6a/attachment-0001.sig>


More information about the Spice-devel mailing list