qapi: Open files with encoding='utf-8'

Python 2 happily reads UTF-8 files in text mode, but Python 3 requires
either UTF-8 locale or an explicit encoding passed to open(). Commit
d4e5ec877ca fixed this by setting the en_US.UTF-8 locale. Falls apart
when the locale isn't be available.

Matthias Maier and Arfrever Frehtes Taifersar Arahesis proposed to use
binary mode instead, with manual conversion from bytes to str. Works,
but opening with an explicit encoding is simpler, so do that.

Since Python 2's open() doesn't support the encoding parameter, we
need to suppress it with a version check.

Backports commit de685ae5e9a4b523513033bd6cadc8187a227170 from qemu
This commit is contained in:
Markus Armbruster 2018-07-02 23:01:21 -04:00 committed by Lioncash
parent 559833a0d4
commit af4b0028b8
No known key found for this signature in database
GPG key ID: 4E3C3CC1031BA9C7

View file

@ -16,6 +16,7 @@ import errno
import os
import re
import string
import sys
try:
from collections import OrderedDict
except:
@ -352,7 +353,10 @@ class QAPISchemaParser(object):
if incl_abs_fname in previously_included:
return None
try:
fobj = open(incl_fname, 'r')
if sys.version_info[0] >= 3:
fobj = open(incl_fname, 'r', encoding='utf-8')
else:
fobj = open(incl_fname, 'r')
except IOError as e:
raise QAPISemError(info, '%s: %s' % (e.strerror, incl_fname))
return QAPISchemaParser(fobj, previously_included, info)
@ -1499,7 +1503,11 @@ class QAPISchemaEvent(QAPISchemaEntity):
class QAPISchema(object):
def __init__(self, fname):
self._fname = fname
parser = QAPISchemaParser(open(fname, 'r'))
if sys.version_info[0] >= 3:
f = open(fname, 'r', encoding='utf-8')
else:
f = open(fname, 'r')
parser = QAPISchemaParser(f)
exprs = check_exprs(parser.exprs)
self.docs = parser.docs
self._entity_list = []
@ -2006,7 +2014,10 @@ 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+')
if sys.version_info[0] >= 3:
f = open(fd, 'r+', encoding='utf-8')
else:
f = os.fdopen(fd, 'r+')
text = (self._top(fname) + self._preamble + self._body
+ self._bottom(fname))
oldtext = f.read(len(text) + 1)