summaryrefslogtreecommitdiff
path: root/giscanner/xmlwriter.py
diff options
context:
space:
mode:
authorSimon Feltman <sfeltman@src.gnome.org>2014-04-28 23:35:41 -0700
committerColin Walters <walters@verbum.org>2015-09-29 23:16:32 -0400
commit0211bda3da384818c0f99b5a468022c7529fed7e (patch)
tree4bcdc08fa0c6898d8a9411a73f370254d562eac5 /giscanner/xmlwriter.py
parent374e7e8c62358225be65e4b33dc591003550ab50 (diff)
downloadgobject-introspection-0211bda3da384818c0f99b5a468022c7529fed7e.tar.gz
giscanner: Use StringIO instead of cStringIO in Python 2
Replace usage of the Python 2 cStringIO module with StringIO and conditionally use io.StringIO for Python 3. This is needed to build up a unicode version of the XML since cStringIO does not support unicode. Add XMLWriter.get_encoded_xml() which returns a utf-8 encoded bytes object of the XML data. Open files for reading/writing in binary mode since we explicitly encode and decode as utf-8. https://bugzilla.gnome.org/show_bug.cgi?id=679438
Diffstat (limited to 'giscanner/xmlwriter.py')
-rwxr-xr-xgiscanner/xmlwriter.py27
1 files changed, 21 insertions, 6 deletions
diff --git a/giscanner/xmlwriter.py b/giscanner/xmlwriter.py
index 851c4c07..54419f71 100755
--- a/giscanner/xmlwriter.py
+++ b/giscanner/xmlwriter.py
@@ -25,13 +25,19 @@ from __future__ import print_function
from __future__ import unicode_literals
import os
+import sys
from contextlib import contextmanager
-from cStringIO import StringIO
from xml.sax.saxutils import escape
from .libtoolimporter import LibtoolImporter
+if sys.version_info.major < 3:
+ from StringIO import StringIO
+else:
+ from io import StringIO
+ unicode = str
+
with LibtoolImporter(None, None):
if 'UNINSTALLED_INTROSPECTION_SRCDIR' in os.environ:
@@ -62,8 +68,12 @@ def build_xml_tag(tag_name, attributes=None, data=None, self_indent=0,
class XMLWriter(object):
def __init__(self):
+ # Build up the XML buffer as unicode strings. When writing to disk,
+ # we can assume the lack of a Byte Order Mark (BOM) and lack
+ # of an "encoding" xml property means utf-8.
+ # See: http://www.opentag.com/xfaq_enc.htm#enc_default
self._data = StringIO()
- self._data.write(b'<?xml version="1.0"?>\n')
+ self._data.write('<?xml version="1.0"?>\n')
self._tag_stack = []
self._indent = 0
self._indent_unit = 2
@@ -92,8 +102,13 @@ class XMLWriter(object):
self._newline_char = ''
def get_xml(self):
+ """Returns a unicode string containing the XML."""
return self._data.getvalue()
+ def get_encoded_xml(self):
+ """Returns a utf-8 encoded bytes object containing the XML."""
+ return self._data.getvalue().encode('utf-8')
+
def write_line(self, line='', indent=True, do_escape=False):
if isinstance(line, bytes):
line = line.decode('utf-8')
@@ -101,11 +116,11 @@ class XMLWriter(object):
if do_escape:
line = escape(line)
if indent:
- self._data.write(('%s%s%s' % (self._indent_char * self._indent,
- line,
- self._newline_char)).encode('UTF-8'))
+ self._data.write('%s%s%s' % (self._indent_char * self._indent,
+ line,
+ self._newline_char))
else:
- self._data.write(('%s%s' % (line, self._newline_char)).encode('UTF-8'))
+ self._data.write('%s%s' % (line, self._newline_char))
def write_comment(self, text):
self.write_line('<!-- %s -->' % (text, ))