Skip to content

ConfigParser accepts delimiters in key name which corrupts .ini file #128843

@marc-hb

Description

@marc-hb

Bug report

Bug description:

Since very first commit 26d513c 15 years ago, configparser.rst has stated:

https://docs.python.org/3.13/library/configparser.html#customizing-parser-behaviour

Delimiters are substrings that delimit keys from values within a section. The first occurrence of a delimiting substring on a line is considered a delimiter. This means values (BUT NOT KEYS) can contain substrings that are in the delimiters".

(emphasis mine)

But as of today's 3.13, nothing stops the user from using delimiters in keys, writing them to a file and finally reading back something completely different. Reproduction:

import configparser

cfg2file = configparser.ConfigParser()
cfg2file['section1'] = {}
# This should fail. It "succeeds".
cfg2file['section1']['one=two'] = 'three'

# Write ambiguous 'one = two = three' line in the .ini file
with open('example.ini', 'w') as configfile:
  cfg2file.write(configfile)


# Read the file back. First equal sign wins.
file2cfg = configparser.ConfigParser()
file2cfg.read('example.ini')


for key in file2cfg['section1']:
    print(key + ' is: ' + file2cfg['section1'][key] )

# True
assert cfg2file['section1']['one=two'] == 'three'
# False!
assert file2cfg['section1']['one=two'] == 'three'

# This is True instead
assert file2cfg['section1']['one'] == 'two = three'

This happens when someone enters west config key=val ... instead of west config key val ...:

CPython versions tested on:

3.13

Operating systems tested on:

Linux

cc: @jaraco

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