[Python-3000-checkins] r56587 - in python/branches/py3k-struni/Lib: io.py test/test_minidom.py xml/dom/minidom.py
guido.van.rossum
python-3000-checkins at python.org
Fri Jul 27 20:03:12 CEST 2007
Author: guido.van.rossum
Date: Fri Jul 27 20:03:11 2007
New Revision: 56587
Modified:
python/branches/py3k-struni/Lib/io.py
python/branches/py3k-struni/Lib/test/test_minidom.py
python/branches/py3k-struni/Lib/xml/dom/minidom.py
Log:
Fix the minidom test.
In order to do this, I added an optional encoding argument to io.StringIO.
The toprettyxml() function returns bytes when you specify an encoding now.
Modified: python/branches/py3k-struni/Lib/io.py
==============================================================================
--- python/branches/py3k-struni/Lib/io.py (original)
+++ python/branches/py3k-struni/Lib/io.py Fri Jul 27 20:03:11 2007
@@ -1262,11 +1262,13 @@
# XXX This is really slow, but fully functional
- def __init__(self, initial_value=""):
- super(StringIO, self).__init__(BytesIO(), "utf-8")
+ def __init__(self, initial_value="", encoding="utf-8", newline=None):
+ super(StringIO, self).__init__(BytesIO(),
+ encoding=encoding,
+ newline=newline)
if initial_value:
self.write(initial_value)
self.seek(0)
def getvalue(self):
- return self.buffer.getvalue().decode("utf-8")
+ return self.buffer.getvalue().decode(self._encoding)
Modified: python/branches/py3k-struni/Lib/test/test_minidom.py
==============================================================================
--- python/branches/py3k-struni/Lib/test/test_minidom.py (original)
+++ python/branches/py3k-struni/Lib/test/test_minidom.py Fri Jul 27 20:03:11 2007
@@ -869,17 +869,17 @@
def testEncodings(self):
doc = parseString('<foo>€</foo>')
- self.confirm(doc.toxml() == '<?xml version="1.0" ?><foo>\u20ac</foo>'
- and doc.toxml('utf-8') ==
- '<?xml version="1.0" encoding="utf-8"?><foo>\xe2\x82\xac</foo>'
- and doc.toxml('iso-8859-15') ==
- '<?xml version="1.0" encoding="iso-8859-15"?><foo>\xa4</foo>',
- "testEncodings - encoding EURO SIGN")
+ self.assertEqual(doc.toxml(),
+ '<?xml version="1.0" ?><foo>\u20ac</foo>')
+ self.assertEqual(doc.toxml('utf-8'),
+ b'<?xml version="1.0" encoding="utf-8"?><foo>\xe2\x82\xac</foo>')
+ self.assertEqual(doc.toxml('iso-8859-15'),
+ b'<?xml version="1.0" encoding="iso-8859-15"?><foo>\xa4</foo>')
# Verify that character decoding errors throw exceptions instead
# of crashing
self.assertRaises(UnicodeDecodeError, parseString,
- '<fran\xe7ais>Comment \xe7a va ? Tr\xe8s bien ?</fran\xe7ais>')
+ b'<fran\xe7ais>Comment \xe7a va ? Tr\xe8s bien ?</fran\xe7ais>')
doc.unlink()
Modified: python/branches/py3k-struni/Lib/xml/dom/minidom.py
==============================================================================
--- python/branches/py3k-struni/Lib/xml/dom/minidom.py (original)
+++ python/branches/py3k-struni/Lib/xml/dom/minidom.py Fri Jul 27 20:03:11 2007
@@ -14,6 +14,7 @@
* SAX 2 namespaces
"""
+import io
import xml.dom
from xml.dom import EMPTY_NAMESPACE, EMPTY_PREFIX, XMLNS_NAMESPACE, domreg
@@ -44,20 +45,19 @@
def toxml(self, encoding = None):
return self.toprettyxml("", "", encoding)
- def toprettyxml(self, indent="\t", newl="\n", encoding = None):
+ def toprettyxml(self, indent="\t", newl="\n", encoding=None):
# indent = the indentation string to prepend, per level
# newl = the newline string to append
- writer = _get_StringIO()
- if encoding is not None:
- import codecs
- # Can't use codecs.getwriter to preserve 2.0 compatibility
- writer = codecs.lookup(encoding)[3](writer)
+ writer = io.StringIO(encoding=encoding)
if self.nodeType == Node.DOCUMENT_NODE:
# Can pass encoding only to document, to put it into XML header
self.writexml(writer, "", indent, newl, encoding)
else:
self.writexml(writer, "", indent, newl)
- return writer.getvalue()
+ if encoding is None:
+ return writer.getvalue()
+ else:
+ return writer.buffer.getvalue()
def hasChildNodes(self):
if self.childNodes:
@@ -360,7 +360,7 @@
def _get_localName(self):
if 'localName' in self.__dict__:
- return self.__dict__['localName']
+ return self.__dict__['localName']
return self.nodeName.split(":", 1)[-1]
def _get_name(self):
@@ -665,7 +665,7 @@
def _get_localName(self):
if 'localName' in self.__dict__:
- return self.__dict__['localName']
+ return self.__dict__['localName']
return self.tagName.split(":", 1)[-1]
def _get_tagName(self):
@@ -1897,11 +1897,6 @@
return (None, fields[0])
-def _get_StringIO():
- # we can't use cStringIO since it doesn't support Unicode strings
- from StringIO import StringIO
- return StringIO()
-
def _do_pulldom_parse(func, args, kwargs):
events = func(*args, **kwargs)
toktype, rootNode = events.getEvent()
More information about the Python-3000-checkins
mailing list