python / python/cpython

configparser: whitespace not stripped when writing empty values

Đang mở
#157,466 2 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

stdlib type-feature
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu từ ConfigParser.write() và _write_section(), được issue xác định là các điểm vào làm phát sinh khoảng trắng ở cuối. Bổ sung coverage cho việc ghi một giá trị rỗng và xác minh rằng dòng INI được tạo ra không có khoảng trắng ở cuối, sau đó chạy các test configparser liên quan. Một PR được liên kết đã được ghi chú trong issue.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
tooling
Loại issue
Lỗi
Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
25/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.