This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: configparser does not close files in read
Type: resource usage Stage: resolved
Components: Library (Lib) Versions: Python 3.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: PetrPy, lukasz.langa, serhiy.storchaka, terry.reedy
Priority: normal Keywords:

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:39adminsetgithub: 72818
2016-11-13 13:15:11SilentGhostsetstage: resolved
2016-11-13 11:59:11PetrPysetstatus: open -> closed
resolution: not a bug
messages: + msg280693
2016-11-12 01:12:00terry.reedysetnosy: + terry.reedy
messages: + msg280627
2016-11-08 11:18:15PetrPysetmessages: + msg280292
2016-11-07 22:28:37lukasz.langasetmessages: + msg280253
2016-11-07 20:32:09serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg280234
2016-11-07 19:52:09r.david.murraysetnosy: + lukasz.langa
2016-11-07 16:49:02PetrPycreate