From 3022ee8ff7be8a1f5f64b267faefc09776a48539 Mon Sep 17 00:00:00 2001 From: Diego Date: Mon, 29 Oct 2018 20:03:14 -0500 Subject: [PATCH 1/3] Cherry Pick 1e8933d518f089b490fb2d3018e967f62378241a --- Doc/library/xml.dom.minidom.rst | 11 +++++++++++ Lib/test/test_minidom.py | 25 +++++++++++++++++++++++++ Lib/xml/dom/minidom.py | 3 +-- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/Doc/library/xml.dom.minidom.rst b/Doc/library/xml.dom.minidom.rst index 40470e8736e7e4..a1f334d4178e43 100644 --- a/Doc/library/xml.dom.minidom.rst +++ b/Doc/library/xml.dom.minidom.rst @@ -143,6 +143,9 @@ module documentation. This section lists the differences between the API and For the :class:`Document` node, an additional keyword argument *encoding* can be used to specify the encoding field of the XML header. + .. versionchanged:: 3.8 + The :meth:`writexml` method now preserves the attribute order specified + by the user. .. method:: Node.toxml(encoding=None) @@ -156,6 +159,10 @@ module documentation. This section lists the differences between the API and encoding. Encoding this string in an encoding other than UTF-8 is likely incorrect, since UTF-8 is the default encoding of XML. + .. versionchanged:: 3.8 + The :meth:`toxml` method now preserves the attribute order specified + by the user. + .. method:: Node.toprettyxml(indent="", newl="", encoding="") Return a pretty-printed version of the document. *indent* specifies the @@ -165,6 +172,10 @@ module documentation. This section lists the differences between the API and The *encoding* argument behaves like the corresponding argument of :meth:`toxml`. + .. versionchanged:: 3.8 + The :meth:`toprettyxml` method now preserves the attribute order specified + by the user. + .. _dom-example: diff --git a/Lib/test/test_minidom.py b/Lib/test/test_minidom.py index e91cdba1eb2eff..d759118d559fa9 100644 --- a/Lib/test/test_minidom.py +++ b/Lib/test/test_minidom.py @@ -2,6 +2,8 @@ import copy import pickle +import io +import contextlib from test.support import findfile import unittest @@ -1560,5 +1562,28 @@ def testProcessingInstructionNameError(self): pi = doc.createProcessingInstruction("y", "z") pi.nodeValue = "crash" + def test_minidom_attribute_order(self): + xml_str = '' + doc = parseString(xml_str) + output = io.StringIO() + + with contextlib.redirect_stdout(output) as xml_file: + doc.writexml(xml_file) + self.assertEqual(output.getvalue(), + '') + + def test_toxml_with_attributes_ordered(self): + xml_str = '' + doc = parseString(xml_str) + self.assertEqual(doc.toxml(), + '') + + def test_toprettyxml_with_attributes_ordered(self): + xml_str = '' + doc = parseString(xml_str) + self.assertEqual(doc.toprettyxml(), + '\n' + '\n') + if __name__ == "__main__": unittest.main() diff --git a/Lib/xml/dom/minidom.py b/Lib/xml/dom/minidom.py index e44e04a069ecb4..469c51735e0385 100644 --- a/Lib/xml/dom/minidom.py +++ b/Lib/xml/dom/minidom.py @@ -854,9 +854,8 @@ def writexml(self, writer, indent="", addindent="", newl=""): writer.write(indent+"<" + self.tagName) attrs = self._get_attributes() - a_names = sorted(attrs.keys()) - for a_name in a_names: + for a_name in attrs.keys(): writer.write(" %s=\"" % a_name) _write_data(writer, attrs[a_name].value) writer.write("\"") From 46da804d97db09b603d5bada14359307d047325e Mon Sep 17 00:00:00 2001 From: Diego Rojas Date: Tue, 30 Oct 2018 17:17:37 -0500 Subject: [PATCH 2/3] Addressing feedback --- Lib/test/test_minidom.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/Lib/test/test_minidom.py b/Lib/test/test_minidom.py index d759118d559fa9..ad5be2f0f0e273 100644 --- a/Lib/test/test_minidom.py +++ b/Lib/test/test_minidom.py @@ -1566,17 +1566,13 @@ def test_minidom_attribute_order(self): xml_str = '' doc = parseString(xml_str) output = io.StringIO() - - with contextlib.redirect_stdout(output) as xml_file: - doc.writexml(xml_file) - self.assertEqual(output.getvalue(), - '') + doc.writexml(output) + self.assertEqual(output.getvalue(), xml_str) def test_toxml_with_attributes_ordered(self): xml_str = '' doc = parseString(xml_str) - self.assertEqual(doc.toxml(), - '') + self.assertEqual(doc.toxml(), xml_str) def test_toprettyxml_with_attributes_ordered(self): xml_str = '' From a78fcca3337b35f568b23a2793247ec575ffbc10 Mon Sep 17 00:00:00 2001 From: Diego Rojas Date: Wed, 7 Nov 2018 07:12:00 -0500 Subject: [PATCH 3/3] Adding news entry --- .../next/Library/2018-10-27-21-11-42.bpo-34160.UzyPZf.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2018-10-27-21-11-42.bpo-34160.UzyPZf.rst b/Misc/NEWS.d/next/Library/2018-10-27-21-11-42.bpo-34160.UzyPZf.rst index 6f3c076d3c0321..775d33aacda7aa 100644 --- a/Misc/NEWS.d/next/Library/2018-10-27-21-11-42.bpo-34160.UzyPZf.rst +++ b/Misc/NEWS.d/next/Library/2018-10-27-21-11-42.bpo-34160.UzyPZf.rst @@ -1 +1 @@ -ElementTree now preserves the attribute order specified by the user. +ElementTree and minidom now preserve the attribute order specified by the user.