|
38 | 38 |
|
39 | 39 | current_dumpfile_suppressions = [] |
40 | 40 |
|
| 41 | +def _iterparse(filename, events): |
| 42 | + # Open the file here so it is closed even when the caller stops iterating |
| 43 | + # early. Python 3.15 warns about unclosed iterparse iterators otherwise. |
| 44 | + with open(filename, 'rb') as f: |
| 45 | + yield from ElementTree.iterparse(f, events=events) |
| 46 | + |
| 47 | + |
41 | 48 | def _load_location(location, element): |
42 | 49 | """Load location from element/dict""" |
43 | 50 | location.file = element.get('file') |
@@ -1237,7 +1244,7 @@ def __init__(self, filename): |
1237 | 1244 | # Parse general configuration options from <dumps> node |
1238 | 1245 | # We intentionally don't clean node resources here because we |
1239 | 1246 | # want to serialize in memory only small part of the XML tree. |
1240 | | - for event, node in ElementTree.iterparse(self.filename, events=('start', 'end')): |
| 1247 | + for event, node in _iterparse(self.filename, events=('start', 'end')): |
1241 | 1248 | if platform_done and rawtokens_done and suppressions_done: |
1242 | 1249 | break |
1243 | 1250 | if node.tag == 'dumps': |
@@ -1296,7 +1303,7 @@ def iterconfigurations(self): |
1296 | 1303 | # Use iterable objects to traverse XML tree for dump files incrementally. |
1297 | 1304 | # Iterative approach is required to avoid large memory consumption. |
1298 | 1305 | # Calling .clear() is necessary to let the element be garbage collected. |
1299 | | - for event, node in ElementTree.iterparse(self.filename, events=('start', 'end')): |
| 1306 | + for event, node in _iterparse(self.filename, events=('start', 'end')): |
1300 | 1307 | # Serialize new configuration node |
1301 | 1308 | if node.tag == 'dump': |
1302 | 1309 | if event == 'start': |
|
0 commit comments