Skip to content

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

Closed

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

Activity

  1. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Jan 14, 2025
  2. vadmium commented on Jan 15, 2025

    @vadmium
    Member

    Issue #65697 is about keys that look like comments, but also mentions keys with an equals sign
    Issue #69909 is also similar, but focusses on add_section

  3. jaraco commented on Jan 15, 2025

    @jaraco
    Member

    I'm going to close this as a duplicate of #65697.

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