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
111
return ElementTree().parse(f)
114
# performance tuning for elementree's serialiser. This should be
115
# sent upstream - RBC 20060523.
116
# the functions here are patched into elementtree at runtime.
118
escape_re = re.compile("[&'\"<>]")
121
"'":"'", # FIXME: overkill
126
def _escape_replace(match, map=escape_map):
127
return map[match.group()]
129
def _escape_attrib(text, encoding=None, replace=None):
130
# escape attribute value
134
text = elementtree.ElementTree._encode(text, encoding)
136
return elementtree.ElementTree._encode_entity(text)
138
return escape_re.sub(_escape_replace, text)
140
text = replace(text, "&", "&")
141
text = replace(text, "'", "'") # FIXME: overkill
142
text = replace(text, "\"", """)
143
text = replace(text, "<", "<")
144
text = replace(text, ">", ">")
146
except (TypeError, AttributeError):
147
elementtree.ElementTree._raise_serialization_error(text)
149
elementtree.ElementTree._escape_attrib = _escape_attrib
151
escape_cdata_re = re.compile("[&<>]")
157
def _escape_cdata_replace(match, map=escape_cdata_map):
158
return map[match.group()]
160
def _escape_cdata(text, encoding=None, replace=None):
161
# escape character data
165
text = elementtree.ElementTree._encode(text, encoding)
167
return elementtree.ElementTree._encode_entity(text)
169
return escape_cdata_re.sub(_escape_cdata_replace, text)
171
text = replace(text, "&", "&")
172
text = replace(text, "<", "<")
173
text = replace(text, ">", ">")
175
except (TypeError, AttributeError):
176
elementtree.ElementTree._raise_serialization_error(text)
178
elementtree.ElementTree._escape_cdata = _escape_cdata
118
181
def escape_invalid_chars(message):
119
182
"""Escape the XML-invalid characters in a commit message.