From d7bd7140e4861ba1f2b0200b7b0842fc7c8bc0f3 Mon Sep 17 00:00:00 2001 From: Ruben Vorderman Date: Fri, 19 Jul 2024 06:58:36 +0200 Subject: [PATCH 1/4] Replace compress function with simpler CPython 3.13 variant --- src/zlib_ng/gzip_ng.py | 38 ++++++++------------------------------ 1 file changed, 8 insertions(+), 30 deletions(-) diff --git a/src/zlib_ng/gzip_ng.py b/src/zlib_ng/gzip_ng.py index 24b5512..769698f 100644 --- a/src/zlib_ng/gzip_ng.py +++ b/src/zlib_ng/gzip_ng.py @@ -180,42 +180,20 @@ def write(self, data): _GzipNGReader = _GzipReader -def _create_simple_gzip_header(compresslevel: int, - mtime=None) -> bytes: - """ - Write a simple gzip header with no extra fields. - :param compresslevel: Compresslevel used to determine the xfl bytes. - :param mtime: The mtime (must support conversion to a 32-bit integer). - :return: A bytes object representing the gzip header. - """ - if mtime is None: - mtime = time.time() - if compresslevel == _COMPRESS_LEVEL_BEST: - xfl = 2 - elif compresslevel == _COMPRESS_LEVEL_FAST: - xfl = 4 - else: - xfl = 0 - # Pack ID1 and ID2 magic bytes, method (8=deflate), header flags (no extra - # fields added to header), mtime, xfl and os (255 for unknown OS). - return struct.pack(" Date: Fri, 19 Jul 2024 07:07:49 +0200 Subject: [PATCH 2/4] Simplify decompress function --- src/zlib_ng/gzip_ng.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/zlib_ng/gzip_ng.py b/src/zlib_ng/gzip_ng.py index 769698f..d98a49f 100644 --- a/src/zlib_ng/gzip_ng.py +++ b/src/zlib_ng/gzip_ng.py @@ -200,8 +200,7 @@ def decompress(data): """Decompress a gzip compressed string in one shot. Return the decompressed string. """ - fp = io.BytesIO(data) - reader = _GzipReader(fp, max(len(data), 16)) + reader = _GzipReader(data) return reader.readall() From 4edc533f7a675b2870b787f8b2ed8e206eaac2f1 Mon Sep 17 00:00:00 2001 From: Ruben Vorderman Date: Fri, 19 Jul 2024 07:08:58 +0200 Subject: [PATCH 3/4] Use shutil.copyfileobj for fileobj copying --- src/zlib_ng/gzip_ng.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/zlib_ng/gzip_ng.py b/src/zlib_ng/gzip_ng.py index d98a49f..f24bdbe 100644 --- a/src/zlib_ng/gzip_ng.py +++ b/src/zlib_ng/gzip_ng.py @@ -22,6 +22,7 @@ import gzip import io import os +import shutil import struct import sys import time @@ -302,11 +303,7 @@ def main(): global READ_BUFFER_SIZE READ_BUFFER_SIZE = args.buffer_size try: - while True: - block = in_file.read(args.buffer_size) - if block == b"": - break - out_file.write(block) + shutil.copyfileobj(in_file, out_file, args.buffer_size) finally: if in_file is not sys.stdin.buffer: in_file.close() From e918f3e611dd80a57a01833e34d4893b415a9116 Mon Sep 17 00:00:00 2001 From: Ruben Vorderman Date: Fri, 19 Jul 2024 09:21:11 +0200 Subject: [PATCH 4/4] Update changelog with simplification changes --- CHANGELOG.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index e68a8e4..76712e3 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -12,6 +12,8 @@ version 0.5.0-dev + Wheels are now build for MacOS arm64 architectures. + Fix a bug where READ and WRITE in zlib_ng.gzip_ng were inconsistent with the values in gzip on Python 3.13 ++ Small simplifications to the ``gzip_ng.compress`` and ``gzip_ng.decompress`` + functions, which should lead to less overhead. version 0.4.3 -----------------