Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

configparser.DuplicateOptionError leaves ConfigParser instance in bad state #116957

Closed
drothlis opened this issue Mar 18, 2024 · 1 comment · Fixed by #116958
Closed

configparser.DuplicateOptionError leaves ConfigParser instance in bad state #116957

drothlis opened this issue Mar 18, 2024 · 1 comment · Fixed by #116958
Labels
type-bug An unexpected behavior, bug, or error

Comments

@drothlis
Copy link
Contributor

drothlis commented Mar 18, 2024

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

@drothlis
Copy link
Contributor Author

This behaviour is because ConfigParser._read doesn't call _join_multiline_values when it raises DuplicateSectionError or DuplicateOptionError (but it does for ParsingError):

cpython/Lib/configparser.py

Lines 1059 to 1062 in 43c9d61

self._join_multiline_values()
# if any parsing errors occurred, raise an exception
if e:
raise e

drothlis added a commit to drothlis/cpython that referenced this issue Mar 18, 2024
…OptionError

If you catch DuplicateOptionError / DuplicateSectionError when reading a
config file (the intention is to skip invalid config files) and then
attempt to use the ConfigParser instance, any values it *had* read
successfully so far, were stored as a list instead of string! Later
`get` calls would raise "AttributeError: 'list' object has no attribute
'find'" from somewhere deep in the interpolation code.
drothlis added a commit to drothlis/cpython that referenced this issue Mar 18, 2024
serhiy-storchaka pushed a commit that referenced this issue Mar 19, 2024
…Error (GH-116958)

If you catch DuplicateOptionError / DuplicateSectionError when reading a
config file (the intention is to skip invalid config files) and then
attempt to use the ConfigParser instance, any values it *had* read
successfully so far, were stored as a list instead of string! Later
`get` calls would raise "AttributeError: 'list' object has no attribute
'find'" from somewhere deep in the interpolation code.
drothlis added a commit to drothlis/cpython that referenced this issue Mar 19, 2024
…plicateOptionError (pythonGH-116958)

If you catch DuplicateOptionError / DuplicateSectionError when reading a
config file (the intention is to skip invalid config files) and then
attempt to use the ConfigParser instance, any values it *had* read
successfully so far, were stored as a list instead of string! Later
`get` calls would raise "AttributeError: 'list' object has no attribute
'find'" from somewhere deep in the interpolation code.
(cherry picked from commit b1bc375)

Co-authored-by: David Röthlisberger <david@rothlis.net>
drothlis added a commit to drothlis/cpython that referenced this issue Mar 19, 2024
…plicateOptionError (pythonGH-116958)

If you catch DuplicateOptionError / DuplicateSectionError when reading a
config file (the intention is to skip invalid config files) and then
attempt to use the ConfigParser instance, any values it *had* read
successfully so far, were stored as a list instead of string! Later
`get` calls would raise "AttributeError: 'list' object has no attribute
'find'" from somewhere deep in the interpolation code.
(cherry picked from commit b1bc375)

Co-authored-by: David Röthlisberger <david@rothlis.net>
ambv pushed a commit that referenced this issue Mar 19, 2024
…eOptionError (GH-116958) (GH-117013)

If you catch DuplicateOptionError / DuplicateSectionError when reading a
config file (the intention is to skip invalid config files) and then
attempt to use the ConfigParser instance, any values it *had* read
successfully so far, were stored as a list instead of string! Later
`get` calls would raise "AttributeError: 'list' object has no attribute
'find'" from somewhere deep in the interpolation code.

(cherry picked from commit b1bc375)
ambv pushed a commit that referenced this issue Mar 19, 2024
…eOptionError (GH-116958) (GH-117012)

If you catch DuplicateOptionError / DuplicateSectionError when reading a
config file (the intention is to skip invalid config files) and then
attempt to use the ConfigParser instance, any values it *had* read
successfully so far, were stored as a list instead of string! Later
`get` calls would raise "AttributeError: 'list' object has no attribute
'find'" from somewhere deep in the interpolation code.

(cherry picked from commit b1bc375)
vstinner pushed a commit to vstinner/cpython that referenced this issue Mar 20, 2024
…OptionError (pythonGH-116958)

If you catch DuplicateOptionError / DuplicateSectionError when reading a
config file (the intention is to skip invalid config files) and then
attempt to use the ConfigParser instance, any values it *had* read
successfully so far, were stored as a list instead of string! Later
`get` calls would raise "AttributeError: 'list' object has no attribute
'find'" from somewhere deep in the interpolation code.
adorilson pushed a commit to adorilson/cpython that referenced this issue Mar 25, 2024
…OptionError (pythonGH-116958)

If you catch DuplicateOptionError / DuplicateSectionError when reading a
config file (the intention is to skip invalid config files) and then
attempt to use the ConfigParser instance, any values it *had* read
successfully so far, were stored as a list instead of string! Later
`get` calls would raise "AttributeError: 'list' object has no attribute
'find'" from somewhere deep in the interpolation code.
diegorusso pushed a commit to diegorusso/cpython that referenced this issue Apr 17, 2024
…OptionError (pythonGH-116958)

If you catch DuplicateOptionError / DuplicateSectionError when reading a
config file (the intention is to skip invalid config files) and then
attempt to use the ConfigParser instance, any values it *had* read
successfully so far, were stored as a list instead of string! Later
`get` calls would raise "AttributeError: 'list' object has no attribute
'find'" from somewhere deep in the interpolation code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant