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

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

* test: improve non-string tag test accuracy and add direct unit test

* test: use minimal payload for non-string tag test

* test: address Adrian+syncrain PR feedback

- remove first docstring line (Adrian: unnecessary)
- replace weak isinstance assert with no-op call
- keep Cython function mention (Adrian: wording is great)

* test: replace silent call with assert results == [] per Adrian

* chore: drop accidental pyproject.toml and uv.lock changes
This commit is contained in:
Gaurav Yadav 2026-06-30 18:19:47 +05:30 committed by GitHub
parent 6ad8a043ca
commit deb7e2861e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 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,14 @@ def test_xml_entity_expansion():
"""
)
assert list(s) == [{"loc": "http://127.0.0.1:8000/"}]
def test_sitemap_non_string_tag():
"""With recover=True and resolve_entities=False, libxml2 >= 2.14.6 (used
by lxml >= 6.1.1) preserves undeclared entity reference nodes whose
.tag is a non-string ``Cython function`` object instead of a ``str``.
_get_tag_name must handle this gracefully instead of raising
AttributeError.
"""
results = list(Sitemap(b"<url>&k;"))
assert results == []