diff mbox series

[2/2] qapi: open files in binary mode and use explicit decoding/encoding in common.py

Message ID 20180614154357.20132-3-tamiko@43-1.org
State New
Headers show
Series Fix compilation with python-3 if en_US.UTF-8 is unavailable | expand

Commit Message

Matthias Maier June 14, 2018, 3:43 p.m. UTC
From: Arfrever Frehtes Taifersar Arahesis <arfrever.fta@gmail.com>

This is a different approach to fix the locale dependent encode/decode
problem in common.py utilizing the binary read/write mode [1,2] and
decode/encode with explicit UTF-8 encoding arguments [3].

This approach is preferred over the fix in commit d4e5ec877ca because it
is (a) locale independent, and (b) does not depend on the en_US.UTF_8
locale to be available.

[1] https://docs.python.org/3.6/library/stdtypes.html#bytes.decode
[2] https://docs.python.org/3.6/library/stdtypes.html#str.encode
[3] https://docs.python.org/3/howto/unicode.html

Signed-off-by: Arfrever Frehtes Taifersar Arahesis <arfrever.fta@gmail.com>
Signed-off-by: Matthias Maier <tamiko@43-1.org>
---
 scripts/qapi/common.py | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
diff mbox series

Patch

diff --git a/scripts/qapi/common.py b/scripts/qapi/common.py
index 2462fc0291..8f8ccc65c3 100644
--- a/scripts/qapi/common.py
+++ b/scripts/qapi/common.py
@@ -258,7 +258,7 @@  class QAPISchemaParser(object):
         self.fname = fp.name
         previously_included.append(os.path.abspath(fp.name))
         self.incl_info = incl_info
-        self.src = fp.read()
+        self.src = fp.read().decode("UTF-8")
         if self.src == '' or self.src[-1] != '\n':
             self.src += '\n'
         self.cursor = 0
@@ -340,7 +340,7 @@  class QAPISchemaParser(object):
             return None
 
         try:
-            fobj = open(incl_fname, 'r')
+            fobj = open(incl_fname, 'rb')
         except IOError as e:
             raise QAPISemError(info, '%s: %s' % (e.strerror, incl_fname))
         return QAPISchemaParser(fobj, previously_included, info)
@@ -1492,7 +1492,7 @@  class QAPISchemaEvent(QAPISchemaEntity):
 class QAPISchema(object):
     def __init__(self, fname):
         self._fname = fname
-        parser = QAPISchemaParser(open(fname, 'r'))
+        parser = QAPISchemaParser(open(fname, 'rb'))
         exprs = check_exprs(parser.exprs)
         self.docs = parser.docs
         self._entity_list = []
@@ -2006,14 +2006,14 @@  class QAPIGen(object):
                 if e.errno != errno.EEXIST:
                     raise
         fd = os.open(pathname, os.O_RDWR | os.O_CREAT, 0o666)
-        f = os.fdopen(fd, 'r+')
+        f = os.fdopen(fd, 'r+b')
         text = (self._top(fname) + self._preamble + self._body
                 + self._bottom(fname))
-        oldtext = f.read(len(text) + 1)
+        oldtext = f.read(len(text) + 1).decode("UTF-8")
         if text != oldtext:
             f.seek(0)
             f.truncate(0)
-            f.write(text)
+            f.write(text.encode("UTF-8"))
         f.close()