Title: ElementTree: itertext() and tostring(method="text") lose the text of elements whose tag is a QName · Issue #161809 · python/cpython · GitHub
Open Graph Title: ElementTree: itertext() and tostring(method="text") lose the text of elements whose tag is a QName · Issue #161809 · python/cpython
X Title: ElementTree: itertext() and tostring(method="text") lose the text of elements whose tag is a QName · Issue #161809 · python/cpython
Description: Bug report Bug description Element.itertext(), and therefore tostring(..., method="text"), skips elements whose tag is not a str, in order to leave out comments and processing instructions. The same check also matches QName tags, so thei...
Open Graph Description: Bug report Bug description Element.itertext(), and therefore tostring(..., method="text"), skips elements whose tag is not a str, in order to leave out comments and processing instructions. The sam...
X Description: Bug report Bug description Element.itertext(), and therefore tostring(..., method="text"), skips elements whose tag is not a str, in order to leave out comments and processing instruction...
Opengraph URL: https://github.com/python/cpython/issues/161809
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"ElementTree: itertext() and tostring(method=\"text\") lose the text of elements whose tag is a QName","articleBody":"### Bug report\n\n### Bug description\n\n`Element.itertext()`, and therefore `tostring(..., method=\"text\")`, skips elements whose tag is not a `str`, in order to leave out comments and processing instructions. The same check also matches `QName` tags, so their text disappears, even though the tree serializes to XML with all of its text:\n\n```python\nimport xml.etree.ElementTree as ET\n\nroot = ET.Element(ET.QName(\"http://example.com/ns\", \"doc\"))\nroot.text = \"hello \"\nb = ET.SubElement(root, \"b\"); b.text = \"world\"; b.tail = \"!\"\ni = ET.SubElement(root, ET.QName(\"http://example.com/ns\", \"i\")); i.text = \"QTEXT\"\n\nET.tostring(root, encoding=\"unicode\")\n# '\u003cns0:doc xmlns:ns0=\"http://example.com/ns\">hello \u003cb>world\u003c/b>!\u003cns0:i>QTEXT\u003c/ns0:i>\u003c/ns0:doc>'\nlist(root.itertext())\n```\n\n| `list(root.itertext())` | C accelerator | pure Python |\n|---|---|---|\n| 3.12.16 | `['hello ', 'world', '!', 'QTEXT']` | `[]` |\n| 3.13.18, 3.14.10 | `['world', '!']` | `[]` |\n\n`tostring(root, method=\"text\", encoding=\"unicode\")` gives `'hello world!QTEXT'` with C on 3.12, `'world!'` with C on 3.13 and 3.14, and `''` with the pure-Python implementation.\n\n**Cause.** Both implementations skip on \"tag is not a `str`\": Python has `if not isinstance(tag, str) and tag is not None: return` in `Element.itertext()`, and C has `if (elem->tag != Py_None && !PyUnicode_Check(elem->tag))` in `elementiter_next()`. The C check came with the fix for gh-149571 (GH-149929), which made C skip comments and processing instructions like Python does; `QName` tags were not considered there. That is why C on 3.13 and 3.14 now drops the text of `QName` elements. It still yields their children's text and tails, while Python returns nothing for the whole subtree.\n\n\n\n### CPython versions tested on:\n\n3.12, 3.13, 3.14, CPython main branch\n\n### Operating systems tested on:\n\n_No response_\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-161878\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/ines-besrour","@type":"Person","name":"ines-besrour"},"datePublished":"2026-11-11T14:06:01Z","url":"https://github.com/python/cpython/issues/161809"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:0597d710-fa5d-120c-a3d5-ac3fcd3e75b4 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C672:8AE25:17195E7:1E98541:6B02086C |
| html-safe-nonce | 4706c844187533d451ce4309d7ddf2909d546617afecd30888653c58c111559d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNjcyOjhBRTI1OjE3MTk1RTc6MUU5ODU0MTo2QjAyMDg2QyIsInZpc2l0b3JfaWQiOiI4Njk1MDkyMTI2MzkzOTYwNTU2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 28a0cd0ab5d69377b994564f6f3ddcdb4502c937796c227d3f786b3373f8af78 |
| hovercard-subject-tag | issue:6330156069 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/161809/issue_layout |
| twitter:image | https://opengraph.githubassets.com/83f770b3f28cdb3ec095eb77986eadac9a24391eac3705092320d546ba20db03/python/cpython/issues/161809 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/83f770b3f28cdb3ec095eb77986eadac9a24391eac3705092320d546ba20db03/python/cpython/issues/161809 |
| og:image:alt | Bug report Bug description Element.itertext(), and therefore tostring(..., method="text"), skips elements whose tag is not a str, in order to leave out comments and processing instructions. The sam... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ines-besrour |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width