Skip to content

Commit e8113f5

Browse files
authored
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)
1 parent 7e954e7 commit e8113f5

File tree

4 files changed

+7
-11
lines changed

4 files changed

+7
-11
lines changed

Doc/library/xml.etree.elementtree.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ Element Objects
764764
Finds the first subelement matching *match*. *match* may be a tag name
765765
or a :ref:`path <elementtree-xpath>`. Returns an element instance
766766
or ``None``. *namespaces* is an optional mapping from namespace prefix
767-
to full name. Pass ``None`` as prefix to move all unprefixed tag names
767+
to full name. Pass ``''`` as prefix to move all unprefixed tag names
768768
in the expression into the given namespace.
769769

770770

@@ -773,7 +773,7 @@ Element Objects
773773
Finds all matching subelements, by tag name or
774774
:ref:`path <elementtree-xpath>`. Returns a list containing all matching
775775
elements in document order. *namespaces* is an optional mapping from
776-
namespace prefix to full name. Pass ``None`` as prefix to move all
776+
namespace prefix to full name. Pass ``''`` as prefix to move all
777777
unprefixed tag names in the expression into the given namespace.
778778

779779

@@ -784,7 +784,7 @@ Element Objects
784784
of the first matching element, or *default* if no element was found.
785785
Note that if the matching element has no text content an empty string
786786
is returned. *namespaces* is an optional mapping from namespace prefix
787-
to full name. Pass ``None`` as prefix to move all unprefixed tag names
787+
to full name. Pass ``''`` as prefix to move all unprefixed tag names
788788
in the expression into the given namespace.
789789

790790

Lib/test/test_xml_etree.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2463,7 +2463,7 @@ def test_findall_different_nsmaps(self):
24632463
nsmap = {'xx': 'Y'}
24642464
self.assertEqual(len(root.findall(".//xx:b", namespaces=nsmap)), 1)
24652465
self.assertEqual(len(root.findall(".//b", namespaces=nsmap)), 2)
2466-
nsmap = {'xx': 'X', None: 'Y'}
2466+
nsmap = {'xx': 'X', '': 'Y'}
24672467
self.assertEqual(len(root.findall(".//xx:b", namespaces=nsmap)), 2)
24682468
self.assertEqual(len(root.findall(".//b", namespaces=nsmap)), 1)
24692469

Lib/xml/etree/ElementPath.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
)
7272

7373
def xpath_tokenizer(pattern, namespaces=None):
74-
default_namespace = namespaces.get(None) if namespaces else None
74+
default_namespace = namespaces.get('') if namespaces else None
7575
for token in xpath_tokenizer_re.findall(pattern):
7676
tag = token[1]
7777
if tag and tag[0] != "{":
@@ -275,11 +275,7 @@ def iterfind(elem, path, namespaces=None):
275275

276276
cache_key = (path,)
277277
if namespaces:
278-
if None in namespaces:
279-
cache_key += (namespaces[None],) + tuple(sorted(
280-
item for item in namespaces.items() if item[0] is not None))
281-
else:
282-
cache_key += tuple(sorted(namespaces.items()))
278+
cache_key += tuple(sorted(namespaces.items()))
283279

284280
try:
285281
selector = _cache[cache_key]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
Path expressions in xml.etree.ElementTree can now avoid explicit namespace
22
prefixes for tags (or the "{namespace}tag" notation) by passing a default
3-
namespace with a 'None' prefix.
3+
namespace with an empty string prefix.

0 commit comments

Comments
 (0)