Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zipfile module doesn't replace os.altsep in filenames in some cases #92184

Closed
pR0Ps opened this issue May 2, 2022 · 2 comments · Fixed by #92185
Closed

Zipfile module doesn't replace os.altsep in filenames in some cases #92184

pR0Ps opened this issue May 2, 2022 · 2 comments · Fixed by #92185
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@pR0Ps
Copy link
Contributor

pR0Ps commented May 2, 2022

The zipfile module currently doesn't replace os.altsep with / in filename in cases where os.altsep is not None and not "/".

I'm not currently aware of any cases where os.altsep is not None or "/", so I don't think this is currently causing any issues, but it's at least not consistent with the documentation which states that os.altsep is "An alternative character used by the operating system to separate pathname components".

To reproduce the issue, the code below manually sets os.sep and os.altsep to be the normal Windows values, then swaps them. This should have no effect on the resulting zip file since the values should both be treated as valid path separators.

import io
import ntpath
import os
import zipfile

def show_zip():
    zf = zipfile.ZipFile(io.BytesIO(), 'w')
    zf.writestr("a/b/", "")
    zf.writestr("a/b\\", "")
    zf.writestr("a\\b/", "")
    zf.writestr("a\\b\\", "")
    print([x.filename for x in zf.infolist()])

os.sep = ntpath.sep
os.altsep = ntpath.altsep
show_zip()

os.altsep, os.sep = os.sep, os.altsep
show_zip()

Expected output:

['a/b/', 'a/b/', 'a/b/', 'a/b/']
['a/b/', 'a/b/', 'a/b/', 'a/b/']

Actual output:

['a/b/', 'a/b/', 'a/b/', 'a/b/']
['a/b/', 'a/b\\', 'a\\b/', 'a\\b\\']

Environment: Python 3.11.0a7+ (heads/main:56f9844014, May 2 2022, 15:22:18) [Clang 11.0.0 (clang-1100.0.33.17)] on darwin

Linked PRs

@gpshead
Copy link
Member

gpshead commented May 11, 2023

i'm not convinced this was really worthwhile... given there are no known applicable platforms, but the logic in the PR was trivial enough and is pedantically correct for consistency. It is set to automerge. Thanks!

gpshead pushed a commit that referenced this issue May 11, 2023
… objects (#92185)

This causes the zipfile module to also consider the character defined by
`os.altsep` (if there is one) to be a path separator and convert it to a
forward slash, as defined by the zip specification.

A logical no-op on all known platforms today as os.altsep is currently only set to a meaningful value on Windows (where it is "/").
carljm added a commit to carljm/cpython that referenced this issue May 11, 2023
* main: (27 commits)
  pythongh-87849: fix SEND specialization family definition (pythonGH-104268)
  pythongh-101819: Adapt _io.IOBase.seek and _io.IOBase.truncate to Argument Clinic (python#104384)
  pythongh-101819: Adapt _io._Buffered* methods to Argument Clinic (python#104367)
  pythongh-101819: Refactor `_io` futher in preparation for module isolation (python#104369)
  pythongh-101819: Adapt _io.TextIOBase methods to Argument Clinic (python#104383)
  pythongh-101117: Improve accuracy of sqlite3.Cursor.rowcount docs (python#104287)
  pythonGH-92184: Convert os.altsep to '/' in filenames when creating ZipInfo objects (python#92185)
  pythongh-104357: fix inlined comprehensions that close over iteration var (python#104368)
  pythonGH-90208: Suppress OSError exceptions from `pathlib.Path.glob()` (pythonGH-104141)
  pythonGH-102181: Improve specialization stats for SEND (pythonGH-102182)
  pythongh-103000: Optimise `dataclasses.asdict` for the common case (python#104364)
  pythongh-103538: Remove unused TK_AQUA code (pythonGH-103539)
  pythonGH-87695: Fix OSError from `pathlib.Path.glob()` (pythonGH-104292)
  pythongh-104263: Rely on Py_NAN and introduce Py_INFINITY (pythonGH-104202)
  pythongh-104010: Separate and improve docs for `typing.get_origin` and `typing.get_args` (python#104013)
  pythongh-101819: Adapt _io._BufferedIOBase_Type methods to Argument Clinic (python#104355)
  pythongh-103960: Dark mode: invert image brightness (python#103983)
  pythongh-104252: Immortalize Py_EMPTY_KEYS (pythongh-104253)
  pythongh-101819: Clean up _io windows console io after pythongh-104197 (python#104354)
  pythongh-101819: Harden _io init (python#104352)
  ...
carljm added a commit to carljm/cpython that referenced this issue May 11, 2023
* main:
  pythongh-87849: fix SEND specialization family definition (pythonGH-104268)
  pythongh-101819: Adapt _io.IOBase.seek and _io.IOBase.truncate to Argument Clinic (python#104384)
  pythongh-101819: Adapt _io._Buffered* methods to Argument Clinic (python#104367)
  pythongh-101819: Refactor `_io` futher in preparation for module isolation (python#104369)
  pythongh-101819: Adapt _io.TextIOBase methods to Argument Clinic (python#104383)
  pythongh-101117: Improve accuracy of sqlite3.Cursor.rowcount docs (python#104287)
  pythonGH-92184: Convert os.altsep to '/' in filenames when creating ZipInfo objects (python#92185)
  pythongh-104357: fix inlined comprehensions that close over iteration var (python#104368)
  pythonGH-90208: Suppress OSError exceptions from `pathlib.Path.glob()` (pythonGH-104141)
@pR0Ps
Copy link
Contributor Author

pR0Ps commented May 11, 2023

I'm not convinced this was really worthwhile

Yeah, I'm with you there. It was more just something I noticed while fixing something else and figured I'd make a quick drive-by commit to fix. Thanks for merging it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

3 participants