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

xml.etree: Avoid XML declaration for the "ascii" encoding #72127

Closed
vstinner opened this issue Sep 2, 2016 · 6 comments
Closed

xml.etree: Avoid XML declaration for the "ascii" encoding #72127

vstinner opened this issue Sep 2, 2016 · 6 comments
Labels
topic-XML type-feature A feature request or enhancement

Comments

@vstinner
Copy link
Member

vstinner commented Sep 2, 2016

BPO 27940
Nosy @scoder, @vstinner, @serhiy-storchaka
Files
  • etree_xml_declaration.patch
  • etree_xml_declaration-2.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 2017-06-28.01:04:05.614>
    created_at = <Date 2016-09-02.10:57:59.411>
    labels = ['expert-XML', 'type-feature']
    title = 'xml.etree: Avoid XML declaration for the "ascii" encoding'
    updated_at = <Date 2017-06-28.01:04:05.613>
    user = 'https://github.com/vstinner'

    bugs.python.org fields:

    activity = <Date 2017-06-28.01:04:05.613>
    actor = 'vstinner'
    assignee = 'none'
    closed = True
    closed_date = <Date 2017-06-28.01:04:05.614>
    closer = 'vstinner'
    components = ['XML']
    creation = <Date 2016-09-02.10:57:59.411>
    creator = 'vstinner'
    dependencies = []
    files = ['44346', '44385']
    hgrepos = []
    issue_num = 27940
    keywords = ['patch']
    message_count = 6.0
    messages = ['274227', '274228', '274231', '274250', '274480', '297098']
    nosy_count = 3.0
    nosy_names = ['scoder', 'vstinner', 'serhiy.storchaka']
    pr_nums = []
    priority = 'normal'
    resolution = 'out of date'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue27940'
    versions = ['Python 3.6']

    @vstinner
    Copy link
    Member Author

    vstinner commented Sep 2, 2016

    The ElementTree module (xml.etree) avoids the XML declaration for "utf-8" and "us-ascii" codecs, but not for the "ascii" encoding.

    Attached patch avoids the XML declaration for the "ascii" codec since it's a subset of UTF-8 and UTF-8 is the default encoding of XML.

    The patch also normalizes the encoding name to handle aliases like "utf8" (UTF-8) or "us_ascii" (ASCII).

    The patch adds unit tests.

    --

    By the way, I'm surprised that the special encoding "unicode" relies on the *current* locale encoding when the XML declaration is requested. Why not alway susing UTF-8 for *unicode* instead of the locale encoding?

    My unit test tests different locale encodings.

    @vstinner vstinner added topic-XML type-feature A feature request or enhancement labels Sep 2, 2016
    @vstinner
    Copy link
    Member Author

    vstinner commented Sep 2, 2016

    Note: I found the "us-ascii" special case when reviewing the issue bpo-27915 which proposed to replace "us-ascii" with "ascii" in the xml.etree module to use the Python fast-path for performance.

    @serhiy-storchaka
    Copy link
    Member

    By the way, I'm surprised that the special encoding "unicode" relies on the *current* locale encoding when the XML declaration is requested. Why not alway susing UTF-8 for *unicode* instead of the locale encoding?

    Because it is usually outputs to sys.stdout or a file opened with default encoding. Agreed, the current locale encoding is not the best choice. It would be better to look at the encoding attribute of the file and fallback to utf-8 or ascii.

    @scoder
    Copy link
    Contributor

    scoder commented Sep 2, 2016

    By the way, I'm surprised that the special encoding "unicode" relies on the *current* locale encoding when the XML declaration is requested.

    That seems a weird choice. Since it serialises to a Unicode string, it shouldn't have any XML declaration at all, if only to make it easy for users to add one themselves if they feel like it.

    I guess it's too late to change that now, though...

    @vstinner
    Copy link
    Member Author

    vstinner commented Sep 6, 2016

    New patch:

    • Avoid codecs.lookup() for method != "xml"
    • More unit tests

    @vstinner
    Copy link
    Member Author

    I lost track of this change. I'm not sure why I proposed it, and I don't feel confortable to change the ElementTree code :-/ I fear a regression, so I prefer to abandon my change.

    @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
    topic-XML type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants