Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'sourceline' property to xml.etree Elements #58286

Closed
leonov mannequin opened this issue Feb 21, 2012 · 3 comments
Closed

Add 'sourceline' property to xml.etree Elements #58286

leonov mannequin opened this issue Feb 21, 2012 · 3 comments
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@leonov
Copy link
Mannequin

leonov mannequin commented Feb 21, 2012

BPO 14078
Files
  • elementtree-sourceline.diff: Initial, proof of concept patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2012-06-08.12:32:29.666>
    created_at = <Date 2012-02-21.22:25:31.934>
    labels = ['type-feature', 'library']
    title = "Add 'sourceline' property to xml.etree Elements"
    updated_at = <Date 2012-06-08.12:32:29.666>
    user = 'https://bugs.python.org/leonov'

    bugs.python.org fields:

    activity = <Date 2012-06-08.12:32:29.666>
    actor = 'eli.bendersky'
    assignee = 'none'
    closed = True
    closed_date = <Date 2012-06-08.12:32:29.666>
    closer = 'eli.bendersky'
    components = ['Library (Lib)']
    creation = <Date 2012-02-21.22:25:31.934>
    creator = 'leonov'
    dependencies = []
    files = ['24594']
    hgrepos = []
    issue_num = 14078
    keywords = ['patch', 'needs review']
    message_count = 3.0
    messages = ['153907', '156558', '161761']
    nosy_count = 2.0
    nosy_names = ['eli.bendersky', 'leonov']
    pr_nums = []
    priority = 'normal'
    resolution = 'wont fix'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue14078'
    versions = ['Python 3.3']

    @leonov
    Copy link
    Mannequin Author

    leonov mannequin commented Feb 21, 2012

    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())
    >>> ...
    

    @leonov leonov mannequin added the stdlib Python modules in the Lib dir label Feb 21, 2012
    @bitdancer bitdancer added the type-feature A feature request or enhancement label Mar 22, 2012
    @elibendersky
    Copy link
    Mannequin

    elibendersky mannequin commented Mar 22, 2012

    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?

    @elibendersky
    Copy link
    Mannequin

    elibendersky mannequin commented May 28, 2012

    Hi Leon,
    Do you have an interest in pursuing this issue?

    @elibendersky elibendersky mannequin closed this as completed Jun 8, 2012
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant