Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-91810: Fix regression with writing an XML declaration with encoding='unicode'#93426

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
PrevPrevious commit
Always open files in UTF-8 with encoding='unicode'.
  • Loading branch information
@serhiy-storchaka
serhiy-storchaka committedJun 12, 2022
commit46ab6e0f636e8f1aa3dca4a32f0b2bdc1e5eaade
8 changes: 1 addition & 7 deletionsLib/test/test_xml_etree.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3739,13 +3739,7 @@ def test_write_to_filename_as_unicode(self):
tree = ET.ElementTree(ET.XML('''<site>\xf8</site>'''))
tree.write(TESTFN, encoding='unicode')
with open(TESTFN, 'rb') as f:
data = f.read()
expected = "<site>\xf8</site>".encode(encoding, 'xmlcharrefreplace')
if encoding.lower() in ('utf-8', 'ascii'):
self.assertEqual(data, expected)
else:
self.assertIn(b"<?xml version='1.0' encoding=", data)
self.assertIn(expected, data)
self.assertEqual(f.read(), b"<site>\xc3\xb8</site>")

def test_write_to_text_file(self):
self.addCleanup(os_helper.unlink, TESTFN)
Expand Down
14 changes: 5 additions & 9 deletionsLib/xml/etree/ElementTree.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -731,8 +731,7 @@ def write(self, file_or_filename,
with _get_writer(file_or_filename, encoding) as (write, declared_encoding):
if method == "xml" and (xml_declaration or
(xml_declaration is None and
not (encoding.lower() == "unicode" and
hasattr(file_or_filename, "write")) and
encoding.lower() != "unicode" and
declared_encoding.lower() not in ("utf-8", "us-ascii"))):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Why not just this?

xml_declaration is None and encoding.lower() not in ("utf-8", "us-ascii", "unicode")

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Because it will write an invalid XML file when pass a file name on non-UTF-8 locale. Runtest_write_to_filename_as_unicode on non-UTF-8 locale.

When the default encoding inopen() be UTF-8, and only UTF-8, and nothing except UTF-8, this code could be simplified.

It can even be done earlier if open the file in UTF-8 instead of the default encoding withencoding='unicode'. But such change is not for backporting. I'll create a separate PR for the main branch only.

write("<?xml version='1.0' encoding='%s'?>\n" % (
declared_encoding,))
Expand All@@ -759,13 +758,10 @@ def _get_writer(file_or_filename, encoding):
except AttributeError:
# file_or_filename is a file name
if encoding.lower() == "unicode":
file = open(file_or_filename, "w",
errors="xmlcharrefreplace")
else:
file = open(file_or_filename, "w", encoding=encoding,
errors="xmlcharrefreplace")
with file:
yield file.write, file.encoding
encoding="utf-8"
with open(file_or_filename, "w", encoding=encoding,
errors="xmlcharrefreplace") as file:
yield file.write, encoding
else:
# file_or_filename is a file-like object
# encoding determines if it is a text or binary writer
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp