Fix _get_tag_name() crash for non-string elem.tag (#7686)

This commit is contained in:
MeGaurav4 2026-06-27 22:38:22 +05:30
parent 185d6b9a20
commit 3977e561c0
2 changed files with 23 additions and 4 deletions

View File

@ -97,10 +97,11 @@ class Sitemap:
@staticmethod
def _get_tag_name(elem: lxml.etree._Element) -> str:
if TYPE_CHECKING:
assert isinstance(elem.tag, str)
_, _, localname = elem.tag.partition("}")
return localname or elem.tag
tag = elem.tag
if not isinstance(tag, str):
return ""
_, _, localname = tag.partition("}")
return localname or tag
def sitemap_urls_from_robots(

View File

@ -311,3 +311,21 @@ def test_xml_entity_expansion():
"""
)
assert list(s) == [{"loc": "http://127.0.0.1:8000/"}]
def test_sitemap_non_string_tag():
"""Regression test for non-string elem.tag crashing _get_tag_name.
When parsing malformed XML with recover=True and resolve_entities=False,
libxml2 can produce nodes whose .tag is not a string (e.g. a Cython
function). _get_tag_name must handle this gracefully instead of raising
AttributeError.
"""
from base64 import b64decode
data = b64decode(
"PHVybGluaz48dXI8dXJsPgAAAAAAAAAEaW5rIHhtbG5zOnhodG1sPSJoZGwiIGhyZWY9ImhsPjxsaW4mazsiaGxybGtuazx4aHRtbDpsaW5rIHhtbG5zOnhodG1sPSJoZGwiIGhyZWY9ImhsaW5rbCIgaHJlZj0iaGw+PHhodG1sZGxzZXQ+"
)
# Must not raise AttributeError
results = list(Sitemap(data))
assert isinstance(results, list)