python / python/cpython

Tidy and optimize `zipfile` module

Open
#152,140 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib type-feature
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Feature or enhancement

Proposal:

Tidy and optimize zipfile module internal pipelines

This issue serves as a tracking umbrella to modernize and optimize zipfile's internal data-handling pipelines—specifically targeting dead/suboptimal code cleanup, memory allocation reduction via memoryview/bytearray, and structural simplification without altering public APIs.


Task 1: Replace _Extra class with a ZipFile._strip_extra_fields() static method

The _Extra class is over-engineered for its active responsibilities. Its only operational usage in the entire module is its strip() method, called exclusively by ZipFile._write_end_record() to provide the stripping logic for ZIP64 fields.

The context-dependent nature of extra fields also makes it difficult to be reused by _decodeExtra() or other methods efficiently. Additionally, its split() classmethod explicitly calls _Extra directly rather than utilizing cls, which introduces an unneeded hardcoded class coupling that hinders clean extensibility.

Proposed Vector

Remove the _Extra class entirely and reimplement its stripping logic as a private static method _strip_extra_fields() that processes a bytearray inside ZipFile, positioned directly beneath its caller. This eliminates dead and suboptimal code, achieves clean encapsulation and code locality, and improves performance by avoiding temporary class allocations.

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

#134999

Linked PRs
  • gh-152141

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the zipfile module's _Extra class and ZipFile._write_end_record(), which are the specific code paths named in the proposal. Check linked PR gh-152141 before starting; done means removing the targeted _Extra usage, preserving public APIs, and retaining ZIP64 extra-field stripping behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.