GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — ElementTree: invalid paths raise KeyError instead of SyntaxError, and C and Python disagree on them · Issue #161808 · python/cpython

ElementTree: invalid paths raise KeyError instead of SyntaxError, and C and Python disagree on them #161808

New issue
Copy link
New issue
Copy link
Open
#161810
Open
ElementTree: invalid paths raise KeyError instead of SyntaxError, and C and Python disagree on them#161808
#161810
Copy link
Labels
extension-modulesC modules in the Modules dirC modules in the Modules dirstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-XMLtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@ines-besrour
ines-besrour
opened on Nov 11, 2026
Issue body actions

Bug report

Bug description

Some malformed paths make find(), findall() and findtext() raise a bare KeyError from ElementPath, while other malformed paths raise SyntaxError("invalid path"). Which paths reach that code also depends on the implementation:

import xml.etree.ElementTree as ET

root = ET.Element("root")
root.find("@")     # KeyError: '@'   (C accelerator and pure Python)
root.find("x=y")   # pure Python: KeyError: '='   C accelerator: None
root.find(")")     # pure Python: KeyError: ')'   C accelerator: None
root.find("]")     # pure Python: KeyError: ']'   C accelerator: None

(The pure-Python results can be reproduced by blocking the accelerator: sys.modules["_elementtree"] = None before importing xml.etree.ElementTree.)

Cause. ElementPath.iterfind() looks up each token with ops[token[0]]. Tokens that start no supported step (@, =, (, ), ], !=, ::) are not in ops, so the lookup raises KeyError. The C accelerator only hands a path to ElementPath when its checkpath() finds one of / * [ @ .; any other path is compared to tags literally. So x=y, ) and ] never reach the lookup in C, but always do in the pure-Python implementation.

Expected. SyntaxError("invalid path") for every path ElementPath cannot parse, as it already raises for other malformed paths. For example, replace the lookup with ops.get(token[0]) and raise SyntaxError when it is missing.

Whether the C fast path should also send such paths to ElementPath, so that both implementations agree, is a separate choice. It would change results for existing code that looks up literal tags such as "a b" or "a:b", so I am only proposing the SyntaxError change.

I'm happy to open a PR.

CPython versions tested on:

CPython main branch, 3.14, 3.13, 3.12

Operating systems tested on:

Linux

Linked PRs

  • gh-161808: Raise SyntaxError for unsupported ElementPath tokens #161810

Activity

  1. ines-besrour
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Nov 11, 2026
  2. picnixz commented on Nov 11, 2026

    @picnixz
    picnixz
    on Nov 11, 2026
    Member
    More actions

    So the syntax is different depending on the implementation?

  3. picnixz
    added
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    extension-modulesC modules in the Modules dirC modules in the Modules dir
    topic-XML
    on Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirC modules in the Modules dirstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-XMLtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.