Commit e8113f51 authored by Stefan Behnel's avatar Stefan Behnel Committed by GitHub

bpo-30485: Change the prefix for defining the default namespace in ElementPath...

bpo-30485: Change the prefix for defining the default namespace in ElementPath from None to '' since there is existing code that uses that and it's more convenient to have an all-string-keys dict (e.g. when sorting items etc.). (#12860)
parent 7e954e7d
...@@ -764,7 +764,7 @@ Element Objects ...@@ -764,7 +764,7 @@ Element Objects
Finds the first subelement matching *match*. *match* may be a tag name Finds the first subelement matching *match*. *match* may be a tag name
or a :ref:`path <elementtree-xpath>`. Returns an element instance or a :ref:`path <elementtree-xpath>`. Returns an element instance
or ``None``. *namespaces* is an optional mapping from namespace prefix or ``None``. *namespaces* is an optional mapping from namespace prefix
to full name. Pass ``None`` as prefix to move all unprefixed tag names to full name. Pass ``''`` as prefix to move all unprefixed tag names
in the expression into the given namespace. in the expression into the given namespace.
...@@ -773,7 +773,7 @@ Element Objects ...@@ -773,7 +773,7 @@ Element Objects
Finds all matching subelements, by tag name or Finds all matching subelements, by tag name or
:ref:`path <elementtree-xpath>`. Returns a list containing all matching :ref:`path <elementtree-xpath>`. Returns a list containing all matching
elements in document order. *namespaces* is an optional mapping from elements in document order. *namespaces* is an optional mapping from
namespace prefix to full name. Pass ``None`` as prefix to move all namespace prefix to full name. Pass ``''`` as prefix to move all
unprefixed tag names in the expression into the given namespace. unprefixed tag names in the expression into the given namespace.
...@@ -784,7 +784,7 @@ Element Objects ...@@ -784,7 +784,7 @@ Element Objects
of the first matching element, or *default* if no element was found. of the first matching element, or *default* if no element was found.
Note that if the matching element has no text content an empty string Note that if the matching element has no text content an empty string
is returned. *namespaces* is an optional mapping from namespace prefix is returned. *namespaces* is an optional mapping from namespace prefix
to full name. Pass ``None`` as prefix to move all unprefixed tag names to full name. Pass ``''`` as prefix to move all unprefixed tag names
in the expression into the given namespace. in the expression into the given namespace.
......
...@@ -2463,7 +2463,7 @@ class ElementFindTest(unittest.TestCase): ...@@ -2463,7 +2463,7 @@ class ElementFindTest(unittest.TestCase):
nsmap = {'xx': 'Y'} nsmap = {'xx': 'Y'}
self.assertEqual(len(root.findall(".//xx:b", namespaces=nsmap)), 1) self.assertEqual(len(root.findall(".//xx:b", namespaces=nsmap)), 1)
self.assertEqual(len(root.findall(".//b", namespaces=nsmap)), 2) self.assertEqual(len(root.findall(".//b", namespaces=nsmap)), 2)
nsmap = {'xx': 'X', None: 'Y'} nsmap = {'xx': 'X', '': 'Y'}
self.assertEqual(len(root.findall(".//xx:b", namespaces=nsmap)), 2) self.assertEqual(len(root.findall(".//xx:b", namespaces=nsmap)), 2)
self.assertEqual(len(root.findall(".//b", namespaces=nsmap)), 1) self.assertEqual(len(root.findall(".//b", namespaces=nsmap)), 1)
......
...@@ -71,7 +71,7 @@ xpath_tokenizer_re = re.compile( ...@@ -71,7 +71,7 @@ xpath_tokenizer_re = re.compile(
) )
def xpath_tokenizer(pattern, namespaces=None): def xpath_tokenizer(pattern, namespaces=None):
default_namespace = namespaces.get(None) if namespaces else None default_namespace = namespaces.get('') if namespaces else None
for token in xpath_tokenizer_re.findall(pattern): for token in xpath_tokenizer_re.findall(pattern):
tag = token[1] tag = token[1]
if tag and tag[0] != "{": if tag and tag[0] != "{":
...@@ -275,10 +275,6 @@ def iterfind(elem, path, namespaces=None): ...@@ -275,10 +275,6 @@ def iterfind(elem, path, namespaces=None):
cache_key = (path,) cache_key = (path,)
if namespaces: if namespaces:
if None in namespaces:
cache_key += (namespaces[None],) + tuple(sorted(
item for item in namespaces.items() if item[0] is not None))
else:
cache_key += tuple(sorted(namespaces.items())) cache_key += tuple(sorted(namespaces.items()))
try: try:
......
Path expressions in xml.etree.ElementTree can now avoid explicit namespace Path expressions in xml.etree.ElementTree can now avoid explicit namespace
prefixes for tags (or the "{namespace}tag" notation) by passing a default prefixes for tags (or the "{namespace}tag" notation) by passing a default
namespace with a 'None' prefix. namespace with an empty string prefix.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment