python / python/cpython

configparser: whitespace not stripped when writing empty values

Open
#157,466 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib type-feature
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug description:

When writing a configparser.ConfigParser to a file, keys with an empty value add a whitespace at the end of the line.

Eg. a line key = , ending with a whitespace.

import configparser
config = configparser.ConfigParser()
config["DEFAULT"] = {'ok': 'value', 'error': ''}
with open("test.ini", "w") as fhandle:
    config.write(fhandle)

Now the contents of test.ini are:

[DEFAULT]
ok = value
error = 

The line with the key "error" ends in a space. It appears the extra whitespace is introduced in ConfigParser.write(). Whitespaces are added to the delimiter, but this does not account for empty values.

class ConfigParser:
    def write(self, fp, space_around_delimiters=True):
        if space_around_delimiters:
            d = " {} ".format(self._delimiters[0])
        ...

Stripping the whitespace of the value in ConfigParser._write_section() solves this.

class ConfigParser:
    def _write_section(self, fp, section_name, section_items, delimiter, unnamed=False):
        if not unnamed:
            fp.write(f"[{section_name}]\n")
        for key, value in section_items:
            self._validate_key_contents(key)
            value = self._interpolation.before_write(
                self, section_name, key, value
            )
            if value is not None or not self._allow_no_value:
                # Convert all possible line-endings into '\n\t'
                value = (delimiter + str(value).replace('\r\n', '\n')
                         .replace('\r', '\n').replace('\n', '\n\t'))
            else:
                value = ""
            # Change from the original: strip empty space to avoid "key = " for
            # empty keys!!!
            line = f"{key}{value}".strip(" ")
            fp.write(f"{line}\n")
            # Done with changes.
        fp.write("\n")
CPython versions tested on:

3.14

Operating systems tested on:

Linux

Linked PRs
  • gh-157467

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 at ConfigParser.write() and _write_section(), which the issue identifies as the entry points introducing the trailing space. Add coverage for writing an empty value and verify the generated INI line has no trailing whitespace, then run the relevant configparser tests. A linked PR is already noted on the issue.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.