Skip to content

configparser.DuplicateOptionError leaves ConfigParser instance in bad state #116957

Closed

Description

@drothlis

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

Activity

  1. drothlis commented on Mar 18, 2024

    @drothlis
    ContributorAuthor

    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

  2. added 2 commits that reference this issue on Mar 18, 2024
  3. added a commit that references this issue on Mar 19, 2024
  4. added 2 commits that reference this issue on Mar 19, 2024
  5. added 2 commits that reference this issue on Mar 19, 2024
  6. added a commit that references this issue on Mar 20, 2024
  7. added a commit that references this issue on Mar 25, 2024
  8. added a commit that references this issue on Apr 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions