io: TextIOWrapper silently corrupts output when encoder is a no-op
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đá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
- 75/100
Hướng nghiên cứu
Đọc _io_TextIOWrapper_write_impl trong Modules/_io/textio.c, đặc biệt là phần kiểm tra được liên kết trong báo cáo, và xem các bài kiểm thử TextIOWrapper gần đó về kiểu giá trị trả về của bộ mã hóa. Tái hiện trường hợp bộ mã hóa passthrough trong issue, sau đó thêm bài kiểm thử hồi quy và xác minh rằng đầu vào không phải ASCII gây ra TypeError đã nêu, trong khi đường xử lý nhanh ASCII vẫn hoạt động.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Bug report
Summary
When an encoder returns the input str itself, TextIOWrapper.write treats it as ASCII and writes the str's internal storage to the file. For non-ASCII text the file is silently corrupted.
Expected: TypeError: encoder should return a bytes object, not 'str'.
Reproducer
We use a 44-character string that ends in an emoji, so CPython internally stores it as four bytes per character.
import codecs
class Enc(codecs.IncrementalEncoder):
def encode(self, text, final=False):
return text
info = codecs.CodecInfo(None, None, incrementalencoder=Enc,
incrementaldecoder=codecs.getincrementaldecoder("utf-8"),
name="passthrough")
codecs.register(lambda n: info if n == "passthrough" else None)
with open("out.txt", "w", encoding="passthrough") as f:
f.write("The quick brown fox jumps over the lazy dog🐍")
print(open("out.txt").read())
The quick b
Only the first 44 bytes (out of 44*4 = 176) were written to the file.
Root cause
Modules/_io/textio.c, in _io_TextIOWrapper_write_impl:
The exemption for b == text is meant for the ASCII fast path, which sets b = text only for ASCII text.
A Python encoder that returns its input also satisfies it, with any text.
Suggested fix
Tie the exemption to what the fast path relies on, that the text is ASCII:
if (!PyBytes_Check(b) && !(b == text && PyUnicode_IS_ASCII(text))) {
With this the reproducer raises the TypeError above.
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
- Ngôn ngữ chính
- Python
- Star
- 77.9k
- Fork
- 37.1k
- Merge trung bình
- 17 giờ 43 phút
- Pull request đã merge (30 ngày)
- 675
Chuẩn bị môi trường
Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.
- Không có Dockerfile hay tệp Docker Compose
- 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 python/cpython
-
OS-windows performance stdlib type-feature
Độ 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
-
OS-windows performance stdlib type-feature
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
Maintainer thường phản hồi trong vòng 1 ngày
-
build OS-unsupported OS-windows type-bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 60/100
Maintainer thường phản hồi trong vòng 1 ngày
-
What's New in 3.12 credits the unstable C API tier to PEP 697 instead of PEP 689Có thể đã có người làm @TheLouisMahdi đã nhận 4 ngày trước. Đang mởdocs
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 82/100
python/cpython#161078 · 3 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
OS-windows type-bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của python/cpython
Issue tương tự
-
docs size:S
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
Maintainer thường phản hồi trong vòng 1 ngày
-
documentation
Độ 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 74/100
strawberry-graphql/strawberry#4794 ·
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 78/100
dataforgoodfr/cetolog#21 ·
-
security_citations_test fails against ecc 2026.11.6: SECURITY.md quotes ecc v2026.11.2's linesĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
btclib-org/btclib#2932 ·
Maintainer thường phản hồi trong vòng 1 ngày