40
41
def _ensure_utf8_re():
41
"""Make sure the _utf8_re regex has been compiled"""
43
if _utf8_re is not None:
45
_utf8_re = re.compile(u'[&<>\'\"\u0080-\uffff]')
48
def _utf8_escape_replace(match, _map=_utf8_escape_map):
42
"""Make sure the _utf8_re and _unicode_re regexes have been compiled."""
43
global _utf8_re, _unicode_re
45
_utf8_re = re.compile('[&<>\'\"]|[\x80-\xff]+')
46
if _unicode_re is None:
47
_unicode_re = re.compile(u'[&<>\'\"\u0080-\uffff]')
50
def _unicode_escape_replace(match, _map=_xml_escape_map):
49
51
"""Replace a string of non-ascii, non XML safe characters with their escape
51
53
This will escape both Standard XML escapes, like <>"', etc.
64
66
return "&#%d;" % ord(match.group())
67
_unicode_to_escaped_map = {}
69
def _encode_and_escape(unicode_str, _map=_unicode_to_escaped_map):
69
def _utf8_escape_replace(match, _map=_xml_escape_map):
70
"""Escape utf8 characters into XML safe ones.
72
This uses 2 tricks. It is either escaping "standard" characters, like "&<>,
73
or it is handling characters with the high-bit set. For ascii characters,
74
we just lookup the replacement in the dictionary. For everything else, we
75
decode back into Unicode, and then use the XML escape code.
78
return _map[match.group()]
80
return ''.join('&#%d;' % ord(uni_chr)
81
for uni_chr in match.group().decode('utf8'))
86
def _encode_and_escape(unicode_or_utf8_str, _map=_to_escaped_map):
70
87
"""Encode the string into utf8, and escape invalid XML characters"""
71
88
# We frequently get entities we have not seen before, so it is better
72
89
# to check if None, rather than try/KeyError
73
text = _map.get(unicode_str)
90
text = _map.get(unicode_or_utf8_str)
75
# The alternative policy is to do a regular UTF8 encoding
76
# and then escape only XML meta characters.
77
# Performance is equivalent once you use cache_utf8. *However*
78
# this makes the serialized texts incompatible with old versions
79
# of bzr. So no net gain. (Perhaps the read code would handle utf8
80
# better than entity escapes, but cElementTree seems to do just fine
82
text = str(_utf8_re.sub(_utf8_escape_replace, unicode_str)) + '"'
83
_map[unicode_str] = text
92
if unicode_or_utf8_str.__class__ == unicode:
93
# The alternative policy is to do a regular UTF8 encoding
94
# and then escape only XML meta characters.
95
# Performance is equivalent once you use cache_utf8. *However*
96
# this makes the serialized texts incompatible with old versions
97
# of bzr. So no net gain. (Perhaps the read code would handle utf8
98
# better than entity escapes, but cElementTree seems to do just fine
100
text = str(_unicode_re.sub(_unicode_escape_replace,
101
unicode_or_utf8_str)) + '"'
103
# Plain strings are considered to already be in utf-8 so we do a
104
# slightly different method for escaping.
105
text = _utf8_re.sub(_utf8_escape_replace,
106
unicode_or_utf8_str) + '"'
107
_map[unicode_or_utf8_str] = text
111
def _get_utf8_or_ascii(a_str,
112
_encode_utf8=cache_utf8.encode,
113
_get_cached_ascii=cache_utf8.get_cached_ascii):
114
"""Return a cached version of the string.
116
cElementTree will return a plain string if the XML is plain ascii. It only
117
returns Unicode when it needs to. We want to work in utf-8 strings. So if
118
cElementTree returns a plain string, we can just return the cached version.
119
If it is Unicode, then we need to encode it.
121
:param a_str: An 8-bit string or Unicode as returned by
122
cElementTree.Element.get()
123
:return: A utf-8 encoded 8-bit string.
125
# This is fairly optimized because we know what cElementTree does, this is
126
# not meant as a generic function for all cases. Because it is possible for
127
# an 8-bit string to not be ascii or valid utf8.
128
if a_str.__class__ == unicode:
129
return _encode_utf8(a_str)
131
return _get_cached_ascii(a_str)
87
134
def _clear_cache():
88
135
"""Clean out the unicode => escaped map"""
89
_unicode_to_escaped_map.clear()
136
_to_escaped_map.clear()
92
139
class Serializer_v5(Serializer):
179
226
def _pack_revision(self, rev):
180
227
"""Revision object -> xml tree"""
228
# For the XML format, we need to write them as Unicode rather than as
229
# utf-8 strings. So that cElementTree can handle properly escaping
231
decode_utf8 = cache_utf8.decode
232
revision_id = rev.revision_id
233
if isinstance(revision_id, str):
234
revision_id = decode_utf8(revision_id)
181
235
root = Element('revision',
182
236
committer = rev.committer,
183
237
timestamp = '%.3f' % rev.timestamp,
184
revision_id = rev.revision_id,
238
revision_id = revision_id,
185
239
inventory_sha1 = rev.inventory_sha1,