Skip to content
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

Preserve attribute ordering across all Python versions #412

Merged
merged 1 commit into from
Dec 20, 2019
Merged
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
21 changes: 21 additions & 0 deletions pyxform/tests_v1/pyxform_test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,9 @@ def check_content(content):
"Invalid parameter: 'body__contains'." "Use 'xml__contains' instead"
)

# preserve attribute ordering across all Python versions before writing to string
reorder_attributes(root)

for code in ["xml", "instance", "model", "itext"]:
for verb in ["contains", "excludes"]:
(code__str, checker) = _check(code, verb)
Expand Down Expand Up @@ -330,3 +333,21 @@ def assertNotContains(self, content, text, msg_prefix=""):
self.assertEqual(
real_count, 0, msg_prefix + "Response should not contain %s" % text_repr
)


def reorder_attributes(root):
"""
Forces alphabetical ordering of all XML attributes to match pre Python 3.8 behavior.
In general, we should not rely on ordering, but changing all the tests is not
realistic at this moment.
See bottom of https://docs.python.org/3/library/xml.etree.elementtree.html#element-objects and
https://github.com/python/cpython/commit/a3697db0102b9b6747fe36009e42f9b08f0c1ea8 for more information.
"""
for el in root.iter():
attrib = el.attrib
if len(attrib) > 1:
# adjust attribute order, e.g. by sorting
attribs = sorted(attrib.items())
attrib.clear()
attrib.update(attribs)