-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
Closed
Closed
Copy link
Labels
type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Bug report
Bug description:
If you catch configparser.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 to get
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!
>>> import configparser
>>> cp = configparser.ConfigParser()
>>> cp.read_string('[a]\nx=1\n[b]\ny=1\ny=2')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.12/configparser.py", line 710, in read_string
self.read_file(sfile, source)
File "/usr/lib/python3.12/configparser.py", line 705, in read_file
self._read(f, source)
File "/usr/lib/python3.12/configparser.py", line 1074, in _read
raise DuplicateOptionError(sectname, optname,
configparser.DuplicateOptionError: While reading from '<string>' [line 5]: option 'y' in section 'b' already exists
>>> cp.get('a', 'x')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.12/configparser.py", line 777, in get
return self._interpolation.before_get(self, section, option, value,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.12/configparser.py", line 367, in before_get
self._interpolate_some(parser, option, L, value, section, defaults, 1)
File "/usr/lib/python3.12/configparser.py", line 384, in _interpolate_some
p = rest.find("%")
^^^^^^^^^
AttributeError: 'list' object has no attribute '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
Linked PRs
Metadata
Metadata
Assignees
Labels
type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error