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

Đang mở Phù hợp với người mới
#7,645 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

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
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python
Lĩnh vực
api

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

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. 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.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của psf/requests

Tất cả issue của psf/requests

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.