Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3909da7

Browse files
author
Victor Stinner
committed
(merge 3.2) Issue #12451: The XInclude default loader of xml.etree now decodes
files from UTF-8 instead of the locale encoding if the encoding is not specified. It now also opens XML files for the parser in binary mode instead of the text mode to avoid encoding issues.
2 parents a0b12a1 + eaf399e commit 3909da7

2 files changed

Lines changed: 10 additions & 4 deletions

File tree

Lib/xml/etree/ElementInclude.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,22 @@ class FatalIncludeError(SyntaxError):
6767
#
6868
# @param href Resource reference.
6969
# @param parse Parse mode. Either "xml" or "text".
70-
# @param encoding Optional text encoding.
70+
# @param encoding Optional text encoding (UTF-8 by default for "text").
7171
# @return The expanded resource. If the parse mode is "xml", this
7272
# is an ElementTree instance. If the parse mode is "text", this
7373
# is a Unicode string. If the loader fails, it can return None
7474
# or raise an IOError exception.
7575
# @throws IOError If the loader fails to load the resource.
7676

7777
def default_loader(href, parse, encoding=None):
78-
file = open(href)
7978
if parse == "xml":
79+
file = open(href, 'rb')
8080
data = ElementTree.parse(file).getroot()
8181
else:
82+
if not encoding:
83+
encoding = 'UTF-8'
84+
file = open(href, 'r', encoding=encoding)
8285
data = file.read()
83-
if encoding:
84-
data = data.decode(encoding)
8586
file.close()
8687
return data
8788

Misc/NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,11 @@ Core and Builtins
200200
Library
201201
-------
202202

203+
- Issue #12451: The XInclude default loader of xml.etree now decodes files from
204+
UTF-8 instead of the locale encoding if the encoding is not specified. It now
205+
also opens XML files for the parser in binary mode instead of the text mode
206+
to avoid encoding issues.
207+
203208
- Issue #12451: doctest.debug_script() doesn't create a temporary file
204209
anymore to avoid encoding issues.
205210

0 commit comments

Comments
 (0)