Configparser does not ignore comments
sunnyhasija opened this issue · 3 comments
I have a configuration file of the following type: config.ini
[main]
subreddits = ["PCMasterrace", "emacs"] # list of all the subreddits
[time]
# make sure that the time is in unixtime
#the starting time and ending time are only needed if you want to pass it to the main script. If using crontab, you dont need to.
start = 1584577650 #explicity stating starting time
end = 1584664050 #explicitly stating ending time.
#
in my python file, when I use config parser I get errors:
config.read("config.ini")
SUBREDDITS = json.loads(config.get("main", "subreddits"))
START_TIME = int(config.get("time", "start"))
ACTUAL_END_TIME = int(config.get("time", "end"))
Error is File "/usr/lib64/python3.9/json/decoder.py", line 340, in decode raise JSONDecodeError("Extra data", s, end).
Upon investigation config parser is getting ["PCMasterrace", "emacs"] # list of all the subreddits
including the comment, and then json.loads is tripping over it.
This issue is thus far in python 3.9, and not in 3.8.
The behavior exists with stdlib too. This project is a backport. Upon search it seems this was reported and closed as won't fix : https://bugs.python.org/issue592527
Yes, but that issue was from 2002. The user is reporting the issue is a regression with Python 3.9.
The user's report appears to be invalid. I created a reproducer and the behavior is the same on Python 3.8 and 3.6.
~ $ python3.9 -c "import configparser; cp = configparser.ConfigParser(); cp.read_string('[section]\\nvalue=1234#foo\\n'); print(cp['section']['value'])"
1234#foo
~ $ python3.8 -c "import configparser; cp = configparser.ConfigParser(); cp.read_string('[section]\\nvalue=1234#foo\\n'); print(cp['section']['value'])"
1234#foo
~ $ python3.6 -c "import configparser; cp = configparser.ConfigParser(); cp.read_string('[section]\\nvalue=1234#foo\\n'); print(cp['section']['value'])"
1234#foo