]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-43292: Fix file leak in `ET.iterparse()` when not exhausted (GH-31696)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 7 Mar 2022 10:31:52 +0000 (02:31 -0800)
committerGitHub <noreply@github.com>
Mon, 7 Mar 2022 10:31:52 +0000 (02:31 -0800)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
(cherry picked from commit 496c428de3318c9c5770937491b71dc3d3f18a6a)

Co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com>
Lib/test/test_xml_etree.py
Lib/xml/etree/ElementTree.py
Misc/ACKS
Misc/NEWS.d/next/Library/2022-03-05-09-43-53.bpo-25707.gTlclP.rst [new file with mode: 0644]

index c5292b5e9ef68c95f24958facf37ee8e2c052a5b..5a34d848b3944cd2fd78938a094574a799f1485c 100644 (file)
@@ -658,6 +658,14 @@ class ElementTreeTest(unittest.TestCase):
                     'junk after document element: line 1, column 12')
             del cm, it
 
+        # Not exhausting the iterator still closes the resource (bpo-43292)
+        with warnings_helper.check_no_resource_warning(self):
+            it = iterparse(TESTFN)
+            del it
+
+        with self.assertRaises(FileNotFoundError):
+            iterparse("nonexistent")
+
     def test_writefile(self):
         elem = ET.Element("tag")
         elem.text = "text"
index e9409fd29a11572d51cdb0d3f41016f7a6348b0e..07be8609b83d6ab6f8e8eccf5e9836d9a2d23566 100644 (file)
@@ -1248,8 +1248,14 @@ def iterparse(source, events=None, parser=None):
     # Use the internal, undocumented _parser argument for now; When the
     # parser argument of iterparse is removed, this can be killed.
     pullparser = XMLPullParser(events=events, _parser=parser)
-    def iterator():
+
+    def iterator(source):
+        close_source = False
         try:
+            if not hasattr(source, "read"):
+                source = open(source, "rb")
+                close_source = True
+            yield None
             while True:
                 yield from pullparser.read_events()
                 # load event buffer
@@ -1265,16 +1271,12 @@ def iterparse(source, events=None, parser=None):
                 source.close()
 
     class IterParseIterator(collections.abc.Iterator):
-        __next__ = iterator().__next__
+        __next__ = iterator(source).__next__
     it = IterParseIterator()
     it.root = None
     del iterator, IterParseIterator
 
-    close_source = False
-    if not hasattr(source, "read"):
-        source = open(source, "rb")
-        close_source = True
-
+    next(it)
     return it
 
 
index e1079456009880c25aae897b4650dadacadc84bd..c9bf79d035fcf13c9459a3c507824d4a32c28279 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1870,6 +1870,7 @@ Wojtek Walczak
 Charles Waldman
 Richard Walker
 Larry Wall
+Jacob Walls
 Kevin Walzer
 Rodrigo Steinmuller Wanderley
 Dingyuan Wang
diff --git a/Misc/NEWS.d/next/Library/2022-03-05-09-43-53.bpo-25707.gTlclP.rst b/Misc/NEWS.d/next/Library/2022-03-05-09-43-53.bpo-25707.gTlclP.rst
new file mode 100644 (file)
index 0000000..a59f0a7
--- /dev/null
@@ -0,0 +1,2 @@
+Fixed a file leak in :func:`xml.etree.ElementTree.iterparse` when the
+iterator is not exhausted. Patch by Jacob Walls.