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

Skip to content

Commit eaf399e

Browse files
author
Victor Stinner
committed
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.
1 parent 12b8d14 commit eaf399e

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
@@ -25,6 +25,11 @@ Core and Builtins
2525
Library
2626
-------
2727

28+
- Issue #12451: The XInclude default loader of xml.etree now decodes files from
29+
UTF-8 instead of the locale encoding if the encoding is not specified. It now
30+
also opens XML files for the parser in binary mode instead of the text mode
31+
to avoid encoding issues.
32+
2833
- Issue #12451: doctest.debug_script() doesn't create a temporary file
2934
anymore to avoid encoding issues.
3035

0 commit comments

Comments
 (0)