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 — gh-91810: ElementTree: Use text file's encoding by default in XML declaration by serhiy-storchaka · Pull Request #91903 · python/cpython

Conversation

@serhiy-storchaka

@serhiy-storchaka serhiy-storchaka commented Apr 25, 2022

Copy link
Copy Markdown
Member

ElementTree method write() and function tostring() now use the text file's
encoding ("UTF-8" if not available) instead of locale encoding in XML
declaration when encoding="unicode" is specified.

Closes #91810.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@serhiy-storchaka
pythongh-91810: ElementTree: Use text file's encoding by default in X…
ca0f8e9
…ML declaration

ElementTree method write() and function tostring() now use the text file's
encoding ("UTF-8" if not available) instead of locale encoding in XML
declaration when encoding="unicode" is specified.
@serhiy-storchaka
serhiy-storchaka requested a review from methane April 25, 2022 07:29
@bedevere-bot bedevere-bot added the awaiting core review label Apr 25, 2022
@serhiy-storchaka serhiy-storchaka added type-bug An unexpected behavior, bug, or error needs backport to 3.9 labels Apr 25, 2022
@AlexWaygood AlexWaygood added the topic-XML label Apr 25, 2022
serhiy-storchaka added 3 commits April 27, 2022 12:06
@serhiy-storchaka
Merge branch 'main' into etree-write-default-encoding
f4cacec
@serhiy-storchaka
Fix tests on Windows.
9d21549
@serhiy-storchaka
Merge branch 'main' into etree-write-default-encoding
23fbea8
@serhiy-storchaka serhiy-storchaka mentioned this pull request Apr 29, 2022
ElementTree should use UTF-8 for xml declaration. #91810
Closed
methane
methane approved these changes May 11, 2022
View reviewed changes
@bedevere-bot bedevere-bot added awaiting merge and removed awaiting core review labels May 11, 2022
@serhiy-storchaka serhiy-storchaka added the needs backport to 3.11 only security fixes label May 11, 2022
@serhiy-storchaka
serhiy-storchaka merged commit 707839b into python:main May 11, 2022
@miss-islington

miss-islington commented May 11, 2022

Copy link
Copy Markdown
Contributor

Thanks @serhiy-storchaka for the PR 🌮🎉.. I'm working now to backport this PR to: 3.9, 3.10, 3.11.
🐍🍒⛏🤖

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot removed the awaiting merge label May 11, 2022
@serhiy-storchaka
serhiy-storchaka deleted the etree-write-default-encoding branch May 11, 2022 06:31
@bedevere-bot bedevere-bot removed the needs backport to 3.11 only security fixes label May 11, 2022
@bedevere-bot

bedevere-bot commented May 11, 2022

Copy link
Copy Markdown

GH-92663 is a backport of this pull request to the 3.11 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot

bedevere-bot commented May 11, 2022

Copy link
Copy Markdown

GH-92664 is a backport of this pull request to the 3.10 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot removed needs backport to 3.10 labels May 11, 2022
@bedevere-bot

bedevere-bot commented May 11, 2022

Copy link
Copy Markdown

GH-92665 is a backport of this pull request to the 3.9 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request May 11, 2022
@serhiy-storchaka @miss-islington
pythongh-91810: ElementTree: Use text file's encoding by default in X…
8c8f49a
…ML declaration (pythonGH-91903)

ElementTree method write() and function tostring() now use the text file's
encoding ("UTF-8" if not available) instead of locale encoding in XML
declaration when encoding="unicode" is specified.
(cherry picked from commit 707839b)

Co-authored-by: Serhiy Storchaka <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request May 11, 2022
@serhiy-storchaka @miss-islington
pythongh-91810: ElementTree: Use text file's encoding by default in X…
d44991b
…ML declaration (pythonGH-91903)

ElementTree method write() and function tostring() now use the text file's
encoding ("UTF-8" if not available) instead of locale encoding in XML
declaration when encoding="unicode" is specified.
(cherry picked from commit 707839b)

Co-authored-by: Serhiy Storchaka <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request May 11, 2022
@serhiy-storchaka @miss-islington
pythongh-91810: ElementTree: Use text file's encoding by default in X…
b3e75da
…ML declaration (pythonGH-91903)

ElementTree method write() and function tostring() now use the text file's
encoding ("UTF-8" if not available) instead of locale encoding in XML
declaration when encoding="unicode" is specified.
(cherry picked from commit 707839b)

Co-authored-by: Serhiy Storchaka <[email protected]>
miss-islington added a commit that referenced this pull request May 11, 2022
@miss-islington
[3.11] gh-91810: ElementTree: Use text file's encoding by default in …
20fec2c
…XML declaration (GH-91903) (GH-92663)

ElementTree method write() and function tostring() now use the text file's
encoding ("UTF-8" if not available) instead of locale encoding in XML
declaration when encoding="unicode" is specified.
(cherry picked from commit 707839b)


Co-authored-by: Serhiy Storchaka <[email protected]>

Automerge-Triggered-By: GH:serhiy-storchaka
miss-islington added a commit that referenced this pull request May 11, 2022
@miss-islington
[3.10] gh-91810: ElementTree: Use text file's encoding by default in …
4730b0d
…XML declaration (GH-91903) (GH-92664)

ElementTree method write() and function tostring() now use the text file's
encoding ("UTF-8" if not available) instead of locale encoding in XML
declaration when encoding="unicode" is specified.
(cherry picked from commit 707839b)


Co-authored-by: Serhiy Storchaka <[email protected]>

Automerge-Triggered-By: GH:serhiy-storchaka
miss-islington added a commit that referenced this pull request May 11, 2022
@miss-islington
[3.9] gh-91810: ElementTree: Use text file's encoding by default in X…
bfc88d3
…ML declaration (GH-91903) (GH-92665)

ElementTree method write() and function tostring() now use the text file's
encoding ("UTF-8" if not available) instead of locale encoding in XML
declaration when encoding="unicode" is specified.
(cherry picked from commit 707839b)


Co-authored-by: Serhiy Storchaka <[email protected]>

Automerge-Triggered-By: GH:serhiy-storchaka
@Yhg1s

Yhg1s commented Jun 1, 2022

Copy link
Copy Markdown
Member

FYI, @pablogsal @ambv: this change broke existing behaviour in a subtle way. I'm not sure if it's worth rolling back in 3.9, especially considering it went into the final regular bugfix release. It may be worth fixing the regression in 3.10 or 3.11, or it may be deemed to be a documentation issue.

The documentation of ElementTree.write() says: xml_declaration controls if an XML declaration should be added to the file. Use False for never, True for always, None for only if not US-ASCII or UTF-8 or Unicode (default is None).

Before this change, passing a file opened in text mode regardless of the encoding used and passing encoding='unicode' to ElementTree.write() would not make ElementTree.write() produce the XML declaration.

After this change, passing a file opened in text mode with UTF-8 or ascii as encoding, and passing encoding='unicode' to ElementTree.write(), no XML declaration is produced (as before). However, passing a file opened in any other encoding (e.g. iso-8859-1) implicitly or explicitly, with encoding='unicode' passed to ElementTree.write() now does produce an XML declaration. This may come as a surprise to code that was manually adding an XML declaration before. For example:

from xml.etree import ElementTree as et

XML_HEADER = """<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE myelement>
"""

root = et.Element("myelement")
tree = et.ElementTree(root)

with open("myfile.xml", "w", encoding="iso-8859-1") as f:
    f.write(XML_HEADER)
    tree.write(f, encoding='unicode')
with open("myfile.xml", encoding="iso-8859-1") as f:
    print(f.read())

... will produce invalid XML because of this change:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE myelement>
<?xml version='1.0' encoding='iso-8859-1'?>
<myelement />

... where before this change it would not emit the second XML declaration. (This is arguably problematic code, but unfortunately the file's encoding can of course be implicit, which makes it harder to tell what's going on. This was synthesized from real code found while upgrading to 3.9.13 at Google.)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@pablogsal

pablogsal commented Jun 1, 2022

Copy link
Copy Markdown
Member

I am a bit worried that for 3.10 people have already developed workarounds for this and we are going to break them again. On the other hand, one could argue that the situation is too specific and that's why nobody has raised issues so far. I am ok rolling it back, though, if that's the consensus.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Yhg1s

Yhg1s commented Jun 1, 2022

Copy link
Copy Markdown
Member

Actually, this change didn't make it into 3.10.4, so it's only in 3.9.13 and 3.11 at this point.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka

serhiy-storchaka commented Jun 2, 2022

Copy link
Copy Markdown
Member Author

#93426 fixes this.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

hello-adam pushed a commit to hello-adam/cpython that referenced this pull request Jun 2, 2022
@miss-islington @hello-adam
[3.9] pythongh-91810: ElementTree: Use text file's encoding by defaul…
b4d15d5
…t in XML declaration (pythonGH-91903) (pythonGH-92665)

ElementTree method write() and function tostring() now use the text file's
encoding ("UTF-8" if not available) instead of locale encoding in XML
declaration when encoding="unicode" is specified.
(cherry picked from commit 707839b)


Co-authored-by: Serhiy Storchaka <[email protected]>

Automerge-Triggered-By: GH:serhiy-storchaka
@MarkusPiotrowski MarkusPiotrowski mentioned this pull request Jun 15, 2022
CI issues @github-actions CI / test_windows (3.9) (pull_request) Failing after 5m — test_windows (3.9) biopython/biopython#3944
Open
@ambv

ambv commented Jun 16, 2022

Copy link
Copy Markdown
Contributor

I think we should fix it in 3.9.14 as well since it's a regression only in 3.9.13.

serhiy-storchaka reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ambv

ambv commented Jun 16, 2022

Copy link
Copy Markdown
Contributor

(In the mean time we did release 3.10.5 with the bug which made this version affected too)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

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

Reviewers

@methane methane methane approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

topic-XML type-bug An unexpected behavior, bug, or error

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

ElementTree should use UTF-8 for xml declaration.

Uh oh!

There was an error while loading. Please reload this page.

8 participants

@serhiy-storchaka @miss-islington @bedevere-bot @Yhg1s @pablogsal @ambv @methane @AlexWaygood
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.