Skip to content

Conversation

@osamakader
Copy link

@osamakader osamakader commented Oct 25, 2025

When iterparse() opens a file by filename and is not explicitly closed, emit a ResourceWarning to alert developers of the resource leak.

This implements the TODO comment at line 1270 of ElementTree.py which has been requesting this feature since the close() method was added.

  • Add _closed flag to IterParseIterator to track state
  • Emit ResourceWarning in del if not closed
  • Add comprehensive test cases
  • Update existing tests to properly close iterators

@python-cla-bot
Copy link

python-cla-bot bot commented Oct 25, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app
Copy link

bedevere-app bot commented Oct 25, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

When iterparse() opens a file by filename and is not explicitly closed,
emit a ResourceWarning to alert developers of the resource leak.

This implements the TODO comment at line 1270 of ElementTree.py which
has been requesting this feature since the close() method was added.

- Add _closed flag to IterParseIterator to track state
- Emit ResourceWarning in __del__ if not closed
- Add comprehensive test cases
- Update existing tests to properly close iterators

Signed-off-by: Osama Abdelkader <osama.abdelkader@gmail.com>
@osamakader osamakader force-pushed the issue-140601-iterparse-warning branch from 54742a1 to bf3f888 Compare October 25, 2025 20:50
@cmaloney
Copy link
Contributor

Please don't force push to CPython PRs; it's preferred to keep commit history intact for easier review. See: https://devguide.python.org/getting-started/pull-request-lifecycle/#quick-guide

@cmaloney
Copy link
Contributor

cc: @serhiy-storchaka who added the close() ResourceWarning comment

ashm-dev

This comment was marked as duplicate.

it = iterparse(SIMPLE_XMLFILE)
del it
import gc
gc.collect() # Ensure previous iterator is cleaned up
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be better to use support.gc_collect() instead of directly calling gc.collect() was specifically added for use in tests to ensure consistent and deterministic garbage collection behavior across platforms and configurations.

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests were intentionally added to check that the resource is closed without explicit close(). Don't add close(). Just replace check_no_resource_warning() with assertWarns(ResourceWarning) and add gc_collect(). There are other tests that explicitly call close().

- Use nonlocal close_source instead of _closed flag
- Bind warnings.warn as default parameter in __del__
- Use assertWarns(ResourceWarning) instead of adding close() calls
- Use support.gc_collect() for consistent test behavior
- Remove broad exception handling

Signed-off-by: Osama Abdelkader <osama.abdelkader@gmail.com>
Signed-off-by: Osama Abdelkader <osama.abdelkader@gmail.com>
Signed-off-by: Osama Abdelkader <osama.abdelkader@gmail.com>
F-strings can fail during cleanup. Use % formatting like
subprocess.Popen does for safer __del__ behavior.

Signed-off-by: Osama Abdelkader <osama.abdelkader@gmail.com>
Check that warning message contains 'unclosed file' and the
filename to ensure it's from iterparse, not the file destructor.

Addresses review feedback from serhiy-storchaka.

Signed-off-by: Osama Abdelkader <osama.abdelkader@gmail.com>
The ResourceWarning tests are already covered in test_iterparse()
as noted by serhiy-storchaka in review.

Signed-off-by: Osama Abdelkader <osama.abdelkader@gmail.com>
Removing source=self prevents unraisable exception in C extension.
The warning message still contains the filename via %r formatting.

Signed-off-by: Osama Abdelkader <osama.abdelkader@gmail.com>
@osamakader
Copy link
Author

Hi @serhiy-storchaka

I've addressed all the review feedback:
All tests pass locally. However, CI shows "Unraisable exception"
warnings in the C extension tests (test_xml_etree_c). The ResourceWarning
is still being emitted correctly, but it's reported as "unraisable".

Is this expected behavior for warnings in __del__ with C extensions,
or do I need to adjust the implementation?

Thanks!

@serhiy-storchaka
Copy link
Member

No, it is not an expected behavior.

I have fixed the warnings. The underlying file is closed when the iterator has been exhausted or raised an exception, so no explicit close() is needed in this case. And many tests depended on this. I have removed a ResourceWarning in such case.

Please add a versionchanged directive in the module documentation. I am not sure about the What's New document. This is a minor change, so it may not need a What's New entry.

Document that iterparse now emits ResourceWarning in Python 3.15
when not explicitly closed.

Signed-off-by: Osama Abdelkader <osama.abdelkader@gmail.com>
@osamakader
Copy link
Author

Thanks @serhiy-storchaka for the support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants