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 read_file() with variable
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Mattia Verga, xtreak
Priority: normal Keywords:

Created on 2020-01-19 12:59 by Mattia Verga, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg360257 - (view) Author: Mattia Verga (Mattia Verga) Date: 2020-01-19 12:59
I'm trying to assign a file object to a variable and then pass this variable to configparse.read_file(), but for some reason that doesn't work:

>>> import configparser
>>> config = configparser.ConfigParser()
>>> config.read_file(open('review-stats.cfg'))
>>> config.sections()
['global']
>>>
>>> config2 = configparser.ConfigParser()
>>> f = open('review-stats.cfg')
>>> f
<_io.TextIOWrapper name='review-stats.cfg' mode='r' encoding='UTF-8'>
>>> config2.read_file(f)
>>> config2.sections()
[]

Shouldn't those results be the same?
msg360259 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2020-01-19 15:23
I can't reproduce this on Linux machine with Python 3.7 and 3.8. Assigning the open file object shouldn't really make a difference unless you are passing the same file object that was read to initialize a different configparser object.

cat review-stats.cfg
[global]
a = b

python3.7
Python 3.7.0 (default, Jun 28 2018, 00:00:00) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import configparser
>>> config = configparser.ConfigParser()
>>> config.read_file(open('review-stats.cfg'))
>>> config.sections()
['global']
>>> config2 = configparser.ConfigParser()
>>> f = open('review-stats.cfg')
>>> f
<_io.TextIOWrapper name='review-stats.cfg' mode='r' encoding='UTF-8'>
>>> config2.read_file(f)
>>> config2.sections()
['global']


python
Python 3.8.0 (default, Nov  6 2019, 21:49:08) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import configparser
>>> config = configparser.ConfigParser()
>>> config.read_file(open('review-stats.cfg'))
>>> config.sections()
['global']
>>> config2 = configparser.ConfigParser()
>>> f = open('review-stats.cfg')
>>> f
<_io.TextIOWrapper name='review-stats.cfg' mode='r' encoding='UTF-8'>
>>> config2.read_file(f)
>>> config2.sections()
['global']
>>> config3 = configparser.ConfigParser()
>>> config3.read_file(f) # This wouldn't work since f.read() is '' and has reached file end.
>>> config3.sections()
[]
>>>
msg360260 - (view) Author: Mattia Verga (Mattia Verga) Date: 2020-01-19 16:08
I started trying to reproduce this with different Python interpreters and I've found it now works as expected also with the original one (BTW it was CPython 3.7.6).
I really don't know why it didn't work before. Sorry for the noise.
History
Date User Action Args
2022-04-11 14:59:25adminsetgithub: 83568
2020-01-19 16:08:33Mattia Vergasetstatus: open -> closed
resolution: not a bug
messages: + msg360260

stage: resolved
2020-01-19 15:23:20xtreaksetnosy: + xtreak
messages: + msg360259
2020-01-19 12:59:22Mattia Vergacreate