python / python/cpython

`zipfile.ZipFile.writestr()` raises `AttributeError` instead of accepting `os.PathLike` objects (e.g. `pathlib.Path`)

オープン 初心者向け
#153,771 コメント 5 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

stdlib type-feature
主要言語
Python
スター
77.2k
フォーク
35.9k
PR マージ指標
PR 指標を取得中

説明

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

zipfile/init.py の ZipFile.writestr()、ZipInfo.init()、_sanitize_filename() から始め、その後 issue にある pathlib.Path の再現手順を実行します。writestr() が pathlib.Path のような os.PathLike オブジェクトを AttributeError を発生させずに受け付け、既存の文字列に対する動作を維持できれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
backend
issue の種類
バグ
難易度
2/5
見積もり時間
1〜3時間
活発さ
静か
明瞭さ
明確に書かれている
初心者へのやさしさ
72/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。