Skip to content

bpo-34160: Preserve user specified order of Element attributes #10163

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 28, 2018
Merged
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
8 changes: 8 additions & 0 deletions Doc/library/xml.etree.elementtree.rst
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,10 @@ Functions

*elem* is an element tree or an individual element.

.. versionchanged:: 3.8
The :func:`dump` function now preserves the attribute order specified
by the user.


.. function:: fromstring(text)

Expand Down Expand Up @@ -947,6 +951,10 @@ ElementTree Objects
.. versionadded:: 3.4
The *short_empty_elements* parameter.

.. versionchanged:: 3.8
The :meth:`write` method now preserves the attribute order specified
by the user.


This is the XML file that is going to be manipulated::

Expand Down
20 changes: 20 additions & 0 deletions Lib/test/test_xml_etree.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# For this purpose, the module-level "ET" symbol is temporarily
# monkey-patched when running the "test_xml_etree_c" test suite.

import contextlib
import copy
import functools
import html
Expand Down Expand Up @@ -1044,6 +1045,25 @@ def test_html_empty_elems_serialization(self):
method='html')
self.assertEqual(serialized, expected)

def test_dump_attribute_order(self):
# See BPO 34160
e = ET.Element('cirriculum', status='public', company='example')
with support.captured_stdout() as stdout:
ET.dump(e)
self.assertEqual(stdout.getvalue(),
'<cirriculum status="public" company="example" />\n')

def test_tree_write_attribute_order(self):
# See BPO 34160
root = ET.Element('cirriculum', status='public', company='example')
tree = ET.ElementTree(root)
f = io.BytesIO()
with contextlib.redirect_stdout(f):
tree.write(f, encoding='utf-8', xml_declaration=True)
self.assertEqual(f.getvalue(),
b"<?xml version='1.0' encoding='utf-8'?>\n"
b'<cirriculum status="public" company="example" />')


class XMLPullParserTest(unittest.TestCase):

Expand Down
2 changes: 1 addition & 1 deletion Lib/xml/etree/ElementTree.py
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,7 @@ def _serialize_xml(write, elem, qnames, namespaces,
k,
_escape_attrib(v)
))
for k, v in sorted(items): # lexical order
for k, v in items:
if isinstance(k, QName):
k = k.text
if isinstance(v, QName):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ElementTree now preserves the attribute order specified by the user.