Issue 28632: configparser does not close files in read (original) (raw)

Created on 2016-11-07 16:49 by PetrPy, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (6)
msg280209 - (view) Author: Petr (PetrPy) Date: 2016-11-07 16:49
When using configparser read method, the file(s) remains opened and cannot be closed, causing ResourceWarning: unclosed file. For example in the following code: config = configparser.ConfigParser() config.read(cfg_fn) ... the file cfg_fn remains opened and is only closed upon destruction of the underlying file object. At some point in history the method read used to close the file, but this has been changed for some reason.
msg280234 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-11-07 20:32
I can't reproduce the issue. Looking at the code it seems to me that the file is always closed. Could you please provide more information Petr?
msg280253 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2016-11-07 22:28
Cannot repro: ``` $ python3 Python 3.5.2 (default, Jul 28 2016, 21:28:00) >>> with open('/tmp/someconfig.py', 'w') as w: ... w.write("""[section] ... option=value ... """) ... 23 >>> import configparser >>> cp = configparser.ConfigParser() >>> cp.read('/tmp/someconfig.py') ['/tmp/someconfig.py'] >>> [CTRL+D] $ ``` If I leave a file unclosed, I get warning: ``` $ python3 Python 3.5.2 (default, Jul 28 2016, 21:28:00) >>> open('/tmp/someconfig.py') <_io.TextIOWrapper name='/tmp/someconfig.py' mode='r' encoding='UTF-8'> >>> [CTRL+D] sys:1: ResourceWarning: unclosed file <_io.TextIOWrapper name='/tmp/someconfig.py' mode='r' encoding='UTF-8'> $ ```
msg280292 - (view) Author: Petr (PetrPy) Date: 2016-11-08 11:18
I am sorry, I can only reproduce it in the production environment so far, it does only occur on Ubuntu Linux (Python 3.5.1) and I am developing on Windows. So right now I cannot narrow it down (it does not occur with simple code, unfortunately). This is what I get after some experimentation: Exception ignored in: <_io.FileIO name='config.ini' mode='rb' closefd=True> ResourceWarning: unclosed file <_io.TextIOWrapper name='config.ini' mode='r' encoding='UTF-8'> The only place when I work with config.ini is the read method of ConfigParser, so it definitely should be an issue in ConfigParser.
msg280627 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2016-11-12 01:12
Given that the open is context managed with open(filename, encoding=encoding) as fp: self._read(fp, filename) how could fp not be closed?
msg280693 - (view) Author: Petr (PetrPy) Date: 2016-11-13 11:59
Thanks for your comments, I am myself quite puzzled how is it possible that the file is not closed after having been read. I suspect this to be an OS problem, therefore I am closing the bug for now.
History
Date User Action Args
2022-04-11 14:58:39 admin set github: 72818
2016-11-13 13:15:11 SilentGhost set stage: resolved
2016-11-13 11:59:11 PetrPy set status: open -> closedresolution: not a bugmessages: +
2016-11-12 01:12:00 terry.reedy set nosy: + terry.reedymessages: +
2016-11-08 11🔞15 PetrPy set messages: +
2016-11-07 22:28:37 lukasz.langa set messages: +
2016-11-07 20:32:09 serhiy.storchaka set nosy: + serhiy.storchakamessages: +
2016-11-07 19:52:09 r.david.murray set nosy: + lukasz.langa
2016-11-07 16:49:02 PetrPy create