mirror of
https://github.com/python/cpython.git
synced 2025-12-23 09:19:18 +00:00
gh-140601: Add ResourceWarning to iterparse when not closed (GH-140603)
Some checks failed
Tests / Change detection (push) Waiting to run
Tests / Docs (push) Blocked by required conditions
Tests / (push) Blocked by required conditions
Tests / Windows MSI (push) Blocked by required conditions
Tests / Check if Autoconf files are up to date (push) Blocked by required conditions
Tests / Check if generated files are up to date (push) Blocked by required conditions
Tests / Ubuntu SSL tests with OpenSSL (push) Blocked by required conditions
Tests / Ubuntu SSL tests with AWS-LC (push) Blocked by required conditions
Tests / Android (aarch64) (push) Blocked by required conditions
Tests / Android (x86_64) (push) Blocked by required conditions
Tests / iOS (push) Blocked by required conditions
Tests / WASI (push) Blocked by required conditions
Tests / Hypothesis tests on Ubuntu (push) Blocked by required conditions
Tests / Address sanitizer (push) Blocked by required conditions
Tests / Sanitizers (push) Blocked by required conditions
Tests / Cross build Linux (push) Blocked by required conditions
Tests / CIFuzz (push) Blocked by required conditions
Tests / All required checks pass (push) Blocked by required conditions
Lint / lint (push) Waiting to run
mypy / Run mypy on Lib/_pyrepl (push) Waiting to run
mypy / Run mypy on Lib/test/libregrtest (push) Waiting to run
mypy / Run mypy on Lib/tomllib (push) Waiting to run
mypy / Run mypy on Tools/build (push) Waiting to run
mypy / Run mypy on Tools/cases_generator (push) Waiting to run
mypy / Run mypy on Tools/clinic (push) Waiting to run
mypy / Run mypy on Tools/jit (push) Waiting to run
mypy / Run mypy on Tools/peg_generator (push) Waiting to run
Tail calling interpreter / aarch64-apple-darwin/clang (push) Waiting to run
Tail calling interpreter / aarch64-unknown-linux-gnu/gcc (push) Waiting to run
Tail calling interpreter / x86_64-pc-windows-msvc/msvc (push) Waiting to run
Tail calling interpreter / x86_64-apple-darwin/clang (push) Waiting to run
Tail calling interpreter / free-threading (push) Waiting to run
Tail calling interpreter / x86_64-unknown-linux-gnu/gcc (push) Waiting to run
JIT / Interpreter (Debug) (push) Has been cancelled
JIT / aarch64-apple-darwin/clang (Release) (push) Has been cancelled
JIT / aarch64-unknown-linux-gnu/gcc (Release) (push) Has been cancelled
JIT / aarch64-apple-darwin/clang (Debug) (push) Has been cancelled
JIT / aarch64-unknown-linux-gnu/gcc (Debug) (push) Has been cancelled
JIT / x86_64-apple-darwin/clang (Release) (push) Has been cancelled
JIT / x86_64-unknown-linux-gnu/gcc (Release) (push) Has been cancelled
JIT / x86_64-apple-darwin/clang (Debug) (push) Has been cancelled
JIT / x86_64-unknown-linux-gnu/gcc (Debug) (push) Has been cancelled
JIT / Free-Threaded (Debug) (push) Has been cancelled
JIT / JIT without optimizations (Debug) (push) Has been cancelled
JIT / JIT with tail calling interpreter (push) Has been cancelled
Some checks failed
Tests / Change detection (push) Waiting to run
Tests / Docs (push) Blocked by required conditions
Tests / (push) Blocked by required conditions
Tests / Windows MSI (push) Blocked by required conditions
Tests / Check if Autoconf files are up to date (push) Blocked by required conditions
Tests / Check if generated files are up to date (push) Blocked by required conditions
Tests / Ubuntu SSL tests with OpenSSL (push) Blocked by required conditions
Tests / Ubuntu SSL tests with AWS-LC (push) Blocked by required conditions
Tests / Android (aarch64) (push) Blocked by required conditions
Tests / Android (x86_64) (push) Blocked by required conditions
Tests / iOS (push) Blocked by required conditions
Tests / WASI (push) Blocked by required conditions
Tests / Hypothesis tests on Ubuntu (push) Blocked by required conditions
Tests / Address sanitizer (push) Blocked by required conditions
Tests / Sanitizers (push) Blocked by required conditions
Tests / Cross build Linux (push) Blocked by required conditions
Tests / CIFuzz (push) Blocked by required conditions
Tests / All required checks pass (push) Blocked by required conditions
Lint / lint (push) Waiting to run
mypy / Run mypy on Lib/_pyrepl (push) Waiting to run
mypy / Run mypy on Lib/test/libregrtest (push) Waiting to run
mypy / Run mypy on Lib/tomllib (push) Waiting to run
mypy / Run mypy on Tools/build (push) Waiting to run
mypy / Run mypy on Tools/cases_generator (push) Waiting to run
mypy / Run mypy on Tools/clinic (push) Waiting to run
mypy / Run mypy on Tools/jit (push) Waiting to run
mypy / Run mypy on Tools/peg_generator (push) Waiting to run
Tail calling interpreter / aarch64-apple-darwin/clang (push) Waiting to run
Tail calling interpreter / aarch64-unknown-linux-gnu/gcc (push) Waiting to run
Tail calling interpreter / x86_64-pc-windows-msvc/msvc (push) Waiting to run
Tail calling interpreter / x86_64-apple-darwin/clang (push) Waiting to run
Tail calling interpreter / free-threading (push) Waiting to run
Tail calling interpreter / x86_64-unknown-linux-gnu/gcc (push) Waiting to run
JIT / Interpreter (Debug) (push) Has been cancelled
JIT / aarch64-apple-darwin/clang (Release) (push) Has been cancelled
JIT / aarch64-unknown-linux-gnu/gcc (Release) (push) Has been cancelled
JIT / aarch64-apple-darwin/clang (Debug) (push) Has been cancelled
JIT / aarch64-unknown-linux-gnu/gcc (Debug) (push) Has been cancelled
JIT / x86_64-apple-darwin/clang (Release) (push) Has been cancelled
JIT / x86_64-unknown-linux-gnu/gcc (Release) (push) Has been cancelled
JIT / x86_64-apple-darwin/clang (Debug) (push) Has been cancelled
JIT / x86_64-unknown-linux-gnu/gcc (Debug) (push) Has been cancelled
JIT / Free-Threaded (Debug) (push) Has been cancelled
JIT / JIT without optimizations (Debug) (push) Has been cancelled
JIT / JIT with tail calling interpreter (push) Has been cancelled
When iterparse() opens a file by filename and is not explicitly closed, emit a ResourceWarning to alert developers of the resource leak. Signed-off-by: Osama Abdelkader <osama.abdelkader@gmail.com> Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
parent
209eaff68c
commit
a486d452c7
5 changed files with 69 additions and 4 deletions
|
|
@ -656,6 +656,10 @@ Functions
|
|||
.. versionchanged:: 3.13
|
||||
Added the :meth:`!close` method.
|
||||
|
||||
.. versionchanged:: next
|
||||
A :exc:`ResourceWarning` is now emitted if the iterator opened a file
|
||||
and is not explicitly closed.
|
||||
|
||||
|
||||
.. function:: parse(source, parser=None)
|
||||
|
||||
|
|
|
|||
|
|
@ -1244,3 +1244,9 @@ that may require changes to your code.
|
|||
|
||||
* :meth:`~mmap.mmap.resize` has been removed on platforms that don't support the
|
||||
underlying syscall, instead of raising a :exc:`SystemError`.
|
||||
|
||||
* Resource warning is now emitted for unclosed
|
||||
:func:`xml.etree.ElementTree.iterparse` iterator if it opened a file.
|
||||
Use its :meth:`!close` method or the :func:`contextlib.closing` context
|
||||
manager to close it.
|
||||
(Contributed by Osama Abdelkader and Serhiy Storchaka in :gh:`140601`.)
|
||||
|
|
|
|||
|
|
@ -1436,17 +1436,39 @@ class IterparseTest(unittest.TestCase):
|
|||
|
||||
def test_resource_warnings_not_exhausted(self):
|
||||
# Not exhausting the iterator still closes the underlying file (bpo-43292)
|
||||
# Not closing before del should emit ResourceWarning
|
||||
it = ET.iterparse(SIMPLE_XMLFILE)
|
||||
with warnings_helper.check_no_resource_warning(self):
|
||||
it.close()
|
||||
del it
|
||||
gc_collect()
|
||||
|
||||
it = ET.iterparse(SIMPLE_XMLFILE)
|
||||
with self.assertWarns(ResourceWarning) as wm:
|
||||
del it
|
||||
gc_collect()
|
||||
# Not 'unclosed file'.
|
||||
self.assertIn('unclosed iterparse iterator', str(wm.warning))
|
||||
self.assertIn(repr(SIMPLE_XMLFILE), str(wm.warning))
|
||||
self.assertEqual(wm.filename, __file__)
|
||||
|
||||
it = ET.iterparse(SIMPLE_XMLFILE)
|
||||
with warnings_helper.check_no_resource_warning(self):
|
||||
action, elem = next(it)
|
||||
it.close()
|
||||
self.assertEqual((action, elem.tag), ('end', 'element'))
|
||||
del it, elem
|
||||
gc_collect()
|
||||
|
||||
it = ET.iterparse(SIMPLE_XMLFILE)
|
||||
with self.assertWarns(ResourceWarning) as wm:
|
||||
action, elem = next(it)
|
||||
self.assertEqual((action, elem.tag), ('end', 'element'))
|
||||
del it, elem
|
||||
gc_collect()
|
||||
self.assertIn('unclosed iterparse iterator', str(wm.warning))
|
||||
self.assertIn(repr(SIMPLE_XMLFILE), str(wm.warning))
|
||||
self.assertEqual(wm.filename, __file__)
|
||||
|
||||
def test_resource_warnings_failed_iteration(self):
|
||||
self.addCleanup(os_helper.unlink, TESTFN)
|
||||
|
|
@ -1461,16 +1483,41 @@ class IterparseTest(unittest.TestCase):
|
|||
next(it)
|
||||
self.assertEqual(str(cm.exception),
|
||||
'junk after document element: line 1, column 12')
|
||||
it.close()
|
||||
del cm, it
|
||||
gc_collect()
|
||||
|
||||
it = ET.iterparse(TESTFN)
|
||||
action, elem = next(it)
|
||||
self.assertEqual((action, elem.tag), ('end', 'document'))
|
||||
with self.assertWarns(ResourceWarning) as wm:
|
||||
with self.assertRaises(ET.ParseError) as cm:
|
||||
next(it)
|
||||
self.assertEqual(str(cm.exception),
|
||||
'junk after document element: line 1, column 12')
|
||||
del cm, it
|
||||
gc_collect()
|
||||
self.assertIn('unclosed iterparse iterator', str(wm.warning))
|
||||
self.assertIn(repr(TESTFN), str(wm.warning))
|
||||
self.assertEqual(wm.filename, __file__)
|
||||
|
||||
def test_resource_warnings_exhausted(self):
|
||||
it = ET.iterparse(SIMPLE_XMLFILE)
|
||||
with warnings_helper.check_no_resource_warning(self):
|
||||
list(it)
|
||||
it.close()
|
||||
del it
|
||||
gc_collect()
|
||||
|
||||
it = ET.iterparse(SIMPLE_XMLFILE)
|
||||
with self.assertWarns(ResourceWarning) as wm:
|
||||
list(it)
|
||||
del it
|
||||
gc_collect()
|
||||
self.assertIn('unclosed iterparse iterator', str(wm.warning))
|
||||
self.assertIn(repr(SIMPLE_XMLFILE), str(wm.warning))
|
||||
self.assertEqual(wm.filename, __file__)
|
||||
|
||||
def test_close_not_exhausted(self):
|
||||
iterparse = ET.iterparse
|
||||
|
||||
|
|
|
|||
|
|
@ -1261,16 +1261,20 @@ def iterparse(source, events=None, parser=None):
|
|||
gen = iterator(source)
|
||||
class IterParseIterator(collections.abc.Iterator):
|
||||
__next__ = gen.__next__
|
||||
|
||||
def close(self):
|
||||
nonlocal close_source
|
||||
if close_source:
|
||||
source.close()
|
||||
close_source = False
|
||||
gen.close()
|
||||
|
||||
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.)
|
||||
def __del__(self, _warn=warnings.warn):
|
||||
if close_source:
|
||||
source.close()
|
||||
try:
|
||||
_warn(f"unclosed iterparse iterator {source.name!r}", ResourceWarning, stacklevel=2)
|
||||
finally:
|
||||
source.close()
|
||||
|
||||
it = IterParseIterator()
|
||||
it.root = None
|
||||
|
|
|
|||
|
|
@ -0,0 +1,4 @@
|
|||
:func:`xml.etree.ElementTree.iterparse` now emits a :exc:`ResourceWarning`
|
||||
when the iterator is not explicitly closed and was opened with a filename.
|
||||
This helps developers identify and fix resource leaks. Patch by Osama
|
||||
Abdelkader.
|
||||
Loading…
Add table
Add a link
Reference in a new issue