`zipfile.ZipFile.writestr()` raises `AttributeError` instead of accepting `os.PathLike` objects (e.g. `pathlib.Path`)
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 77.2k
- Fork
- 35.9k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
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
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia in zipfile/init.py, su ZipFile.writestr(), ZipInfo.init() e _sanitize_filename(), quindi esegui la riproduzione con pathlib.Path dell'issue. Il lavoro è completato quando writestr() accetta oggetti os.PathLike come pathlib.Path senza sollevare AttributeError, mantenendo il comportamento esistente per le stringhe.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- backend
- Tipo di issue
- Bug
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Stato di attività
- Tranquilla
- Chiarezza
- Specificata chiaramente
- Idoneità per principianti
- 72/100