This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: ElementTree.ParseError does not implement SyntaxError interface as expected
Type: Stage:
Components: Library (Lib) Versions: Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: jamercee, scoder
Priority: normal Keywords:

Created on 2019-10-28 09:11 by jamercee, last changed 2022-04-11 14:59 by admin.

Messages (2)
msg355518 - (view) Author: Jim Carroll (jamercee) * Date: 2019-10-28 09:11
_elementtree.c defines a custom exception 'xml.etree.ElementTree.ParseError' that inherits from SyntaxError. According to the docs https://docs.python.org/3/library/exceptions.html#SyntaxError

``Instances of this class have attributes filename, lineno, offset and text for easier access to the details.``

The ElementTree.Parser does not provide these extra attributes. While unittesting a badly formatted string passed to ET.fromstring(), the unittests/results.py module passes the exception to traceback.py, which attempts to extract the members causing it to raise a new exception.

Either ElementTree.Parser should be raising something other than SyntaxErrors, or it should be providing the required attributes, or traceback.py needs to be more flexible and test for the extra attributes before accessing.

I'm willing to submit a patch, but before diving in, wasn't sure which path people would prefer to see fixed?

The code in traceback.py is from lines 516 - 521.
msg355686 - (view) Author: Jim Carroll (jamercee) * Date: 2019-10-29 22:46
This patch solves the issue

diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c
index c3f30c9339..d265021f75 100644
--- a/Modules/_elementtree.c
+++ b/Modules/_elementtree.c
@@ -2782,6 +2782,7 @@ treebuilder_handle_start(TreeBuilderObject* self, PyObject* tag,
                 st->parseerror_obj,
                 "multiple elements on top level"
                 );
+                       PyErr_SyntaxLocationEx("xml.etree.ElementTree", 0, 0);
             goto error;
         }
         Py_INCREF(node);
@@ -3267,6 +3268,7 @@ expat_set_error(enum XML_Error error_code, Py_ssize_t line, Py_ssize_t column,
     Py_DECREF(position);

     PyErr_SetObject(st->parseerror_obj, error);
+       PyErr_SyntaxLocationEx("xml.etree.ElementTree", (int)line, (int)column);
     Py_DECREF(error);
 }
History
Date User Action Args
2022-04-11 14:59:22adminsetgithub: 82792
2019-10-30 02:12:26xtreaksetnosy: + scoder
2019-10-29 22:46:43jamerceesetmessages: + msg355686
2019-10-28 17:17:46brett.cannonsettitle: Bug in traceback.py -> ElementTree.ParseError does not implement SyntaxError interface as expected
2019-10-28 09:11:14jamerceecreate