configparser: whitespace not stripped when writing empty values
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 77.2k
- フォーク
- 35.9k
- PR マージ指標
- PR 指標を取得中
説明
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
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
ConfigParser.write() と _write_section() から始めます。issue では、これらが末尾の空白を導入するエントリポイントとして特定されています。空の値を書き込む場合のカバレッジを追加し、生成された INI 行に末尾の空白がないことを確認してから、関連する configparser テストを実行します。リンクされた PR はすでに issue に記載されています。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- tooling
- issue の種類
- バグ
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 活発さ
- 停滞
- 明瞭さ
- 明確に書かれている
- 初心者へのやさしさ
- 25/100