gh-141968: Use take_bytes in zipfile._ZipDecrypter by cmaloney · Pull Request #142240 · python/cpython · GitHub
Skip to content

gh-141968: Use take_bytes in zipfile._ZipDecrypter - #142240

Closed
cmaloney wants to merge 1 commit into
python:mainfrom
cmaloney:zipfile_take_bytes
Closed

gh-141968: Use take_bytes in zipfile._ZipDecrypter#142240
cmaloney wants to merge 1 commit into
python:mainfrom
cmaloney:zipfile_take_bytes

Conversation

@cmaloney

@cmaloney cmaloney commented Dec 3, 2025

Copy link
Copy Markdown
Contributor

Removes a copy going from bytearray to bytes.

Removes a copy going from bytearray to bytes.
@cmaloney cmaloney changed the title gh-141968: Use take_byes in zipfile._ZipDecrypter gh-141968: Use take_bytes in zipfile._ZipDecrypter Dec 15, 2025
@cmaloney

Copy link
Copy Markdown
Contributor Author

@serhiy-storchaka

Copy link
Copy Markdown
Member

Does it cause a measurable difference?

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think this has a measurable effect, but at least it does not make it worse.

@cmaloney

cmaloney commented Jan 8, 2026

Copy link
Copy Markdown
Contributor Author

@cosmicexplorer cosmicexplorer left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just checked and we do not have any test cases for zip files using zipcrypto which are larger larger than 1K in the zip crate either, btw (see https://github.com/zip-rs/zip2/tree/master/tests/).

As a general comment on this corner of the zip format: I've never seen anyone actually using the zip format's cryptography extensions in the wild, and it has pretty severe cryptographic flaws, so I don't think it's terribly important to optimize. I would personally look to discourage its use.

However, by way of comparison, I can see that we do indeed make sure to avoid copies in our io::Read implementation of encrypted zip file entries in the zip crate: https://github.com/zip-rs/zip2/blob/5fcfad0bdfa587351a2c6f9b322c5dccbfc51aff/src/read.rs#L130-L157

impl<R: Read + ?Sized> Read for CryptoReader<'_, R> {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        match self {
            CryptoReader::Plaintext(r) => r.read(buf),
            CryptoReader::ZipCrypto(r) => r.read(buf),
            #[cfg(feature = "aes-crypto")]
            CryptoReader::Aes { reader: r, .. } => r.read(buf),
        }
    }


    fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
        match self {
            CryptoReader::Plaintext(r) => r.read_to_end(buf),
            CryptoReader::ZipCrypto(r) => r.read_to_end(buf),
            #[cfg(feature = "aes-crypto")]
            CryptoReader::Aes { reader: r, .. } => r.read_to_end(buf),
        }
    }


    fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
        match self {
            CryptoReader::Plaintext(r) => r.read_to_string(buf),
            CryptoReader::ZipCrypto(r) => r.read_to_string(buf),
            #[cfg(feature = "aes-crypto")]
            CryptoReader::Aes { reader: r, .. } => r.read_to_string(buf),
        }
    }
}

So this PR generally seems like the right thing to do. However, if we're looking to optimize zip performance in cpython more generally (i.e. for use cases besides the niche and broken cryptographic support), I would be very interested in contributing to that effort.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants