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: reading an invalid file triggers ParsingError *and* mutations #95546

Open
asottile opened this issue Aug 1, 2022 · 4 comments
Open
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@asottile
Copy link
Contributor

asottile commented Aug 1, 2022

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

@asottile asottile added the type-bug An unexpected behavior, bug, or error label Aug 1, 2022
@PurityLake
Copy link
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.

@drothlis
Copy link
Contributor

drothlis commented Mar 18, 2024

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']

@drothlis
Copy link
Contributor

drothlis commented Mar 18, 2024

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?

@drothlis
Copy link
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
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

4 participants