GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — xml.etree.ElementTree.iterparse() should emit ResourceWarning when not explicitly closed · Issue #140601 · python/cpython

xml.etree.ElementTree.iterparse() should emit ResourceWarning when not explicitly closed #140601

New issue
Copy link
New issue
Copy link
Closed
Closed
xml.etree.ElementTree.iterparse() should emit ResourceWarning when not explicitly closed#140601
Copy link
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-XMLtype-featureA feature request or enhancementA feature request or enhancement

Description

@osamakader
osamakader
opened on Oct 25, 2025
Issue body actions

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 closed:

def __del__(self):
    # TODO: Emit a ResourceWarning if it was not explicitly closed.
    # (When the close() method will be supported in all maintained Python versions.)
    if close_source:
        source.close()

The close() method has been available for many years now, so this TODO can be implemented.

Problem

When 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:

  • File descriptor leaks
  • Files remaining locked on Windows
  • Resource exhaustion in long-running programs

Currently, no warning is issued to alert developers of this problem.

Proposed Solution

Implement the TODO by:

  1. Adding a _closed flag to track whether close() has been called
  2. Emitting a ResourceWarning in __del__() if the iterator was not closed and had opened a file internally (close_source == True)
  3. The warning should only apply when iterparse() opened the file itself (filename passed), not when a file object was provided

Example

Current behavior (no warning):

import xml.etree.ElementTree as ET

# This leaks a file handle but gives no warning
context = ET.iterparse('data.xml')
next(context)
# Iterator goes out of scope without close()

Expected behavior (with fix):

import xml.etree.ElementTree as ET

# This should emit ResourceWarning
context = ET.iterparse('data.xml')
next(context)
# ResourceWarning: unclosed file <_io.BufferedReader name='data.xml'>

# Proper usage (no warning):
context = ET.iterparse('data.xml')
next(context)
context.close()  # Explicit close

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

  • gh-140601: Add ResourceWarning to iterparse when not closed #140603
  • gh-140601: Refactor ElementTree.iterparse() tests #141499
  • [3.14] gh-140601: Refactor ElementTree.iterparse() tests (GH-141499) #141502
  • [3.13] gh-140601: Refactor ElementTree.iterparse() tests (GH-141499) #141503

Activity

  1. osamakader
    added
    type-featureA feature request or enhancementA feature request or enhancement
    on Oct 25, 2025
  2. osamakader
    added 5 commits that reference this issue on Oct 25, 2025

    pythongh-140601: Add ResourceWarning to iterparse when not closed

    e9cdee7

    pythongh-140601: Add ResourceWarning to iterparse when not closed

    a9e15e5

    pythongh-140601: Add ResourceWarning to iterparse when not closed

    b5c76da

    pythongh-140601: Add ResourceWarning to iterparse when not closed

    ad2354b

    pythongh-140601: Add ResourceWarning to iterparse when not closed

    7d1d79a
  3. picnixz
    added
    topic-XML
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    on Oct 25, 2025
  4. osamakader
    added 2 commits that reference this issue on Oct 25, 2025

    pythongh-140601: Add ResourceWarning to iterparse when not closed

    54742a1

    pythongh-140601: Add ResourceWarning to iterparse when not closed

    bf3f888
  5. serhiy-storchaka
    added 2 commits that reference this issue on Nov 13, 2025

    pythongh-140601: Refactor ElementTree.iterparse() tests

    3b35a2c

    gh-140601: Refactor ElementTree.iterparse() tests (GH-141499)

    2fbd396
  6. miss-islington
    added 2 commits that reference this issue on Nov 13, 2025

    pythongh-140601: Refactor ElementTree.iterparse() tests (pythonGH-141499

    75c5add

    pythongh-140601: Refactor ElementTree.iterparse() tests (pythonGH-141499

    f9570d7
  7. serhiy-storchaka
    added 3 commits that reference this issue on Nov 13, 2025

    [3.13] gh-140601: Refactor ElementTree.iterparse() tests (GH-141499) (G…

    72b6ff0

    [3.14] gh-140601: Refactor ElementTree.iterparse() tests (GH-141499) (G…

    7b8c257

    gh-140601: Add ResourceWarning to iterparse when not closed (GH-140603)

    a486d45
  8. serhiy-storchaka commented on Nov 13, 2025

    @serhiy-storchaka
    serhiy-storchaka
    on Nov 13, 2025
    Member
    More actions

    Thank you for your contribution @osamakader.

  9. serhiy-storchaka
    closed this as completedon Nov 13, 2025
  10. osamakader commented on Nov 13, 2025

    @osamakader
    osamakader
    on Nov 13, 2025
    ContributorAuthor
    More actions

    Many thanks @serhiy-storchaka for your support.

  11. CuriousLearner
    added 3 commits that reference this issue on Nov 13, 2025

    Merge branch 'main' into fix-issue21914

    b9f473c

    Merge branch 'main' of github.com:python/cpython into fix-Issue18387

    c46ecc5

    Merge branch 'main' of github.com:python/cpython into fix-issue24255

    c240289
  12. StanFromIreland
    added 2 commits that reference this issue on Dec 6, 2025

    pythongh-140601: Refactor ElementTree.iterparse() tests (pythonGH-141499

    62a94d2

    pythongh-140601: Add ResourceWarning to iterparse when not closed (py…

    50f2e4e
  13. fedepell
    added 3 commits that reference this issue on Dec 23, 2025

    Fix unit test with Python 3.15.0a3

    66566c9

    Fix unit test with Python 3.15.0a3

    9ea9fa3

    Fix unit test with Python 3.15.0a3

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-XMLtype-featureA feature request or enhancementA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.