QName doesn't work with iterators

Bug #1865141 reported by Xavier (Open ERP)
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
lxml
Fix Released
Medium
Unassigned

Bug Description

The documentation of QName (https://lxml.de/api/lxml.etree.QName-class.html) states:

> You can pass QName objects wherever a tag name is expected.

The signature of `_Element.iter` is defined as:

> iter(self, tag=None, *tags)

from this one would expect to pass a QName directly to iter would work and filter on that qname, however

    t = etree.fromstring("<root/>")
    for node in t.iter(QName('http://example.org/ns', 'a')):
        ...

instead of doing nothing raises "TypeError: 'lxml.etree.QName' object is not iterable".

Workaround: str(QName(...)) but that's not great, and the documentation is still not correct.

Environment:

Python : sys.version_info(major=3, minor=6, micro=9, releaselevel='final', serial=0)
lxml.etree : (4, 5, 0, 0)
libxml used : (2, 9, 10)
libxml compiled : (2, 9, 10)
libxslt used : (1, 1, 34)
libxslt compiled : (1, 1, 34)

description: updated
Revision history for this message
scoder (scoder) wrote :

Ah, yes, thanks for the report. The MultiTagMatcher needs to unpack QNames here:

https://github.com/lxml/lxml/blob/41cc5f378e2454ff1cabe5d227242cce211a3a2b/src/lxml/etree.pyx#L2720

I would just let it create the string and recurse. This is not performance critical at all.
PR welcome, including tests, which should go near the".iter()" tests in "test_etree.py".

Changed in lxml:
importance: Undecided → Low
status: New → Triaged
Revision history for this message
Xavier (Open ERP) (xmo-deactivatedaccount) wrote :

Looking at the qname definition it looks like all that'd be necessary would be an

    elif tag is QName:
        if tag.text in seen:
            return
        seen.add(tag.text)
        self._py_tags.append((tag.namespace or '', tag.localname))

does that seem right?

Revision history for this message
scoder (scoder) wrote :

I would literally go for

    elif isinstance(tag, QName):
        self._storeTags(self, tag.text, seen)

Revision history for this message
Xavier (Open ERP) (xmo-deactivatedaccount) wrote :
scoder (scoder)
Changed in lxml:
importance: Low → Medium
milestone: none → 4.5.1
status: Triaged → Fix Committed
scoder (scoder)
Changed in lxml:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.