summaryrefslogtreecommitdiff
path: root/python_modules/codegen.py
diff options
context:
space:
mode:
authorAlexander Wauck <awauck@codeweavers.com>2015-03-31 12:44:09 -0500
committerChristophe Fergeau <cfergeau@redhat.com>2015-04-01 13:39:03 +0200
commitba52c4cae204c4fc7104b14ac446d4d5a65a26f8 (patch)
tree12ac2dc1916f69f524b6b4bc588c476941759c19 /python_modules/codegen.py
parent14218d7d6b9524e4f7854c27bab8b5fdf3b4e88a (diff)
Make spice_codegen.py work on both Python 2 and 3
This is a new version of my previous patch that does not include six.py. It's still kind of big, but at least it's all spice-common changes now. There are also a few other fixes that Christophe brought to my attention. Note that six now needs to be installed on the system (python-six on Fedora and Debian, six on PyPI). This *should* be enough to make spice_codegen.py work on both Python 2 and Python 3. The major changes are as follows: * cStringIO.StringIO -> io.StringIO * str vs. unicode updates (io.StringIO doesn't like str) * integer division * foo.has_key(bar) -> bar in foo * import internal_thing -> from . import internal_thing * removed from __future__ import with_statement (might break Python 2.5?) * changed some lambdas to list comprehensions (done by 2to3) * cast some_dict.keys() to list where needed (e.g. for sorting) * use normal type names with isinstance instead of types.WhateverType Signed-off-by: Alexander Wauck <awauck@codeweavers.com>
Diffstat (limited to 'python_modules/codegen.py')
-rw-r--r--python_modules/codegen.py28
1 files changed, 16 insertions, 12 deletions
diff --git a/python_modules/codegen.py b/python_modules/codegen.py
index 009cf95..55f513b 100644
--- a/python_modules/codegen.py
+++ b/python_modules/codegen.py
@@ -1,5 +1,6 @@
-from __future__ import with_statement
-from cStringIO import StringIO
+
+import six
+from io import StringIO
def camel_to_underscores(s, upper = False):
res = ""
@@ -85,10 +86,10 @@ class CodeWriter:
self.options[opt] = value
def has_option(self, opt):
- return self.options.has_key(opt)
+ return opt in self.options
def set_is_generated(self, kind, name):
- if not self.generated.has_key(kind):
+ if kind not in self.generated:
v = {}
self.generated[kind] = v
else:
@@ -96,13 +97,13 @@ class CodeWriter:
v[name] = 1
def is_generated(self, kind, name):
- if not self.generated.has_key(kind):
+ if kind not in self.generated:
return False
v = self.generated[kind]
- return v.has_key(name)
+ return name in v
def getvalue(self):
- strs = map(lambda writer: writer.getvalue(), self.contents)
+ strs = [writer.getvalue() for writer in self.contents]
return "".join(strs)
def get_subwriter(self):
@@ -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)
if len(s) == 0:
return
if self.at_line_start:
for i in range(self.indentation):
- self.out.write(" ")
+ self.out.write(u" ")
self.at_line_start = False
self.out.write(s)
return self
def newline(self):
- self.out.write("\n")
+ self.out.write(u"\n")
self.at_line_start = True
return self
@@ -341,7 +345,7 @@ class CodeWriter:
self.indentation = indentation
def add_function_variable(self, ctype, name):
- if self.function_variables.has_key(name):
+ if name in self.function_variables:
assert(self.function_variables[name] == ctype)
else:
self.function_variables[name] = ctype