Title: xml.etree.ElementTree.iterparse() should emit ResourceWarning when not explicitly closed · Issue #140601 · python/cpython · GitHub
Open Graph Title: xml.etree.ElementTree.iterparse() should emit ResourceWarning when not explicitly closed · Issue #140601 · python/cpython
X Title: xml.etree.ElementTree.iterparse() should emit ResourceWarning when not explicitly closed · Issue #140601 · python/cpython
Description: Feature or enhancement Proposal: Description The iterparse() function in xml.etree.ElementTree has a TODO comment (line 1270) requesting that a ResourceWarning be emitted when the iterator is garbage collected without being explicitly cl...
Open Graph Description: Feature or enhancement Proposal: Description The iterparse() function in xml.etree.ElementTree has a TODO comment (line 1270) requesting that a ResourceWarning be emitted when the iterator is garba...
X Description: Feature or enhancement Proposal: Description The iterparse() function in xml.etree.ElementTree has a TODO comment (line 1270) requesting that a ResourceWarning be emitted when the iterator is garba...
Opengraph URL: https://github.com/python/cpython/issues/140601
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"xml.etree.ElementTree.iterparse() should emit ResourceWarning when not explicitly closed","articleBody":"# Feature or enhancement\n\n### Proposal:\n\n### Description\n\nThe `iterparse()` function in `xml.etree.ElementTree` has a TODO comment (line 1270) requesting that a `ResourceWarning` be emitted when the iterator is garbage collected without being explicitly closed:\n\n```python\ndef __del__(self):\n # TODO: Emit a ResourceWarning if it was not explicitly closed.\n # (When the close() method will be supported in all maintained Python versions.)\n if close_source:\n source.close()\n```\n\nThe `close()` method has been available for many years now, so this TODO can be implemented.\n\n### Problem\n\nWhen `iterparse()` opens a file by filename (rather than accepting a pre-opened file object), it manages the file internally. If the iterator is not explicitly closed, the file handle remains open until garbage collection, which can lead to:\n\n- File descriptor leaks\n- Files remaining locked on Windows\n- Resource exhaustion in long-running programs\n\nCurrently, no warning is issued to alert developers of this problem.\n\n### Proposed Solution\n\nImplement the TODO by:\n\n1. Adding a `_closed` flag to track whether `close()` has been called\n2. Emitting a `ResourceWarning` in `__del__()` if the iterator was not closed and had opened a file internally (`close_source == True`)\n3. The warning should only apply when `iterparse()` opened the file itself (filename passed), not when a file object was provided\n\n### Example\n\n**Current behavior (no warning):**\n```python\nimport xml.etree.ElementTree as ET\n\n# This leaks a file handle but gives no warning\ncontext = ET.iterparse('data.xml')\nnext(context)\n# Iterator goes out of scope without close()\n```\n\n**Expected behavior (with fix):**\n```python\nimport xml.etree.ElementTree as ET\n\n# This should emit ResourceWarning\ncontext = ET.iterparse('data.xml')\nnext(context)\n# ResourceWarning: unclosed file \u003c_io.BufferedReader name='data.xml'\u003e\n\n# Proper usage (no warning):\ncontext = ET.iterparse('data.xml')\nnext(context)\ncontext.close() # Explicit close\n```\n\n### Has this already been discussed elsewhere?\n\nThis is a minor feature, which does not need previous discussion elsewhere\n\n### Links to previous discussion of this feature:\n\n_No response_\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-140603\n* gh-141499\n* gh-141502\n* gh-141503\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/osamakader","@type":"Person","name":"osamakader"},"datePublished":"2025-10-25T18:58:53.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/140601/cpython/issues/140601"}
| 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:65ccf3b4-5bca-f185-bf10-6ed1e9b3935a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D966:385F9F:1507B13:1CFC659:696AA479 |
| html-safe-nonce | 86fb7cd8247fa3520af88fc30cfa0dd2fd5ad9f163ce755d2957330124108798 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEOTY2OjM4NUY5RjoxNTA3QjEzOjFDRkM2NTk6Njk2QUE0NzkiLCJ2aXNpdG9yX2lkIjoiMjY3NjYxOTg2NjgzNDM3OTg5NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | a0f31b3216a6a687a04ee6bfda23c2b1715fe3b7a1dbc2b4467358f4ab7cbd20 |
| hovercard-subject-tag | issue:3552861781 |
| 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/140601/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f5ec27291936fcd071f365d423d8400a9d4cd385524f91358cb13a5bb38f6fef/python/cpython/issues/140601 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f5ec27291936fcd071f365d423d8400a9d4cd385524f91358cb13a5bb38f6fef/python/cpython/issues/140601 |
| og:image:alt | Feature or enhancement Proposal: Description The iterparse() function in xml.etree.ElementTree has a TODO comment (line 1270) requesting that a ResourceWarning be emitted when the iterator is garba... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | osamakader |
| hostname | github.com |
| expected-hostname | github.com |
| None | a51f97dbb9326f71c08ecb61577457d543c602124d1a2672871258ef37ac5261 |
| 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 | 4bd0eac606c70914085176ef312ebdcd97a8cdf1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width