Skip to content

xml Python and C version ParseError difference #158166

Description

@alexprengere

Bug report

Bug description:

The ParseError class from the xml module does not cast the msg attribute to str when running on the pure-Python version, here is a MRE:

import xml.etree.ElementTree as etree
try:
    etree.XML("<")
except etree.ParseError as e:
    print("XMLParser:", etree.XMLParser)
    print("args:", e.args)
    print("msg:", repr(e.msg))
    print("msg type:", type(e.msg))

On regular CPython using the C-accelerator:

XMLParser: <class 'xml.etree.ElementTree.XMLParser'>
args: ('unclosed token: line 1, column 0',)
msg: 'unclosed token: line 1, column 0'
msg type: <class 'str'>

If you force the execution of the Python module:

XMLParser: <class 'xml.etree.ElementTree.XMLParser'>
args: (ExpatError('unclosed token: line 1, column 0'),)
msg: ExpatError('unclosed token: line 1, column 0')
msg type: <class 'pyexpat.ExpatError'>

The issue is this function:

    def _raiseerror(self, value):
        err = ParseError(value)
        err.code = value.code
        err.position = value.lineno, value.offset
        raise err

At all call sites, value is an exception, so effectively the first argument is always an exception. Note that ParseError is a sub-class of SyntaxError.
In the C-version, the first agument is always cast to a str.
I encountered this bug through PyPy that use the Python version of lxml.

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs

Activity

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

    stdlibStandard Library Python modules in the Lib/ directorytopic-XMLtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions