python / python/cpython

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

Open
#95,546 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib type-bug
Dominant language
Python
Stars
77.2k
Forks
36k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the configparser.py implementation shown in the traceback and reproduce the read_string example from the issue. Trace how _read handles valid lines before raising ParsingError, then add or locate focused tests for the failure case. Done means an invalid input raises ParsingError without leaving partial contents in the ConfigParser object.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.