This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Add default_namespace argument to xml.etree.ElementTree.tostring()
Type: enhancement Stage: resolved
Components: Library (Lib), XML Versions: Python 3.8
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Bernt.Røskar.Brenna, eli.bendersky, scoder, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2019-03-07 18:57 by Bernt.Røskar.Brenna, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 12225 merged Bernt.Røskar.Brenna, 2019-03-07 19:10
Messages (9)
msg337428 - (view) Author: Bernt Røskar Brenna (Bernt.Røskar.Brenna) * Date: 2019-03-07 18:57
default_namespace is often used when serializing ET elements. tostring() is mainly a wrapper around ElementTree.write(), and it is therefore natural that it mirrors write's argument. tostring() already passes encoding, method and short_empty_elements to write.
msg337455 - (view) Author: Stefan Behnel (scoder) * (Python committer) Date: 2019-03-08 08:37
The feature seems reasonable to me and the patch looks good.
msg337456 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-03-08 08:50
See also issue31256.

I do not know what is better: make tostring() to accept all options of write(), or keep it simpler. What strategy lxml supports?
msg337458 - (view) Author: Stefan Behnel (scoder) * (Python committer) Date: 2019-03-08 08:58
lxml does not support the "default_namespace" option specifically (because its tree model preserves namespace prefixes), but it generally makes all (justifiable) serialisation options available to both tostring() and ET.write().

I think the same should apply to ElementTree. Both the "default_namespace" and "doctype" options seem useful regardless of the serialisation target.
msg337460 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-03-08 09:51
Okay. Bernt, do you mind to add also the xml_declaration option in PR 12225 or create a separate PR for issue31256?
msg337473 - (view) Author: Bernt Røskar Brenna (Bernt.Røskar.Brenna) * Date: 2019-03-08 13:28
I will add xml_declaration and push to the existing PR.
msg337478 - (view) Author: Bernt Røskar Brenna (Bernt.Røskar.Brenna) * Date: 2019-03-08 14:02
I pushed changes to the PR. I also added xml_declaration and default_namespace to the tostringlist() method.
msg340190 - (view) Author: Stefan Behnel (scoder) * (Python committer) Date: 2019-04-14 08:07
New changeset ffca16e25a70fd44a87b13b379b5ec0c7a11e926 by Stefan Behnel (Bernt Røskar Brenna) in branch 'master':
bpo-36227: ElementTree.tostring() default_namespace and xml_declaration arguments (GH-12225)
msg340193 - (view) Author: Stefan Behnel (scoder) * (Python committer) Date: 2019-04-14 08:16
Thank you for you contribution.
Date User Action Args
2022-04-11 14:59:12adminsetgithub: 80408
2019-04-14 08:16:04scodersetstatus: open -> closed
messages: + msg340193

components: + XML
resolution: fixed
stage: patch review -> resolved
2019-04-14 08:07:16scodersetmessages: + msg340190
2019-03-08 14:02:12Bernt.Røskar.Brennasetmessages: + msg337478
2019-03-08 13:28:32Bernt.Røskar.Brennasetmessages: + msg337473
2019-03-08 09:51:18serhiy.storchakasetmessages: + msg337460
2019-03-08 08:58:56scodersetmessages: + msg337458
2019-03-08 08:50:51serhiy.storchakasetmessages: + msg337456
2019-03-08 08:37:57scodersetmessages: + msg337455
2019-03-08 03:06:25xtreaksetnosy: + scoder, eli.bendersky, serhiy.storchaka
2019-03-07 19:10:22Bernt.Røskar.Brennasetkeywords: + patch
stage: patch review
pull_requests: + pull_request12215
2019-03-07 18:57:14Bernt.Røskar.Brennacreate