Skip to content

gh-127550 Fix ElementTree write newline handling#148975

Closed
durawat wants to merge 1 commit intopython:mainfrom
durawat:elementtree-newline
Closed

gh-127550 Fix ElementTree write newline handling#148975
durawat wants to merge 1 commit intopython:mainfrom
durawat:elementtree-newline

Conversation

@durawat
Copy link
Copy Markdown

@durawat durawat commented Apr 25, 2026

gh-127550: Fix ElementTree.write to ensure newline consistency between standalone and context manager use

Summary

Ensure consistent newline handling in ElementTree.write() when writing
to a filename versus a file-like object.

Relevant code in Lib/xml/etree/ElementTree.py

Function: _get_writer(file_or_filename, encoding)

Removing newline="\n" from the first code block caused multiple test
failures in Lib/test/test_xml_etree.py. In contrast, adding
newline="\n" to the second block resulted in only one failing test.

Writer for file-like objects

file = io.TextIOWrapper(
    file,
    encoding=encoding,
    errors="xmlcharrefreplace",
    newline="\n",
)

Writer for file or filename

   with open(file_or_filename, "w", encoding=encoding,
                  errors="xmlcharrefreplace", newline="\n") as file:

Changes

Added newline="\n" to the open() call for filename-based writing,
instead of removing it from the TextIOWrapper path, to ensure consistent
behavior between file-like objects and filenames.

This change improves consistency between the two code paths.

Rationale

According to the XML specification, XML parsers normalize line endings.
The sequences \r\n, \r, and \n are all normalized to \n during parsing.
Therefore, differences in newline handling at write time do not affect
parsed XML content, but consistency in output behavior is desirable.

Tests

  • Fixed one failing test case in Lib/test/test_xml_etree.py
  • Added a new regression test in Lib/test/test_xml_etree.py

@python-cla-bot
Copy link
Copy Markdown

python-cla-bot Bot commented Apr 25, 2026

The following commit authors need to sign the Contributor License Agreement:

CLA not signed

@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented Apr 25, 2026

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@durawat durawat force-pushed the elementtree-newline branch from 0d0bc0d to 2ed04f0 Compare April 25, 2026 02:10
@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented Apr 25, 2026

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@picnixz
Copy link
Copy Markdown
Member

picnixz commented Apr 25, 2026

The existing tests seem to indicate that this is a deliberate behavior. I am closing this PR because we need consensus first. Please don't open PRs in the future if no consensus is reached. In addition, don't use LLMs as we forbid them (read https://devguide.python.org/getting-started/generative-ai/ for acceptable uses)

@picnixz picnixz closed this Apr 25, 2026
@durawat durawat deleted the elementtree-newline branch April 25, 2026 11:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants