`zipfile`: file type issues
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:
Hey.
I think there exist a number of issues (which may, depending on how code is used, in principle even be security relevant).
First, AFAIU, ZIP files may contain (at least) regular files, directories (as "standalone" items in the archive, like an empty directory) and symbolic links.
For example using the zip program:
$ mkdir empty
$ zip d.zip empty
adding: empty/ (stored 0%)
$ ln -s /dev/null bar
$ zip --symlinks s.zip bar
adding: bar (stored 0%)
$
When I open the one with the symlink in Python:
>>> import zipfile
>>> z = zipfile.ZipFile("s.zip", "r")
>>> z.namelist()
['bar']
>>> f = z.open("bar", "r")
>>> f.read()
b'/dev/null'
>>> i = z.getinfo("bar")
>>> i.is_dir()
False
>>> p = zipfile.Path(z, "bar")
>>> p.filename
PosixPath('s.zip/bar')
>>> p.is_dir()
False
>>> p.is_file()
True
>>> p.is_symlink()
True
>>>
- It's IMO debatable whether
z.open("<symlink>", "r")should succeed or not. IMOzipfile.ZipFile.open()quite clearly is ZIP'sopen(), but that AFAIK, never opens symlinks but only follows them. - Even if that behaviour is desired (i.e. like a
os.readlink()for ZIPs), then it's still completely unexpected and the ZipFile object has nois_symlink()-method ... (onlyzipfile.Pathhas such, so one needs to create that first, which seems quite unhandy). - Speaking of which
zipfile.Path’sis_dir(),is_file()andis_symlink()functions seem either buggy or semantically inconsistent and/or badly documented.
Usually, "file" means either any type of file (directory, symlink, device, etc.) or regular files (and sometimes also symlinks if they point to regular files).
Here, the symlink points to nothing, so it cannot be the latter case. Also - see below - a directory wouldn't returnTrueforis_file(), so the it's not the former either.
The docs also don't meantion what "file" means.
Now the same with d.zip:
>>> import zipfile
>>> z = zipfile.ZipFile("d.zip", "r")
>>> z.namelist()
['empty/']
>>> f = z.open("empty/", "r")
>>> f.read()
b''
>>> i = archive_file.getinfo("empty/")
>>> i.is_dir()
True
>>> p = zipfile.Path(z,"empty/")
>>> p.filename
PosixPath('d.zip/empty')
>>> p.is_dir()
True
>>> p.is_file()
False
>>> p.is_symlink()
False
>>>
- IMO, that
zipfile.ZipFile.open()succeeds on a directory (and gives an emptybytes) seems pretty strange at best. It does so even if the directory isn't empty but contains files. - There's also that thing that sometimes that directory pathnames are suffixed by
/and sometimes not. Maybe I've missed it but that doesn't seem to be documented, but may be crucial when e.g. matching filenames - and is IMO unexpected. - As mentioned above,
is_file()here isFalse, which would imply that the meaning of that function should be that a file is either a regular file or a regular file or a symbolic link pointing to such.
Cheers,
Chris.
CPython versions tested on:
3.13
Operating systems tested on:
Linux
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 riproducendo gli esempi di Python 3.13 con zipfile.ZipFile.open(), ZipInfo.is_dir() e zipfile.Path.is_dir(), is_file() e is_symlink(). Leggi la documentazione e i test correlati di zipfile, quindi stabilisci la semantica prevista per directory, file regolari, collegamenti simbolici e barre finali prima di definire i criteri di completamento.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- operating-systems
- Tipo di issue
- Bug
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Da chiarire
- Idoneità per principianti
- 25/100