Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f30bb0e

Browse files
committed
Patch #1762412: Fix test case for struni branch.
1 parent 3e1f85e commit f30bb0e

1 file changed

Lines changed: 20 additions & 44 deletions

File tree

Lib/xml/etree/ElementTree.py

Lines changed: 20 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -634,16 +634,16 @@ def write(self, file, encoding="us-ascii"):
634634
if not encoding:
635635
encoding = "us-ascii"
636636
elif encoding != "utf-8" and encoding != "us-ascii":
637-
file.write("<?xml version='1.0' encoding='%s'?>\n" % encoding)
637+
file.write(_encode("<?xml version='1.0' encoding='%s'?>\n" % encoding, encoding))
638638
self._write(file, self._root, encoding, {})
639639

640640
def _write(self, file, node, encoding, namespaces):
641641
# write XML to file
642642
tag = node.tag
643643
if tag is Comment:
644-
file.write("<!-- %s -->" % _escape_cdata(node.text, encoding))
644+
file.write(_encode("<!-- %s -->" % _escape_cdata(node.text), encoding))
645645
elif tag is ProcessingInstruction:
646-
file.write("<?%s?>" % _escape_cdata(node.text, encoding))
646+
file.write(_encode("<?%s?>" % _escape_cdata(node.text), encoding))
647647
else:
648648
items = list(node.items())
649649
xmlns_items = [] # new namespaces in this scope
@@ -653,7 +653,7 @@ def _write(self, file, node, encoding, namespaces):
653653
if xmlns: xmlns_items.append(xmlns)
654654
except TypeError:
655655
_raise_serialization_error(tag)
656-
file.write("<" + _encode(tag, encoding))
656+
file.write(_encode("<" + tag, encoding))
657657
if items or xmlns_items:
658658
items.sort() # lexical order
659659
for k, v in items:
@@ -669,24 +669,22 @@ def _write(self, file, node, encoding, namespaces):
669669
if xmlns: xmlns_items.append(xmlns)
670670
except TypeError:
671671
_raise_serialization_error(v)
672-
file.write(" %s=\"%s\"" % (_encode(k, encoding),
673-
_escape_attrib(v, encoding)))
672+
file.write(_encode(" %s=\"%s\"" % (k, _escape_attrib(v)), encoding))
674673
for k, v in xmlns_items:
675-
file.write(" %s=\"%s\"" % (_encode(k, encoding),
676-
_escape_attrib(v, encoding)))
674+
file.write(_encode(" %s=\"%s\"" % (k, _escape_attrib(v)), encoding))
677675
if node.text or len(node):
678-
file.write(">")
676+
file.write(_encode(">", encoding))
679677
if node.text:
680-
file.write(_escape_cdata(node.text, encoding))
678+
file.write(_encode(_escape_cdata(node.text), encoding))
681679
for n in node:
682680
self._write(file, n, encoding, namespaces)
683-
file.write("</" + _encode(tag, encoding) + ">")
681+
file.write(_encode("</" + tag + ">", encoding))
684682
else:
685-
file.write(" />")
683+
file.write(_encode(" />", encoding))
686684
for k, v in xmlns_items:
687685
del namespaces[v]
688686
if node.tail:
689-
file.write(_escape_cdata(node.tail, encoding))
687+
file.write(_encode(_escape_cdata(node.tail), encoding))
690688

691689
# --------------------------------------------------------------------
692690
# helpers
@@ -727,10 +725,7 @@ def _encode(s, encoding):
727725
except AttributeError:
728726
return s # 1.5.2: assume the string uses the right encoding
729727

730-
if sys.version[:3] == "1.5":
731-
_escape = re.compile(r"[&<>\"\x80-\xff]+") # 1.5.2
732-
else:
733-
_escape = re.compile(eval(r'u"[&<>\"\u0080-\uffff]+"'))
728+
_escape = re.compile(r"[&<>\"\u0080-\uffff]+")
734729

735730
_escape_map = {
736731
"&": "&amp;",
@@ -772,29 +767,19 @@ def escape_entities(m, map=_escape_map):
772767
# the following functions assume an ascii-compatible encoding
773768
# (or "utf-16")
774769

775-
def _escape_cdata(text, encoding=None):
770+
def _escape_cdata(text):
776771
# escape character data
777772
try:
778-
if encoding:
779-
try:
780-
text = _encode(text, encoding)
781-
except UnicodeError:
782-
return _encode_entity(text)
783773
text = text.replace("&", "&amp;")
784774
text = text.replace("<", "&lt;")
785775
text = text.replace(">", "&gt;")
786776
return text
787777
except (TypeError, AttributeError):
788778
_raise_serialization_error(text)
789779

790-
def _escape_attrib(text, encoding=None):
780+
def _escape_attrib(text):
791781
# escape attribute value
792782
try:
793-
if encoding:
794-
try:
795-
text = _encode(text, encoding)
796-
except UnicodeError:
797-
return _encode_entity(text)
798783
text = text.replace("&", "&amp;")
799784
text = text.replace("'", "&apos;") # FIXME: overkill
800785
text = text.replace("\"", "&quot;")
@@ -982,7 +967,7 @@ class dummy:
982967
file = dummy()
983968
file.write = data.append
984969
ElementTree(element).write(file, encoding)
985-
return "".join(data)
970+
return b"".join(data)
986971

987972
##
988973
# Generic element structure builder. This builder converts a sequence
@@ -1114,20 +1099,11 @@ def __init__(self, html=0, target=None):
11141099
parser.StartElementHandler = self._start_list
11151100
except AttributeError:
11161101
pass
1117-
encoding = None
1118-
if not parser.returns_unicode:
1119-
encoding = "utf-8"
1102+
encoding = "utf-8"
11201103
# target.xml(encoding, None)
11211104
self._doctype = None
11221105
self.entity = {}
11231106

1124-
def _fixtext(self, text):
1125-
# convert text string to ascii, if possible
1126-
try:
1127-
return _encode(text, "ascii")
1128-
except UnicodeError:
1129-
return text
1130-
11311107
def _fixname(self, key):
11321108
# expand qname, and convert name string to ascii, if possible
11331109
try:
@@ -1136,15 +1112,15 @@ def _fixname(self, key):
11361112
name = key
11371113
if "}" in name:
11381114
name = "{" + name
1139-
self._names[key] = name = self._fixtext(name)
1115+
self._names[key] = name
11401116
return name
11411117

11421118
def _start(self, tag, attrib_in):
11431119
fixname = self._fixname
11441120
tag = fixname(tag)
11451121
attrib = {}
11461122
for key, value in attrib_in.items():
1147-
attrib[fixname(key)] = self._fixtext(value)
1123+
attrib[fixname(key)] = value
11481124
return self._target.start(tag, attrib)
11491125

11501126
def _start_list(self, tag, attrib_in):
@@ -1153,11 +1129,11 @@ def _start_list(self, tag, attrib_in):
11531129
attrib = {}
11541130
if attrib_in:
11551131
for i in range(0, len(attrib_in), 2):
1156-
attrib[fixname(attrib_in[i])] = self._fixtext(attrib_in[i+1])
1132+
attrib[fixname(attrib_in[i])] = attrib_in[i+1]
11571133
return self._target.start(tag, attrib)
11581134

11591135
def _data(self, text):
1160-
return self._target.data(self._fixtext(text))
1136+
return self._target.data(text)
11611137

11621138
def _end(self, tag):
11631139
return self._target.end(self._fixname(tag))

0 commit comments

Comments
 (0)