Preserve Expat qualified names without namespace expansion (#9070)

Retain raw element and attribute prefixes when namespace_separator is None. Keep expanded names and parser configuration unchanged, with regression coverage in the pyexpat snippet.

Assisted-by: OpenAI Codex:model identifier unavailable
This commit is contained in:
youknowdot authored and GitHub committed 2026-10-11 23:48:08 +09:00
1 parent 1fa883b9b5
commit 2ab8af332f
2 files changed
+27

No files matched your search

+4
View File
@@ -1051,6 +1051,10 @@ mod _pyexpat {
fn make_name(&self, name: &xml::name::OwnedName) -> String {
match (&self.namespace_separator, &name.namespace) {
(Some(sep), Some(ns)) => format!("{}{}{}", ns, sep, name.local_name),
(None, _) => match &name.prefix {
Some(prefix) => format!("{prefix}:{}", name.local_name),
None => name.local_name.clone(),
},
_ => name.local_name.clone(),
}
}
+23
View File
@@ -0,0 +1,23 @@
from xml.parsers import expat
# Without namespace expansion, element and attribute names retain prefixes.
for separator, expected_name, expected_attribute in (
(None, "p:x", "p:a"),
("!", "urn:p!x", "urn:p!a"),
):
for ordered in (False, True):
parser = expat.ParserCreate(namespace_separator=separator)
parser.ordered_attributes = ordered
starts = []
ends = []
parser.StartElementHandler = lambda name, attrs: starts.append((name, attrs))
parser.EndElementHandler = ends.append
parser.Parse('<r xmlns:p="urn:p"><p:x p:a="v" a="u"></p:x></r>', True)
assert [name for name, attrs in starts] == ["r", expected_name]
assert ends == [expected_name, "r"]
expected_attributes = (
[expected_attribute, "v", "a", "u"]
if ordered
else {expected_attribute: "v", "a": "u"}
)
assert starts[1][1] == expected_attributes