Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
Closed
Description
Bug report
Bug description:
If you catchconfigparser.Error when reading a config file (the intention is to skip invalid config files) and then attempt to use the ConfigParser instance, you can get really weird errors. In the following example,read raises DuplicateOptionError from the 2nd section, but attempting toget a value from the first section fails in the interpolation code, because somehow the value has been stored as a list instead of a string!
>>>importconfigparser>>>cp=configparser.ConfigParser()>>>cp.read_string('[a]\nx=1\n[b]\ny=1\ny=2')Traceback (mostrecentcalllast):File"<stdin>",line1,in<module>File"/usr/lib/python3.12/configparser.py",line710,inread_stringself.read_file(sfile,source)File"/usr/lib/python3.12/configparser.py",line705,inread_fileself._read(f,source)File"/usr/lib/python3.12/configparser.py",line1074,in_readraiseDuplicateOptionError(sectname,optname,configparser.DuplicateOptionError:Whilereadingfrom'<string>' [line5]:option'y'insection'b'alreadyexists>>>cp.get('a','x')Traceback (mostrecentcalllast):File"<stdin>",line1,in<module>File"/usr/lib/python3.12/configparser.py",line777,ingetreturnself._interpolation.before_get(self,section,option,value,^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File"/usr/lib/python3.12/configparser.py",line367,inbefore_getself._interpolate_some(parser,option,L,value,section,defaults,1)File"/usr/lib/python3.12/configparser.py",line384,in_interpolate_somep=rest.find("%")^^^^^^^^^AttributeError:'list'objecthasnoattribute'find'
Disabling interpolation can read the value from the 1st section but it's a list, not a str!
>>> cp.get('a', 'x', raw=True)['1']CPython versions tested on:
3.10, 3.12, CPython main branch
Operating systems tested on:
Linux