`iter_lines(decode_unicode=True, delimiter=<str>)` raises `TypeError` when `Response.encoding` is `None`
还没有人认领这个 Issue。
- #1 来自 @draarivpatel-ui —— 已关闭,未合并
评估
调研方向
故障位于 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 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
psf/requests 的其他 Issue
-
难度 1/5 1 小时以内 新手友好度 85/100
-
get_encoding_from_headers treats any content type containing 'text' as text/* (ISO-8859-1)可能已有人在做 @suyashgitt 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 76/100
-
难度 2/5 1-3 小时 新手友好度 78/100
-
Invalid `params` type (e.g. int) raises misleading "TypeError: Cannot mix str and non-str arguments"未关闭
难度 2/5 1-3 小时 新手友好度 72/100
-
难度 2/5 1-3 小时 新手友好度 70/100
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 62/100
OCA/account-invoicing#2529 ·
维护者通常 1 天内回复
-
area/ci
难度 1/5 1 小时以内 新手友好度 62/100
维护者通常 1 天内回复
-
ImageGen generate-batch aborts valid JSONL jobs containing a Unicode line separator in poster text未关闭bug imagen skills
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
Email validation accepts a trailing newline可能已有人在做 @marcello-pichini 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 76/100
维护者通常 1 天内回复