`zipfile.ZipFile.writestr()` raises `AttributeError` instead of accepting `os.PathLike` objects (e.g. `pathlib.Path`)
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 77.2k
- Fork
- 35.9k
- Chỉ số merge pull request
- Chỉ số pull request đang chờ
Mô tả
Bug report
Bug description:
Unlike ZipFile.write() (which accepts os.PathLike objects such as pathlib.Path for both its filename and arcname arguments), ZipFile.writestr() fails when passed a PathLike object as its zinfo_or_arcname parameter.
Instead of converting it to a string using os.fspath() (as expected by modern standard library conventions), it attempts to call .find(chr(0)) directly on the path object, resulting in an immediate AttributeError.
Repro:
import zipfile
import pathlib
import io
z = zipfile.ZipFile(io.BytesIO(), 'w')
# Passes a pathlib.Path instead of a raw string
z.writestr(pathlib.Path('foo.txt'), b'hello')
Traceback:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File ".../zipfile/__init__.py", line 2020, in writestr
zinfo = ZipInfo(zinfo_or_arcname)._for_archive(self)
^^^^^^^^^^^^^^^^^^^^^^^^^
File ".../zipfile/__init__.py", line 455, in __init__
filename = _sanitize_filename(filename)
File ".../zipfile/__init__.py", line 410, in _sanitize_filename
null_byte = filename.find(chr(0))
^^^^^^^^^^^^^
AttributeError: 'PosixPath' object has no attribute 'find'
CPython versions tested on:
3.14
Operating systems tested on:
macOS
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.
Hướng nghiên cứu
Bắt đầu trong zipfile/init.py tại ZipFile.writestr(), ZipInfo.init() và _sanitize_filename(), sau đó chạy phần tái hiện với pathlib.Path từ issue. Hoàn tất khi writestr() chấp nhận các đối tượng os.PathLike như pathlib.Path mà không phát sinh AttributeError, đồng thời vẫn giữ nguyên hành vi hiện có với chuỗi.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- python
- Lĩnh vực
- backend
- Loại issue
- Lỗi
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức độ hoạt động
- Ít trao đổi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức phù hợp với người mới
- 72/100