Skip to content

configparser: reading an invalid file triggers ParsingError *and* mutations #95546

Open

Description

@asottile

Bug report

A clear and concise description of what the bug is.
Include a minimal, reproducible example (https://stackoverflow.com/help/minimal-reproducible-example), if possible.

Your environment

  • CPython versions tested on: 3.11.0b5
  • Operating system and architecture: x86_64

here is a simple example:

>>> import configparser
>>> cfg = configparser.ConfigParser()
>>> cfg.read_string('[foo]\nx = 1\n...\n')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.11/configparser.py", line 738, in read_string
    self.read_file(sfile, source)
  File "/usr/lib/python3.11/configparser.py", line 733, in read_file
    self._read(f, source)
  File "/usr/lib/python3.11/configparser.py", line 1131, in _read
    raise e
configparser.ParsingError: Source contains parsing errors: '<string>'
	[line  3]: '...\n'
>>> cfg['foo']['x']
'1'

I expect reading to be an all-or-nothing operation -- if the file contains a syntax error it shouldn't modify the ConfigParser object with half of the file's contents

Activity

  1. PurityLake commented on Aug 2, 2022

    @PurityLake
    Contributor
    # a non-fatal parsing error occurred. set up the
    # exception but keep going. the exception will be
    # raised at the end of the file and will contain a
    # list of all bogus lines

    Apparently, the ParsingError is considered non-fatal so it continues. If you use:

    cfg.read_string('[foo]\nx = 1\n...\ny = 2')

    y can also be retrieved.

    I don't feel raising the error after the fact makes much sense either but it is done with the idea that you are shown all the lines that are wrong instead of having to run the config as many times are there are errors.

  2. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Aug 2, 2022
  3. drothlis commented on Mar 18, 2024

    @drothlis
    Contributor

    Furthermore, if you catch configparser.Error (the intention is to skip invalid config files) and then attempt to use the ConfigParser instance, you can get really weird errors. In this 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'
    

    Reproduced with Python 3.10 and 3.12.

    P.S. 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']
    
  4. drothlis commented on Mar 18, 2024

    @drothlis
    Contributor

    The behaviour in my previous comment 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

    Maybe this should go in a finally block? Or modify the method so that DuplicateSectionError and DuplicateOptionError aren't raised immediately, like the existing handling of ParsingErrors?

  5. drothlis commented on Mar 18, 2024

    @drothlis
    Contributor

    I have raised #116957 for the bug I described in my 2 previous comments, which is about Duplicate{Section,Option}Error rather than ParsingError.

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

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions