Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Store separate local:prefix string in qnames cache for attributes
This is in preparation of a future change that requires the default
namespace to not be applied to attributes.
  • Loading branch information
mthuurne committed Dec 9, 2018
commit 71119f5f0a9515cab86d31f608e93c32ad99c3e6
21 changes: 12 additions & 9 deletions Lib/xml/etree/ElementTree.py
Original file line number Diff line number Diff line change
Expand Up @@ -836,8 +836,10 @@ def _get_writer(file_or_filename, encoding):
def _namespaces(elem, default_namespace=None):
# identify namespaces used in this tree

# maps qnames to *encoded* prefix:local names
qnames = {None: None}
# maps qnames to *encoded* prefix:local names,
# value is a pair of prefix:local strings,
# second value is for attribute names
qnames = {None: (None, None)}

# maps uri:s to prefixes
namespaces = {}
Expand Down Expand Up @@ -870,7 +872,8 @@ def serialize_qname(qname):

def add_qname(qname):
if qname not in qnames:
qnames[qname] = serialize_qname(qname)
serialized = serialize_qname(qname)
qnames[qname] = (serialized, serialized)

# populate qname and namespaces table
for elem in elem.iter():
Expand Down Expand Up @@ -903,7 +906,7 @@ def _serialize_xml(write, elem, qnames, namespaces,
elif tag is ProcessingInstruction:
write("<?%s?>" % text)
else:
tag = qnames[tag]
tag = qnames[tag][0]
if tag is None:
if text:
write(_escape_cdata(text))
Expand All @@ -927,10 +930,10 @@ def _serialize_xml(write, elem, qnames, namespaces,
if isinstance(k, QName):
k = k.text
if isinstance(v, QName):
v = qnames[v.text]
v = qnames[v.text][0]
else:
v = _escape_attrib(v)
write(" %s=\"%s\"" % (qnames[k], v))
write(" %s=\"%s\"" % (qnames[k][1], v))
if text or len(elem) or not short_empty_elements:
write(">")
if text:
Expand Down Expand Up @@ -960,7 +963,7 @@ def _serialize_html(write, elem, qnames, namespaces, **kwargs):
elif tag is ProcessingInstruction:
write("<?%s?>" % _escape_cdata(text))
else:
tag = qnames[tag]
tag = qnames[tag][0]
if tag is None:
if text:
write(_escape_cdata(text))
Expand All @@ -983,11 +986,11 @@ def _serialize_html(write, elem, qnames, namespaces, **kwargs):
if isinstance(k, QName):
k = k.text
if isinstance(v, QName):
v = qnames[v.text]
v = qnames[v.text][0]
else:
v = _escape_attrib_html(v)
# FIXME: handle boolean attributes
write(" %s=\"%s\"" % (qnames[k], v))
write(" %s=\"%s\"" % (qnames[k][1], v))
write(">")
ltag = tag.lower()
if text:
Expand Down