classification
Title: Add the close method for ElementTree.iterparse() object
Type: resource usage Stage: needs patch
Components: Library (Lib) Versions: Python 3.6
process
Status: open Resolution:
Dependencies: 25638 Superseder:
Assigned To: serhiy.storchaka Nosy List: ebarry, serhiy.storchaka
Priority: normal Keywords:

Created on 2015-11-23 13:57 by serhiy.storchaka, last changed 2015-11-26 09:40 by serhiy.storchaka.

Messages (4)
msg255159 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-11-23 13:57
If ElementTree.iterparse() is called with file names, it opens a file. When resulting iterator is not exhausted, the file lefts not closed.

>>> import xml.etree.ElementTree as ET
>>> import gc
>>> ET.iterparse('/dev/null')
<xml.etree.ElementTree._IterParseIterator object at 0xb6f9e38c>
>>> gc.collect()
__main__:1: ResourceWarning: unclosed file <_io.BufferedReader name='/dev/null'>
34

Martin Panter proposed in issue25688 to add an explicit way to clean it up, like a generator.close() method.
msg255164 - (view) Author: Emanuel Barry (ebarry) * Date: 2015-11-23 14:17
I am unable to reproduce the issue on Windows 7 with 3.5.0; I have tried opening a small (non-empty) text. Here's the result:


>>> import xml.etree.ElementTree as ET
>>> import gc
>>> ET.iterparse("E:/New.txt")
<xml.etree.ElementTree._IterParseIterator object at 0x0023ABB0>
>>> gc.collect()
59
msg255171 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-11-23 14:56
You have to enable deprecation warnings. Run the interpreter with the -Wa option.
msg255173 - (view) Author: Emanuel Barry (ebarry) * Date: 2015-11-23 15:08
Oh, my bad. Ignore my last message, behaviour is identical then. Thanks for clearing that up.
History
Date User Action Args
2015-11-26 09:40:50serhiy.storchakasetdependencies: + Verify the etree_parse and etree_iterparse benchmarks are working appropriately
2015-11-23 15:08:12ebarrysetmessages: + msg255173
2015-11-23 14:56:23serhiy.storchakasetmessages: + msg255171
2015-11-23 14:17:12ebarrysetnosy: + ebarry
messages: + msg255164
2015-11-23 13:57:25serhiy.storchakacreate