`iter_lines(decode_unicode=True, delimiter=<str>)` raises `TypeError` when `Response.encoding` is `None`

未关闭 适合新手
#7,645 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

  • #1 来自 @draarivpatel-ui —— 已关闭,未合并

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
62/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
python
领域
api

调研方向

故障位于 src/requests/models.py 中的 iter_lines,因为 Response.encoding 为 None,chunk 是 bytes,而 chunk.split(delimiter) 接收到的却是 str 类型的分隔符。运行 issue 中的复现代码片段以确认 TypeError,然后检查 decode_unicode=True 时 iter_lines 如何解码 chunk。当调用返回 str 行或抛出清晰的错误时即完成,并在现有 iter_lines 测试旁边添加一个测试。已关闭且未合并的关联 pull request 可能包含之前的尝试,值得先阅读。

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

描述

Expected Result

Either return lines, or raise a clear error message

I spotted this while investigating whether Pyrefly could run on the Requests source code. (happy to make a PR to add Pyrefly if you'd be interested in it by the way, it may avert future issues like this one? looks like only collaboratos can make PRs, so maybe not)

Actual Result

Unexpected error message:

[b'line one', b'line two', b'line three']
Traceback (most recent call last):
  File "/home/mgorelli/scratch/t.py", line 15, in <module>
    print(list(make_response().iter_lines(decode_unicode=True, delimiter="\n")))
          ~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/mgorelli/scratch/.venv/lib/python3.14/site-packages/requests/models.py", line 1016, in iter_lines
    lines = chunk.split(delimiter)  # type: ignore[arg-type]
TypeError: a bytes-like object is required, not 'str'

Reproduction Steps

import io

import requests


def make_response():
    r = requests.models.Response()
    r.status_code = 200
    r.raw = io.BytesIO(b"line one\nline two\nline three")
    r.encoding = None  # e.g. a Content-Type with no charset
    return r


print(list(make_response().iter_lines(decode_unicode=True)))
print(list(make_response().iter_lines(decode_unicode=True, delimiter="\n")))

System Information

$ python -m requests.help
{
  "chardet": {
    "version": null
  },
  "charset_normalizer": {
    "version": "3.5.2"
  },
  "cryptography": {
    "version": ""
  },
  "idna": {
    "version": "3.20"
  },
  "implementation": {
    "name": "CPython",
    "version": "3.14.4"
  },
  "platform": {
    "release": "6.6.87.2-microsoft-standard-WSL2",
    "system": "Linux"
  },
  "pyOpenSSL": {
    "openssl_version": "",
    "version": null
  },
  "requests": {
    "version": "2.34.2"
  },
  "system_ssl": {
    "version": "30500060"
  },
  "urllib3": {
    "version": "2.8.0"
  },
  "using_charset_normalizer": true,
  "using_pyopenssl": false
}
主要语言
Python
星标
54.7k
派生
10.8k
平均合并
18 分钟
30 天内合并 PR
1

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

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

psf/requests 的其他 Issue

查看 psf/requests 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

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