Skip to content

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

Closed
@pR0Ps

Description

@pR0Ps

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions