python / python/cpython

configparser: BasicInterpolation raises TypeError interpolating a value-less option

未關閉
#153,855 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

stdlib type-bug
主要語言
Python
星號
77.2k
分支
35.9k
PR 合併指標
PR 指標待擷取

描述

Bug report

Bug description:

With allow_no_value=True and the default BasicInterpolation, interpolating a reference to a value-less option raises TypeError instead of resolving or raising a configparser error. ExtendedInterpolation treats the missing value as empty in the same situation.

import configparser
cfg = configparser.ConfigParser(allow_no_value=True)
cfg.read_string("[s]\na\nb = x%(a)sy\n")
cfg.get("s", "b")

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    cfg.get("s", "b")
    ~~~~~~~^^^^^^^^^^
  File "../Lib/configparser.py", line 855, in get
    return self._interpolation.before_get(self, section, option, value,
          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                                          d)
                                          ^^
  File "../Lib/configparser.py", line 433, in before_get
    self._interpolate_some(parser, option, L, value, section, defaults, 1)
    ~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "../Lib/configparser.py", line 474, in _interpolate_some
    if "%" in v:
      ^^^^^^^^
TypeError: argument of type 'NoneType' is not a container or iterable

The same pattern with ExtendedInterpolation (b = x${s:a}y) returns "xy".

ExtendedInterpolation._interpolate_some guards this (if v is None: continue, added in gh-130941); BasicInterpolation._interpolate_some does not and reaches if "%" in v: with v set to None (Lib/configparser.py).

Observed on 3.16.0a0.

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs
  • gh-153857

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

從 Lib/configparser.py 中的 BasicInterpolation._interpolate_some 開始,並按照報告中的描述,將其處理方式與 ExtendedInterpolation._interpolate_some 進行比較。確認無值選項的情況不再引發 TypeError,並遵循預期的插值行為;已連結的 PR gh-153857 表明相關工作已經在進行中。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
backend
Issue 類型
缺陷
難度
2/5
預估耗時
1-3 小時
活躍度
停滯
描述清晰度
描述清楚
新手友好度
25/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。