`iter_lines(decode_unicode=True, delimiter=<str>)` raises `TypeError` when `Response.encoding` is `None`
Chưa có ai nhận issue này.
- #1 của @draarivpatel-ui — đã đóng, không merge
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 62/100
Hướng nghiên cứu
Lỗi nằm ở iter_lines trong src/requests/models.py, nơi chunk.split(delimiter) nhận một delimiter kiểu str trong khi chunk là bytes vì Response.encoding là None. Hãy chạy đoạn mã tái hiện trong issue để xác nhận TypeError, sau đó kiểm tra cách iter_lines giải mã các chunk khi decode_unicode=True. Công việc hoàn tất khi lời gọi trả về các dòng str hoặc ném ra một lỗi rõ ràng, và có một test được thêm cạnh các test iter_lines hiện có. Pull request liên kết đã đóng và chưa được merge có thể cho thấy một lần thử trước đó, nên đọc trước.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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
}
- Ngôn ngữ chính
- Python
- Star
- 54.7k
- Fork
- 10.8k
- Merge trung bình
- 18 phút
- Pull request đã merge (30 ngày)
- 1
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của psf/requests
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 85/100
-
get_encoding_from_headers treats any content type containing 'text' as text/* (ISO-8859-1)Có thể đã có người làm @suyashgitt đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
-
Invalid `params` type (e.g. int) raises misleading "TypeError: Cannot mix str and non-str arguments"Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
OCA/account-invoicing#2529 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
area/ci
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 62/100
Maintainer thường phản hồi trong vòng 1 ngày
-
ImageGen generate-batch aborts valid JSONL jobs containing a Unicode line separator in poster textĐang mởbug imagen skills
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Email validation accepts a trailing newlineCó thể đã có người làm @marcello-pichini đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
Maintainer thường phản hồi trong vòng 1 ngày