configparser: whitespace-ending delimiter with an empty value cannot be parsed (write() output is unreadable)
Personne n'a encore pris cette issue.
- Langage dominant
- Python
- Étoiles
- 77.2k
- Forks
- 35.9k
- Métriques de merge des PR
- Métriques de PR en attente
Description
Bug report
The whitespace-in-delimiters feature added in gh-156353 (3.16) cannot parse an option that has an empty value when the delimiter ends in whitespace — including its own write() output.
_read matches option lines against line.clean, which is val.strip() (both sides) with comments removed. For a whitespace delimiter, the delimiter is the trailing whitespace, so strip() erases it and OPTCRE no longer matches.
Reproduction (on main, 3.16.0a0)
import configparser, io
# (1) plain read — rejected, though the option regex itself matches "key ":
cp = configparser.ConfigParser(delimiters=(' ',))
cp.read_string("[s]\nkey \n") # -> ParsingError (expected: {'key': ''})
# (2) round-trip — write() emits a line read_string() then refuses to parse:
w = configparser.ConfigParser(delimiters=(' ',)); w['s'] = {'key': ''}
buf = io.StringIO(); w.write(buf)
print(repr(buf.getvalue())) # '[s]\nkey \n\n'
configparser.ConfigParser(delimiters=(' ',)).read_string(buf.getvalue()) # ParsingError
A serializer that cannot read its own output is a clear self-inconsistency. Empty values are a supported, first-class feature for every other delimiter (key=, key:, key->, key|| all read back as {'key': ''}), so whitespace delimiters should behave the same.
Bug class
Any delimiter whose string ends in whitespace combined with an empty (or all-whitespace) value: ' ', '\t', ' ', 'x ', '= ' all raise; delimiters without trailing whitespace ('->', ' x') work.
Why it's not caught
The gh-156353 tests (test_space_delimiter, test_any_delimiter) only use non-empty values, so this edge is unpinned.
Fix
Match the option regex against a form that preserves trailing whitespace (the delimiter). optval is already stripped after the match, so ordinary values keep no trailing whitespace and the other uses of line.clean are unaffected. I have a small patch + tests and will open a PR referencing this issue.
Found with AI assistance; I've reproduced and verified the behaviour and fix on a locally built interpreter and understand them.
Linked PRs
- gh-157457
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez par ConfigParser._read et la couverture existante de test_space_delimiter et test_any_delimiter décrite dans l’issue. Reproduisez les cas de valeurs vides avec read_string() et write(), puis étendez les tests afin que les délimiteurs se terminant par des espaces blancs et les valeurs vides soient analysés de manière cohérente et que le round trip réussisse.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- python
- Domaine
- tooling
- Type d'issue
- Bug
- Difficulté
- 3/5
- Temps estimé
- 1-2 jours
- Activité
- À l'abandon
- Clarté
- Clairement spécifiée
- Accessibilité débutants
- 25/100