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: Add 'sourceline' property to xml.etree Elements
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.3
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: eli.bendersky, leonov
Priority: normal Keywords: needs review, patch

Created on 2012-02-21 22:25 by leonov, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
elementtree-sourceline.diff leonov, 2012-02-21 22:25 Initial, proof of concept patch review
Messages (3)
msg153907 - (view) Author: Leon Matthews (leonov) Date: 2012-02-21 22:25
The lxml implementation of the ElementTree API puts a `sourceline` property onto every Element object, which I recently found useful when producing diagnostic messages.  I think it would be a useful improvement to make the standard library's ElementTree implementation.

The attached patch works by copying the current line number from the Expat parser into the Element object after the Element object is created (so as to minimise its intrusiveness for now).

The patch is just a proof of concept, and although all tests pass, the patch currently smells a little hacky and fragile to me.  Hopefully though, it will start a discussion with somebody more experienced.

PS. So as not to create a hard dependency on lxml.etree, in my project I worked around the issue as follows.  While this works in my case, the standard library seems a more logical place for this change::

    class XMLParserWithLines(XMLParser):
        """
        Add a `sourceline` attribute to element, like lxml.etree
        """
        def _start_list(self, *args, **kwargs):
            element = super(self.__class__, self)._start_list(*args, **kwargs)
        element.sourceline = self._parser.CurrentLineNumber
        return element

    
    >>> tree = ElementTree()
    >>> tree.parse(path, parser=XMLParserWithLines())
    >>> ...
msg156558 - (view) Author: Eli Bendersky (eli.bendersky) * (Python committer) Date: 2012-03-22 13:34
Hi Leon,
Thanks for the patch.

I suggest to start by raising this to the python-ideas mailing list, to see if anyone has objections / different idea about doing this.

Next, keep in mind that starting with 3.3, the default ElementTree implementation comes from the C accelerator Modules/_elementtree.c, and compatibility between the Python and C implementations becomes important. Therefore, each such change has to be added to both implementations. Is your patch done in a common section, or just the Python implementation?
msg161761 - (view) Author: Eli Bendersky (eli.bendersky) * (Python committer) Date: 2012-05-28 08:31
Hi Leon,
Do you have an interest in pursuing this issue?
History
Date User Action Args
2022-04-11 14:57:27adminsetgithub: 58286
2012-06-08 12:32:29eli.benderskysetstatus: open -> closed
resolution: wont fix
stage: patch review -> resolved
2012-05-28 08:31:50eli.benderskysetmessages: + msg161761
2012-03-22 13:34:51eli.benderskysetmessages: + msg156558
2012-03-22 12:29:46r.david.murraysetkeywords: + needs review
type: enhancement
stage: patch review
2012-03-21 19:16:42ned.deilysetnosy: + eli.bendersky
2012-02-21 22:25:31leonovcreate