GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — ElementTree should use UTF-8 for xml declaration. · Issue #91810 · python/cpython

ElementTree should use UTF-8 for xml declaration. #91810

New issue
Copy link
New issue
Copy link
Closed
#91903
Closed
ElementTree should use UTF-8 for xml declaration.#91810
#91903
Copy link
Labels
topic-XMLtype-featureA feature request or enhancementA feature request or enhancement

Description

@methane
methane
opened on Apr 22, 2022
Issue body actions

Feature or enhancement

Currently, ElementTree.tostring(root, encoding="unicode", xml_declaration=True) uses locale encoding.

I think ElementTree should use UTF-8, instead of locale encoding.

Example:

$ LANG=ja_JP.eucJP ./python.exe
Python 3.11.0a7+ (heads/bytes-alloc-dirty:7fbc7f6128, Apr 19 2022, 16:53:54) [Clang 12.0.0 (clang-1200.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import xml.etree.ElementTree as ET
>>> et = ET.fromstring("<t>hello</t>")
>>> ET.tostring(et, encoding="unicode", xml_declaration=True)
"<?xml version='1.0' encoding='eucJP'?>\n<t>hello</t>"

Code:

cpython/Lib/xml/etree/ElementTree.py

Lines 732 to 742 in bcf14ae

with _get_writer(file_or_filename, enc_lower) as write:
if method == "xml" and (xml_declaration or
(xml_declaration is None and
enc_lower not in ("utf-8", "us-ascii", "unicode"))):
declared_encoding = encoding
if enc_lower == "unicode":
# Retrieve the default encoding for the xml declaration
import locale
declared_encoding = locale.getpreferredencoding()
write("<?xml version='1.0' encoding='%s'?>\n" % (
declared_encoding,))

Pitch

  • UTF-8 is the most common encoding for XML.
  • Locale encoding name (e.g. cp932 or eucJP) would be different from XML encoding name recommended by w3c (e.g. Shift_JIS or EUC-JP).

Activity

Next
  1. methane
    added
    type-featureA feature request or enhancementA feature request or enhancement
    topic-XML
    on Apr 22, 2022
  2. serhiy-storchaka commented on Apr 22, 2022

    @serhiy-storchaka
    serhiy-storchaka
    on Apr 22, 2022
    Member
    More actions

    Look at dump(). It writes an element to stdout, which usually uses the locale encoding. I think it is the rationale of using the locale encoding here. You need to change dump() to use the stdout's encoding explicitly.

  3. serhiy-storchaka commented on Apr 22, 2022

    @serhiy-storchaka
    serhiy-storchaka
    on Apr 22, 2022
    Member
    More actions

    Or maybe change write() to get the encoding from the output string if available.

  4. methane commented on Apr 23, 2022

    @methane
    methane
    on Apr 23, 2022
    MemberAuthor
    More actions

    dump don't use xml_declaration=True. So this issue doesn't affect it.

  5. methane commented on Apr 25, 2022

    @methane
    methane
    on Apr 25, 2022
    MemberAuthor
    More actions

    @scoder would you give us an advice? (you are listed as etree expert in expert index).

    There is no correct behavior, because output is Unicode and etree don't know what is real output encoding.

    There are some cases that current behavior is better (e.g. using default encoding (e.g. open(filename, 'w')).
    On the other hand, encoding="cp932" (in Japanese Windows) is non-portable (encoding="Shift_JIS" should be used), and UTF-8 is the most recommended encoding for XML.

    I have two ideas:

    a. Make UTF-8 default. This is simplest.
    b. Keep using locale.getpreferredencoding() and wait PEP 686 accepted. (But it should be replaced with locale.getpreferredencoding(False) anyway.)

  6. serhiy-storchaka commented on Apr 25, 2022

    @serhiy-storchaka
    serhiy-storchaka
    on Apr 25, 2022
    Member
    More actions

    Adding encoding="UTF-8" and using cp932 to encode the content would be even worse.

    Maybe add a simple mapping from Python encodings to XML encodings (for example we need to write "ascii" as "us-ascii")? Later we can discuss adding a public API for this.

  7. serhiy-storchaka commented on Apr 25, 2022

    @serhiy-storchaka
    serhiy-storchaka
    on Apr 25, 2022
    Member
    More actions

    I proposed to get the default encoding from the file object if available. #91812 (comment)

  8. methane commented on Apr 25, 2022

    @methane
    methane
    on Apr 25, 2022
    MemberAuthor
    More actions

    Adding encoding="UTF-8" and using cp932 to encode the content would be even worse.

    Of course, we should recommend to use UTF-8.
    Note that encoding='cp932' and using UTF-8 is possible bug for now already.
    Any default value may cause bug. There is no one correct default. But UTF-8 may be the best for now.

    Maybe add a simple mapping from Python encodings to XML encodings (for example we need to write "ascii" as "us-ascii")? Later we can discuss adding a public API for this.

    We may not know Python encoding because output is Unicode (e.g. Unicode string or StringIO).
    Such idea works only when output is TextIOWrapper. (And there are no guarantee that TextIOWrapper.encoding is really the final encoding.)

    If we want to support arbitrary encoding, we should add another option like xml_declaration_encoding="Shift_JIS".
    But this is not strict necessary.
    User can chose xml_declaration=False and prepend <?xml version="1.0" encoding="Shift_JIS" ?> manually when they really need to use encoding other than UTF-8.

  9. serhiy-storchaka
    added a commit that references this issue on Apr 25, 2022

    pythongh-91810: ElementTree: Use text file's encoding by default in X…

    ca0f8e9
  10. serhiy-storchaka commented on Apr 25, 2022

    @serhiy-storchaka
    serhiy-storchaka
    on Apr 25, 2022
    Member
    More actions

    Note that encoding='cp932' and using UTF-8 is possible bug for now already.

    Yes, it is a bug, and #91903 fixes it.

  11. serhiy-storchaka
    added 2 commits that reference this issue on Apr 27, 2022

    pythongh-91810: Expand ElementTree.write() tests to use non-ASCII data

    68008bd

    gh-91810: Expand ElementTree.write() tests to use non-ASCII data (GH-…

    f60b4c3
  12. miss-islington
    added a commit that references this issue on Apr 27, 2022

    pythongh-91810: Expand ElementTree.write() tests to use non-ASCII data (

    74fec20
  13. serhiy-storchaka
    added a commit that references this issue on Apr 27, 2022

    [3.9] pythongh-91810: Expand ElementTree.write() tests to use non-ASC…

    ece063d
  14. 8 remaining items

  15. serhiy-storchaka
    added a commit that references this issue on May 11, 2022

    gh-91810: ElementTree: Use text file's encoding by default in XML dec…

    707839b
  16. miss-islington
    added 3 commits that reference this issue on May 11, 2022

    [3.11] gh-91810: ElementTree: Use text file's encoding by default in …

    20fec2c

    [3.10] gh-91810: ElementTree: Use text file's encoding by default in …

    4730b0d

    [3.9] gh-91810: ElementTree: Use text file's encoding by default in X…

    bfc88d3
  17. serhiy-storchaka
    added a commit that references this issue on Jun 2, 2022

    pythongh-91810: Fix regression with writing an XML declaration with e…

    f73b7cb
  18. hello-adam
    added 2 commits that reference this issue on Jun 2, 2022

    [3.9] pythongh-91810: Expand ElementTree.write() tests to use non-ASC…

    8d2b4c0

    [3.9] pythongh-91810: ElementTree: Use text file's encoding by defaul…

    b4d15d5
  19. serhiy-storchaka
    added a commit that references this issue on Jun 14, 2022

    gh-91810: Fix regression with writing an XML declaration with encodin…

    d7db9dc
  20. miss-islington
    added 3 commits that reference this issue on Jun 14, 2022

    pythongh-91810: Fix regression with writing an XML declaration with e…

    1392468

    pythongh-91810: Fix regression with writing an XML declaration with e…

    73edebc

    pythongh-91810: Fix regression with writing an XML declaration with e…

    8ff7c23
  21. ambv
    added 2 commits that reference this issue on Jun 16, 2022

    gh-91810: Fix regression with writing an XML declaration with encodin…

    893adbf

    gh-91810: Fix regression with writing an XML declaration with encodin…

    a27f3cc
  22. serhiy-storchaka
    added a commit that references this issue on Jun 26, 2022

    Revert 30935 defer bpo45162 to 312 (#94285)

    1ffde3e
  23. ambv
    added a commit that references this issue on Jul 26, 2022

    gh-91810: Fix regression with writing an XML declaration with encodin…

    6fe7e02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic-XMLtype-featureA feature request or enhancementA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.