classification
Title: ElementTree.find attribute matching with empty namespace
Type: behavior Stage: resolved
Components: XML Versions: Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: eli.bendersky, lukasz.langa, scoder, steve.dower
Priority: normal Keywords: 3.7regression

Created on 2019-05-07 13:19 by steve.dower, last changed 2019-05-09 05:25 by scoder. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 13201 merged scoder, 2019-05-08 17:33
Messages (5)
msg341721 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2019-05-07 13:19
I suspect this is related to the fix for issue30485, but haven't fully debugged it.

In PC/layout/support/appxmanifest.py, I have code that looks up an existing element and adds it if missing:

def find_or_add(xml, element, attr=None, always_add=False):
    if always_add:
        e = None
    else:
        q = element
        if attr:
            q += "[@{}='{}']".format(*attr)
        e = xml.find(q, APPXMANIFEST_NS)
    if e is None:
        ...
    return e

For my 3.8.0a4 build, this started failing to match elements with attributes included. As a result, I ended up with N elements in a space where the schema only allows 1, and part of the 3.8.0a4 release is now blocked (okay, it's unblocked because I manually fixed a file and built the rest by hand, but it still held things up by a day).

I found that by removing the empty string entry in my namespaces dictionary the issue is fixed:

APPXMANIFEST_NS = {
    #"": "http://schemas.microsoft.com/appx/manifest/foundation/windows10",
    "m": "http://schemas.microsoft.com/appx/manifest/foundation/windows10",
    ...
}

So I'm not exactly sure what's going on, but as code that worked fine in 3.8.0a3 and earlier now no longer works, I think we should see whether it was a deliberate break or accidental. If there's no reason to regress users, I'd prefer to avoid it.
msg341723 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2019-05-07 13:36
Here is a test case that fails on 3.8.0a4 and succeeds on 3.7.3:

    def test_find_attribute_with_empty_ns(self):
        NS = "http://effbot.org/ns"
        element = ET.fromstring(f"""<body xmlns="{NS}"><section id="1"/></body>""")
        self.assertIsNotNone(element.find("t:section[@id='1']", {"":NS, "t":NS}))
msg341867 - (view) Author: Stefan Behnel (scoder) * (Python committer) Date: 2019-05-08 12:17
Right, thanks for the reproducer. The default namespace should not apply to attributes. I'll write up a PR today.
msg341965 - (view) Author: Stefan Behnel (scoder) * (Python committer) Date: 2019-05-09 05:22
New changeset 88db8bd0648588c67eeab16d0bc72ec5c206e3ad by Stefan Behnel in branch 'master':
bpo-36831: Do not apply default namespace to unprefixed attributes in ElementPath. (#13201)
https://github.com/python/cpython/commit/88db8bd0648588c67eeab16d0bc72ec5c206e3ad
msg341966 - (view) Author: Stefan Behnel (scoder) * (Python committer) Date: 2019-05-09 05:24
Sorry for the annoyance during the release.
History
Date User Action Args
2019-05-09 05:25:16scodersetstage: needs patch -> resolved
2019-05-09 05:24:34scodersetstatus: open -> closed
messages: + msg341966

keywords: - patch
resolution: fixed
stage: patch review -> needs patch
2019-05-09 05:22:53scodersetmessages: + msg341965
2019-05-08 17:33:08scodersetkeywords: + patch
stage: needs patch -> patch review
pull_requests: + pull_request13112
2019-05-08 12:17:34scodersetmessages: + msg341867
2019-05-07 13:36:50steve.dowersetmessages: + msg341723
2019-05-07 13:19:59steve.dowercreate