ZipInfo filename is mangled when os.sep is not '/'
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 3/5
- Temps estimé
- 1-2 jours
- Accessibilité débutants
- 45/100
Piste de recherche
Commencez dans Lib/zipfile.py, au niveau de la traduction référencée du nom de fichier de ZipInfo autour des lignes 378-382, et reproduisez l’exemple signalé os.sep='.'. C’est terminé lorsque ZipInfo conserve les noms de fichiers de l’archive avec des séparateurs '/', sans remplacer '.' sur les systèmes dont le séparateur n’est ni '/' ni '\' ; le contenu ne nomme pas de fichier de test.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
Bug report
The ZipInfo object within ZipFile performs an explicit translation of the filename.
I believe this is intended to make it easy to use on Windows where you might pass an explicit pathname to the ZipInfo object creation. On Windows the filesystem separator is commonly \ (although it supports / in many cases), so that this foces the the filename attribute to contain a filename in the unix form.
This logic is used whether the ZipInfo object is created manually (usually to add a new file), or when the filename has been taken from an archive that is being extracted.
However, the logic is broken on systems where the os.sep is anything else other than \ or /. On systems where the os.sep is . this means that if you try to create an archive with a file containing a . extension the filename in the archive will be mangled. On such a system, extracting an archive will also mangle the filename.
To demonstrate this, it is possible to do a very simple command line example:
>>> import zipfile
>>> import os
>>> os.sep = '.'
>>> zipfile.ZipInfo('hello.txt')
<ZipInfo filename='hello/txt' file_size=0>
In the real world, this breaks any possibility of using this module on RISC OS where the filesystem separator in os.sep is .. In the current Python 3 on RISC OS, the ZipFile module will always mangle filenames that have standard extensions.
I believe that the intention of the object is that:
- the
filenameinitialiser on the object and attribute is in unicode format (this has been enforced since Python 3 by the explicit decodes in the archive member reading code). - the
filenameattribute is formed as would be stored in the archive, using/as a directory separator (stated by documentationfilename should be the full name of the archive member). - the
filenameinitialiser on the object is allowed to be supplied a path name on unix and windows systems, as a convenience (the referenced code will have been relied on by existing software).
As such, I believe the referenced code is broken, and to retain the above assumptions and to allow the handling of zip archives on systems where os.sep is not / or \, the code should instead read:
if os.sep == "\\" and os.sep in filename:
filename = filename.replace(os.sep, "/")
This removes the overzealous replacement of os.sep in the creation of the ZipInfo object.
Further problems exist with the from_file method which I shall raise separately.
There are some issues which might be related to this (but this change does not preclude them): https://github.com/python/cpython/issues/90139 and https://github.com/python/cpython/issues/92184.
Your environment
- CPython versions tested on: Python 3.9, 3.10
- Operating system and architecture: On OS X, simulating the problem seen on RISC OS.
- Langage dominant
- Python
- Étoiles
- 77.2k
- Forks
- 36k
- Merge moyen
- 1 j 9 h
- PR mergées (30 j)
- 558
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de python/cpython
-
docs pending
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
-
stdlib type-feature
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
-
stdlib type-feature
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
-
build type-bug
Difficulté 2/5 1-3 heures Accessibilité débutants 76/100
-
stdlib topic-email type-feature
Difficulté 2/5 1-3 heures Accessibilité débutants 70/100
Toutes les issues de python/cpython
Issues similaires
-
Difficulté 1/5 Moins d'une heure Accessibilité débutants 90/100
-
bug
Difficulté 2/5 1-3 heures Accessibilité débutants 86/100
zostera/django-bootstrap4#894 ·
-
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
use-agent-os/agent-os#3276 ·
-
Difficulté 2/5 1-3 heures Accessibilité débutants 88/100
zephyrproject-rtos/zephyr#119726 ·
-
area/auth bug comp/agent P3 platform/discord type/security
Difficulté 2/5 1-3 heures Accessibilité débutants 88/100
NousResearch/hermes-agent#117848 ·