From 3d03255c95a3e0473a8843947989e9e1233eeab4 Mon Sep 17 00:00:00 2001 From: Daniel Hillier Date: Fri, 26 Apr 2024 21:55:37 +1000 Subject: [PATCH 1/3] Remove outdated c14n references from ElementTree Some references to a removed `c14n` serialization method remained in ElementTree.py. Attempting to use `method='c14n'` in the `write()` method would result in `ValueError: unknown method 'c14n'`. Removes the `write_c14n()` method that uses `method='c14n'` and also raises the same error. --- Lib/xml/etree/ElementTree.py | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/Lib/xml/etree/ElementTree.py b/Lib/xml/etree/ElementTree.py index 9e15d34d22aa6c4..3f5ef4c63598a65 100644 --- a/Lib/xml/etree/ElementTree.py +++ b/Lib/xml/etree/ElementTree.py @@ -696,7 +696,7 @@ def write(self, file_or_filename, *default_namespace* -- sets the default XML namespace (for "xmlns") - *method* -- either "xml" (default), "html, "text", or "c14n" + *method* -- either "xml" (default), "html or "text" *short_empty_elements* -- controls the formatting of elements that contain no content. If True (default) @@ -710,10 +710,7 @@ def write(self, file_or_filename, elif method not in _serialize: raise ValueError("unknown method %r" % method) if not encoding: - if method == "c14n": - encoding = "utf-8" - else: - encoding = "us-ascii" + encoding = "us-ascii" with _get_writer(file_or_filename, encoding) as (write, declared_encoding): if method == "xml" and (xml_declaration or (xml_declaration is None and @@ -729,10 +726,6 @@ def write(self, file_or_filename, serialize(write, self._root, qnames, namespaces, short_empty_elements=short_empty_elements) - def write_c14n(self, file): - # lxml.etree compatibility. use output method instead - return self.write(file, method="c14n") - # -------------------------------------------------------------------- # serialization support @@ -961,8 +954,6 @@ def _serialize_text(write, elem): "xml": _serialize_xml, "html": _serialize_html, "text": _serialize_text, -# this optional method is imported at the end of the module -# "c14n": _serialize_c14n, } @@ -1074,8 +1065,8 @@ def tostring(element, encoding=None, method=None, *, *element* is an Element instance, *encoding* is an optional output encoding defaulting to US-ASCII, *method* is an optional output which can - be one of "xml" (default), "html", "text" or "c14n", *default_namespace* - sets the default XML namespace (for "xmlns"). + be one of "xml" (default), "html" or "text", *default_namespace* sets the + default XML namespace (for "xmlns"). Returns an (optionally) encoded string containing the XML data. From 95faf7febbcbc259146589a12e7f55f2bdcf4bc6 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 25 May 2026 12:56:04 +0300 Subject: [PATCH 2/3] Update Lib/xml/etree/ElementTree.py --- Lib/xml/etree/ElementTree.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/xml/etree/ElementTree.py b/Lib/xml/etree/ElementTree.py index d793193e6e82c23..26d6f2c84c89eed 100644 --- a/Lib/xml/etree/ElementTree.py +++ b/Lib/xml/etree/ElementTree.py @@ -714,7 +714,7 @@ def write(self, file_or_filename, *default_namespace* -- sets the default XML namespace (for "xmlns") - *method* -- either "xml" (default), "html or "text" + *method* -- either "xml" (default), "html" or "text" *short_empty_elements* -- controls the formatting of elements that contain no content. If True From 7a7fe173a94390cc46474c9bf312fe69fc028c8b Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sun, 9 Aug 2026 23:26:41 +0300 Subject: [PATCH 3/3] Add NEWS and What's New entries --- Doc/whatsnew/3.16.rst | 11 +++++++++++ .../2026-08-09-21-30-00.gh-issue-118318.Xr4Kd8.rst | 5 +++++ 2 files changed, 16 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-08-09-21-30-00.gh-issue-118318.Xr4Kd8.rst diff --git a/Doc/whatsnew/3.16.rst b/Doc/whatsnew/3.16.rst index f1ff4fcf9bafde0..de1751e2fab4373 100644 --- a/Doc/whatsnew/3.16.rst +++ b/Doc/whatsnew/3.16.rst @@ -186,6 +186,17 @@ tarfile * The undocumented and unused :attr:`!tarfile.TarFile.tarfile` attribute has been deprecated since Python 3.13. +xml.etree.ElementTree +--------------------- + +* The undocumented :meth:`!xml.etree.ElementTree.ElementTree.write_c14n` method + and the support of ``method="c14n"`` in + :meth:`~xml.etree.ElementTree.ElementTree.write` and + :func:`~xml.etree.ElementTree.tostring`. + They never worked and always raised :exc:`ValueError`. + Use :func:`xml.etree.ElementTree.canonicalize` instead. + (Contributed by Daniel Hillier in :gh:`118318`.) + .. Add removals above alphabetically, not here at the end. diff --git a/Misc/NEWS.d/next/Library/2026-08-09-21-30-00.gh-issue-118318.Xr4Kd8.rst b/Misc/NEWS.d/next/Library/2026-08-09-21-30-00.gh-issue-118318.Xr4Kd8.rst new file mode 100644 index 000000000000000..31a39e50c72dd5f --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-08-09-21-30-00.gh-issue-118318.Xr4Kd8.rst @@ -0,0 +1,5 @@ +Remove the ``write_c14n()`` method of :class:`xml.etree.ElementTree.ElementTree` +and the support of ``method="c14n"`` in :meth:`~xml.etree.ElementTree.ElementTree.write` +and :func:`~xml.etree.ElementTree.tostring`. They never worked and always +raised :exc:`ValueError`. Use :func:`xml.etree.ElementTree.canonicalize` +instead.