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.

Author leonov
Recipients leonov
Date 2012-02-21.22:25:30
SpamBayes Score 4.174785e-09
Marked as misclassified No
Message-id <1329863134.79.0.699697784351.issue14078@psf.upfronthosting.co.za>
In-reply-to
Content
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())
    >>> ...
History
Date User Action Args
2012-02-21 22:25:34leonovsetrecipients: + leonov
2012-02-21 22:25:34leonovsetmessageid: <1329863134.79.0.699697784351.issue14078@psf.upfronthosting.co.za>
2012-02-21 22:25:31leonovlinkissue14078 messages
2012-02-21 22:25:31leonovcreate