summaryrefslogtreecommitdiffstats
path: root/Lib/xml/etree/ElementTree.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2007-08-09 01:03:29 (GMT)
committerGuido van Rossum <guido@python.org>2007-08-09 01:03:29 (GMT)
commit34d1928766bcae11d8f679a6bf361d9210b6429e (patch)
tree0b3d5842a690f811fa03f3b52420bbab9f0f5524 /Lib/xml/etree/ElementTree.py
parent918f49e645474382251bfddbb0a2e030051083ef (diff)
downloadcpython-34d1928766bcae11d8f679a6bf361d9210b6429e.zip
cpython-34d1928766bcae11d8f679a6bf361d9210b6429e.tar.gz
cpython-34d1928766bcae11d8f679a6bf361d9210b6429e.tar.bz2
SF patch# 1770008 by Christian Heimes (plus some extras).
Completely get rid of StringIO.py and cStringIO.c. I had to fix a few tests and modules beyond what Christian did, and invent a few conventions. E.g. in elementtree, I chose to write/return Unicode strings whe no encoding is given, but bytes when an explicit encoding is given. Also mimetools was made to always assume binary files.
Diffstat (limited to 'Lib/xml/etree/ElementTree.py')
-rw-r--r--Lib/xml/etree/ElementTree.py29
1 files changed, 17 insertions, 12 deletions
diff --git a/Lib/xml/etree/ElementTree.py b/Lib/xml/etree/ElementTree.py
index 2fba177..782af81 100644
--- a/Lib/xml/etree/ElementTree.py
+++ b/Lib/xml/etree/ElementTree.py
@@ -625,15 +625,16 @@ class ElementTree:
# Writes the element tree to a file, as XML.
#
# @param file A file name, or a file object opened for writing.
- # @param encoding Optional output encoding (default is US-ASCII).
+ # @param encoding Optional output encoding (default is None)
- def write(self, file, encoding="us-ascii"):
+ def write(self, file, encoding=None):
assert self._root is not None
if not hasattr(file, "write"):
- file = open(file, "wb")
- if not encoding:
- encoding = "us-ascii"
- elif encoding != "utf-8" and encoding != "us-ascii":
+ if encoding:
+ file = open(file, "wb")
+ else:
+ file = open(file, "w")
+ if encoding and encoding != "utf-8":
file.write(_encode("<?xml version='1.0' encoding='%s'?>\n" % encoding, encoding))
self._write(file, self._root, encoding, {})
@@ -720,10 +721,10 @@ def dump(elem):
sys.stdout.write("\n")
def _encode(s, encoding):
- try:
+ if encoding:
return s.encode(encoding)
- except AttributeError:
- return s # 1.5.2: assume the string uses the right encoding
+ else:
+ return s
_escape = re.compile(r"[&<>\"\u0080-\uffff]+")
@@ -954,10 +955,11 @@ fromstring = XML
##
# Generates a string representation of an XML element, including all
-# subelements.
+# subelements. If encoding is None, the return type is a string;
+# otherwise it is a bytes array.
#
# @param element An Element instance.
-# @return An encoded string containing the XML data.
+# @return An (optionally) encoded string containing the XML data.
# @defreturn string
def tostring(element, encoding=None):
@@ -967,7 +969,10 @@ def tostring(element, encoding=None):
file = dummy()
file.write = data.append
ElementTree(element).write(file, encoding)
- return b"".join(data)
+ if encoding:
+ return b"".join(data)
+ else:
+ return "".join(data)
##
# Generic element structure builder. This builder converts a sequence