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 accepts delimiters in key name which corrupts .ini file #128843

Open
marc-hb opened this issue Jan 14, 2025 · 0 comments
Open

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

marc-hb opened this issue Jan 14, 2025 · 0 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@marc-hb
Copy link

marc-hb commented Jan 14, 2025

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 ...:

zephyrproject-rtos/west#779

CPython versions tested on:

3.13

Operating systems tested on:

Linux

cc: @jaraco

@marc-hb marc-hb added the type-bug An unexpected behavior, bug, or error label Jan 14, 2025
@picnixz picnixz added the stdlib Python modules in the Lib dir label Jan 14, 2025
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

2 participants