python / python/cpython

Forbid shlex.quote returning a quoted empty string (`”””`) for falsey non-`str` data

未关闭
#149,280 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

stdlib type-feature
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

Bug report

Bug description:

Full discussion and history of the bug is on discuss.python.org.

tldr:

shlex.quote's opening check for the empty string as a special case is still placed before the isinstance type check. This is intentional as when the isinstance check was added, the patch was intended to not break compatibility with eg people expecting shlex.quote(None) to return "''", implicitly coercing None to the empty string "" and then quoting it as "''".

def quote(s):
    if not s:
        return "''"

    if not isinstance(s, str):
        raise TypeError(f"expected string object, got {type(s).__name__!r}")
    ...

We think either of the following is a best course of action:

  • Print a DeprecationWarning when one calls quote on falsey non-str data
  • Leave the function alone, as people may now be relying on this as a feature, not a bug, and we don't gain much by doing a patch.

Or we could do a hard-break and just swap the order of the isinstance and if not s checks around, which would break anybody relying on None -> "''" (not recommended!).


I have a couple of git tags that I will link below with reference implementation for 'fixing' this issue (if it needs to be fixed). I just need the gh- number of this issue first for the blurb entries.

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

阅读 shlex.quote 当前对空字符串和类型的检查,然后查看链接的讨论和兼容性历史。该 issue 尚未准备好进行具体实现:在能够定义完成标准之前,maintainer 必须决定是发出警告、保留现有行为,还是进行彻底的不兼容变更。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
cli
Issue 类型
缺陷
难度
5/5
预计耗时
一周以上
活跃度
冷清
描述清晰度
需要澄清
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。