Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions Doc/library/xml.etree.elementtree.rst
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,11 @@ Functions
not given, the standard :class:`XMLParser` parser is used. Returns an
:class:`ElementTree` instance.

.. note::

When the *source* encoding is ``ISO-8859-1`` and the mode is ``r``,
a warning is emited.


.. function:: ProcessingInstruction(target, text=None)

Expand Down
7 changes: 7 additions & 0 deletions Lib/test/test_xml_etree.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,13 @@ def test_makeelement(self):
elem[:] = tuple([subelem])
self.serialize_check(elem, '<tag><subtag key="value" /></tag>')

def test_parse_encoding_warn(self):
with self.assertWarns(RuntimeWarning) as cm:
with open(SIMPLE_XMLFILE, 'r', encoding='ISO-8859-1') as fp:
ET.parse(fp)
self.assertIn("ISO-8859-1 encoding should be read using 'rb' mode.",
str(cm.warnings[0].message))

def test_parsefile(self):
# Test parsing from file.

Expand Down
3 changes: 3 additions & 0 deletions Lib/xml/etree/ElementTree.py
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,9 @@ def parse(source, parser=None):
Return an ElementTree instance.

"""
if getattr(source, 'encoding', None) == 'ISO-8859-1' and source.mode == 'r':
import warnings
warnings.warn("ISO-8859-1 encoding should be read using 'rb' mode.", category=RuntimeWarning)
tree = ElementTree()
tree.parse(source, parser)
return tree
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Added a warning in the :func:`xml.etree.ElementTree.parse` for
files opened in ``r`` mode with ``'ISO-8859-1'`` encoding. Patch by RUANG.
Loading