33
31
from xml.etree.cElementTree import (ElementTree, SubElement, Element,
34
32
XMLTreeBuilder, fromstring, tostring)
35
33
import xml.etree as elementtree
36
# Also import ElementTree module so monkey-patching below always works
37
import xml.etree.ElementTree
38
34
except ImportError:
39
35
from cElementTree import (ElementTree, SubElement, Element,
40
36
XMLTreeBuilder, fromstring, tostring)
115
108
return ElementTree().parse(f)
111
# performance tuning for elementree's serialiser. This should be
112
# sent upstream - RBC 20060523.
113
# the functions here are patched into elementtree at runtime.
115
escape_re = re.compile("[&'\"<>]")
118
"'":"'", # FIXME: overkill
123
def _escape_replace(match, map=escape_map):
124
return map[match.group()]
126
def _escape_attrib(text, encoding=None, replace=None):
127
# escape attribute value
131
text = elementtree.ElementTree._encode(text, encoding)
133
return elementtree.ElementTree._encode_entity(text)
135
return escape_re.sub(_escape_replace, text)
137
text = replace(text, "&", "&")
138
text = replace(text, "'", "'") # FIXME: overkill
139
text = replace(text, "\"", """)
140
text = replace(text, "<", "<")
141
text = replace(text, ">", ">")
143
except (TypeError, AttributeError):
144
elementtree.ElementTree._raise_serialization_error(text)
146
elementtree.ElementTree._escape_attrib = _escape_attrib
148
escape_cdata_re = re.compile("[&<>]")
154
def _escape_cdata_replace(match, map=escape_cdata_map):
155
return map[match.group()]
157
def _escape_cdata(text, encoding=None, replace=None):
158
# escape character data
162
text = elementtree.ElementTree._encode(text, encoding)
164
return elementtree.ElementTree._encode_entity(text)
166
return escape_cdata_re.sub(_escape_cdata_replace, text)
168
text = replace(text, "&", "&")
169
text = replace(text, "<", "<")
170
text = replace(text, ">", ">")
172
except (TypeError, AttributeError):
173
elementtree.ElementTree._raise_serialization_error(text)
175
elementtree.ElementTree._escape_cdata = _escape_cdata
118
178
def escape_invalid_chars(message):
119
179
"""Escape the XML-invalid characters in a commit message.