From 1f00810b5b57a8bfce693154ad0bcd8dbded5675 Mon Sep 17 00:00:00 2001 From: pprossi Date: Thu, 19 Dec 2024 13:14:32 +0100 Subject: [PATCH 1/9] Update ElementTree.py Added possibility to customize hardcoded xml declaration --- Lib/xml/etree/ElementTree.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/Lib/xml/etree/ElementTree.py b/Lib/xml/etree/ElementTree.py index ce67d7d7d54748..32a6ecd2dd2d50 100644 --- a/Lib/xml/etree/ElementTree.py +++ b/Lib/xml/etree/ElementTree.py @@ -679,6 +679,7 @@ def iterfind(self, path, namespaces=None): def write(self, file_or_filename, encoding=None, xml_declaration=None, + xml_declaration_definition='', default_namespace=None, method=None, *, short_empty_elements=True): @@ -694,6 +695,14 @@ def write(self, file_or_filename, is added if encoding IS NOT either of: US-ASCII, UTF-8, or Unicode + *xml_declaration_definition* -- string for customizing encoding declaration + as documentation always shows doublequotes but + singlequote where hardcoded here. + to be rfc conform which allows doublequotes and + singlequote for declaration. default value is + switched to doublquotes here to stay on one format. + placeholders: {version}, {encoding} + *default_namespace* -- sets the default XML namespace (for "xmlns") *method* -- either "xml" (default), "html, "text", or "c14n" @@ -719,8 +728,10 @@ def write(self, file_or_filename, (xml_declaration is None and encoding.lower() != "unicode" and declared_encoding.lower() not in ("utf-8", "us-ascii"))): - write("\n" % ( - declared_encoding,)) + # version configuration is'nt necessary, can be overwritten + # in declaration_definition at runtime + data = {'version':'1.0', 'encoding': declared_encoding} + write(xml_declaration_definition.format(**data)+"\n") if method == "text": _serialize_text(write, self._root) else: From 9b3d9038768d072c83fffe1bb05f57541c73339a Mon Sep 17 00:00:00 2001 From: pprossi Date: Thu, 19 Dec 2024 14:19:18 +0100 Subject: [PATCH 2/9] Update ElementTree.py fixed lint error triling spaces unchanged default declaration to pass tests --- Lib/xml/etree/ElementTree.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Lib/xml/etree/ElementTree.py b/Lib/xml/etree/ElementTree.py index 32a6ecd2dd2d50..712b846b14414f 100644 --- a/Lib/xml/etree/ElementTree.py +++ b/Lib/xml/etree/ElementTree.py @@ -679,7 +679,7 @@ def iterfind(self, path, namespaces=None): def write(self, file_or_filename, encoding=None, xml_declaration=None, - xml_declaration_definition='', + xml_declaration_definition="", default_namespace=None, method=None, *, short_empty_elements=True): @@ -700,7 +700,7 @@ def write(self, file_or_filename, singlequote where hardcoded here. to be rfc conform which allows doublequotes and singlequote for declaration. default value is - switched to doublquotes here to stay on one format. + untouched to pass tests. placeholders: {version}, {encoding} *default_namespace* -- sets the default XML namespace (for "xmlns") @@ -731,7 +731,7 @@ def write(self, file_or_filename, # version configuration is'nt necessary, can be overwritten # in declaration_definition at runtime data = {'version':'1.0', 'encoding': declared_encoding} - write(xml_declaration_definition.format(**data)+"\n") + write(xml_declaration_definition.format(**data)+"\n") if method == "text": _serialize_text(write, self._root) else: From b7967aede85c38bcbdbcead97e5b687c6520f0e1 Mon Sep 17 00:00:00 2001 From: pprossi Date: Fri, 20 Dec 2024 17:58:10 +0100 Subject: [PATCH 3/9] Update test_xml_etree.py test cases for custom xml declaration --- Lib/test/test_xml_etree.py | 48 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py index ae06a9cc11855f..745a4fd71310f2 100644 --- a/Lib/test/test_xml_etree.py +++ b/Lib/test/test_xml_etree.py @@ -4021,6 +4021,54 @@ def test_write_to_user_binary_writer_with_bom(self): '''\n''' ''''''.encode("utf-16")) + def test_custom_declaration_to_user_binary_writer_with_bom(self): + tree = ET.ElementTree(ET.XML('''''')) + raw = io.BytesIO() + writer = self.dummy() + writer.write = raw.write + writer.seekable = lambda: True + writer.tell = raw.tell + tree.write(writer, encoding="utf-16", xml_declaration_definition='') + self.assertEqual(raw.getvalue(), + '''\n''' + ''''''.encode("utf-16")) + + def test_custom_declaration2_to_user_binary_writer_with_bom(self): + tree = ET.ElementTree(ET.XML('''''')) + raw = io.BytesIO() + writer = self.dummy() + writer.write = raw.write + writer.seekable = lambda: True + writer.tell = raw.tell + tree.write(writer, encoding="utf-16", xml_declaration_definition='') + self.assertEqual(raw.getvalue(), + '''\n''' + ''''''.encode("utf-16")) + + def test_custom_declaration3_to_user_binary_writer_with_bom(self): + tree = ET.ElementTree(ET.XML('''''')) + raw = io.BytesIO() + writer = self.dummy() + writer.write = raw.write + writer.seekable = lambda: True + writer.tell = raw.tell + tree.write(writer, encoding="utf-16", xml_declaration_definition='') + self.assertEqual(raw.getvalue(), + '''\n''' + ''''''.encode("utf-16")) + + def test_custom_declaration4_to_user_binary_writer_with_bom(self): + tree = ET.ElementTree(ET.XML('''''')) + raw = io.BytesIO() + writer = self.dummy() + writer.write = raw.write + writer.seekable = lambda: True + writer.tell = raw.tell + tree.write(writer, encoding="utf-16", xml_declaration_definition='') + self.assertEqual(raw.getvalue(), + '''\n''' + ''''''.encode("utf-16")) + r def test_tostringlist_invariant(self): root = ET.fromstring('foo') self.assertEqual( From b90f90559bb6ae05ea62000964ea0b7c8d047e58 Mon Sep 17 00:00:00 2001 From: pprossi Date: Fri, 20 Dec 2024 19:21:17 +0100 Subject: [PATCH 4/9] Update test_xml_etree.py fix: unknowen r in code :) --- Lib/test/test_xml_etree.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py index 745a4fd71310f2..b5a22e8c216212 100644 --- a/Lib/test/test_xml_etree.py +++ b/Lib/test/test_xml_etree.py @@ -4068,7 +4068,7 @@ def test_custom_declaration4_to_user_binary_writer_with_bom(self): self.assertEqual(raw.getvalue(), '''\n''' ''''''.encode("utf-16")) - r + def test_tostringlist_invariant(self): root = ET.fromstring('foo') self.assertEqual( From b90b81e1cda9c6dd77ae69e818cef8e7a7bae039 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Fri, 20 Dec 2024 18:54:54 +0000 Subject: [PATCH 5/9] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2024-12-20-18-54-53.gh-issue-128136.tgbvWt.rst | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2024-12-20-18-54-53.gh-issue-128136.tgbvWt.rst diff --git a/Misc/NEWS.d/next/Library/2024-12-20-18-54-53.gh-issue-128136.tgbvWt.rst b/Misc/NEWS.d/next/Library/2024-12-20-18-54-53.gh-issue-128136.tgbvWt.rst new file mode 100644 index 00000000000000..beed547922dae9 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-12-20-18-54-53.gh-issue-128136.tgbvWt.rst @@ -0,0 +1,13 @@ +New parameter added to the write function that allows you to customize the xml declaration, which was previously hard-coded. + +The standard functionality is retained; if you don't specify the xml_declaration_definition parameter, the previous standard declaration is used. " + +When specifying the parameter, the following options are available. +E.g.: +- tee.write(file, encoding="utf-8", xml_declaration=True, xml_declaration_definition='''''') + +- tee.write(file, encoding="utf-8", xml_declaration=True, xml_declaration_definition='''''') + +- tee.write(file, encoding="utf-8", xml_declaration=True, xml_declaration_definition='''''') + +The placeholders {version} and {encoding} are replaced, version is always 1.0 and enconding depends on the code as before. From 6018922edfd3347945e92f40bcb2bb772f8e5967 Mon Sep 17 00:00:00 2001 From: pprossi Date: Fri, 20 Dec 2024 20:23:44 +0100 Subject: [PATCH 6/9] Update 2024-12-20-18-54-53.gh-issue-128136.tgbvWt.rst --- .../2024-12-20-18-54-53.gh-issue-128136.tgbvWt.rst | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2024-12-20-18-54-53.gh-issue-128136.tgbvWt.rst b/Misc/NEWS.d/next/Library/2024-12-20-18-54-53.gh-issue-128136.tgbvWt.rst index beed547922dae9..118525274dfb92 100644 --- a/Misc/NEWS.d/next/Library/2024-12-20-18-54-53.gh-issue-128136.tgbvWt.rst +++ b/Misc/NEWS.d/next/Library/2024-12-20-18-54-53.gh-issue-128136.tgbvWt.rst @@ -1,13 +1,12 @@ New parameter added to the write function that allows you to customize the xml declaration, which was previously hard-coded. -The standard functionality is retained; if you don't specify the xml_declaration_definition parameter, the previous standard declaration is used. " +The standard functionality is retained; if you don't specify the xml_declaration_definition parameter, the previous standard declaration is used. ```` When specifying the parameter, the following options are available. E.g.: -- tee.write(file, encoding="utf-8", xml_declaration=True, xml_declaration_definition='''''') -- tee.write(file, encoding="utf-8", xml_declaration=True, xml_declaration_definition='''''') +- tree.write(file, encoding="utf-8", xml_declaration=True, xml_declaration_definition='''''') +- tree.write(file, encoding="utf-8", xml_declaration=True, xml_declaration_definition='''''') +- tree.write(file, encoding="utf-8", xml_declaration=True, xml_declaration_definition='''''') -- tee.write(file, encoding="utf-8", xml_declaration=True, xml_declaration_definition='''''') - -The placeholders {version} and {encoding} are replaced, version is always 1.0 and enconding depends on the code as before. +The placeholders {version} and {encoding} are replaced, version is always 1.0 and enconding depends on the code as before. From 52bc8dfaf44c3ba06418d9c331377450a7d27910 Mon Sep 17 00:00:00 2001 From: pprossi Date: Fri, 20 Dec 2024 20:33:06 +0100 Subject: [PATCH 7/9] Update 2024-12-20-18-54-53.gh-issue-128136.tgbvWt.rst --- .../next/Library/2024-12-20-18-54-53.gh-issue-128136.tgbvWt.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/Misc/NEWS.d/next/Library/2024-12-20-18-54-53.gh-issue-128136.tgbvWt.rst b/Misc/NEWS.d/next/Library/2024-12-20-18-54-53.gh-issue-128136.tgbvWt.rst index 118525274dfb92..f12522fbae5e3b 100644 --- a/Misc/NEWS.d/next/Library/2024-12-20-18-54-53.gh-issue-128136.tgbvWt.rst +++ b/Misc/NEWS.d/next/Library/2024-12-20-18-54-53.gh-issue-128136.tgbvWt.rst @@ -9,4 +9,5 @@ E.g.: - tree.write(file, encoding="utf-8", xml_declaration=True, xml_declaration_definition='''''') - tree.write(file, encoding="utf-8", xml_declaration=True, xml_declaration_definition='''''') + The placeholders {version} and {encoding} are replaced, version is always 1.0 and enconding depends on the code as before. From 5b15a7eac12db7c4e63ed1848c4b655f3a64b530 Mon Sep 17 00:00:00 2001 From: pprossi Date: Fri, 20 Dec 2024 21:42:08 +0100 Subject: [PATCH 8/9] Update 2024-12-20-18-54-53.gh-issue-128136.tgbvWt.rst --- .../2024-12-20-18-54-53.gh-issue-128136.tgbvWt.rst | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2024-12-20-18-54-53.gh-issue-128136.tgbvWt.rst b/Misc/NEWS.d/next/Library/2024-12-20-18-54-53.gh-issue-128136.tgbvWt.rst index f12522fbae5e3b..f2886bd4bbf1ae 100644 --- a/Misc/NEWS.d/next/Library/2024-12-20-18-54-53.gh-issue-128136.tgbvWt.rst +++ b/Misc/NEWS.d/next/Library/2024-12-20-18-54-53.gh-issue-128136.tgbvWt.rst @@ -1,13 +1,8 @@ New parameter added to the write function that allows you to customize the xml declaration, which was previously hard-coded. - The standard functionality is retained; if you don't specify the xml_declaration_definition parameter, the previous standard declaration is used. ```` - When specifying the parameter, the following options are available. E.g.: - -- tree.write(file, encoding="utf-8", xml_declaration=True, xml_declaration_definition='''''') -- tree.write(file, encoding="utf-8", xml_declaration=True, xml_declaration_definition='''''') -- tree.write(file, encoding="utf-8", xml_declaration=True, xml_declaration_definition='''''') - - +tree.write(file, encoding="utf-8", xml_declaration=True, xml_declaration_definition='''''') +tree.write(file, encoding="utf-8", xml_declaration=True, xml_declaration_definition='''''') +tree.write(file, encoding="utf-8", xml_declaration=True, xml_declaration_definition='''''') The placeholders {version} and {encoding} are replaced, version is always 1.0 and enconding depends on the code as before. From d8a23006b8ff0f0ebd1ae968dbdb2b01a1539b26 Mon Sep 17 00:00:00 2001 From: pprossi Date: Sat, 21 Dec 2024 11:22:26 +0100 Subject: [PATCH 9/9] Update ElementTree.py move new parameter to end of argument list to non positional parameters. --- Lib/xml/etree/ElementTree.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/xml/etree/ElementTree.py b/Lib/xml/etree/ElementTree.py index 712b846b14414f..06d5f04ac577ed 100644 --- a/Lib/xml/etree/ElementTree.py +++ b/Lib/xml/etree/ElementTree.py @@ -679,10 +679,10 @@ def iterfind(self, path, namespaces=None): def write(self, file_or_filename, encoding=None, xml_declaration=None, - xml_declaration_definition="", default_namespace=None, method=None, *, - short_empty_elements=True): + short_empty_elements=True, + xml_declaration_definition=""): """Write element tree to a file as XML. Arguments: