Skip to content

Commit 20be6ba

Browse files
authored
gh-132983: Introduce compression package and move _compression module (GH-133018)
* Introduces `compression` package for https://peps.python.org/pep-0784/ This commit introduces the `compression` package, specified in PEP 784 to re-export the `lzma`, `bz2`, `gzip`, and `zlib` modules. Introduction of `compression.zstd` will be completed in a future commit once the `_zstd` module is merged. This commit also moves the `_compression` private module to `compression._common._streams`. * Re-exports existing module docstrings.
1 parent 6d53b75 commit 20be6ba

File tree

12 files changed

+42
-22
lines changed

12 files changed

+42
-22
lines changed

Lib/bz2.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
__author__ = "Nadeem Vawda <[email protected]>"
1111

1212
from builtins import open as _builtin_open
13+
from compression._common import _streams
1314
import io
1415
import os
15-
import _compression
1616

1717
from _bz2 import BZ2Compressor, BZ2Decompressor
1818

@@ -23,7 +23,7 @@
2323
_MODE_WRITE = 3
2424

2525

26-
class BZ2File(_compression.BaseStream):
26+
class BZ2File(_streams.BaseStream):
2727

2828
"""A file object providing transparent bzip2 (de)compression.
2929
@@ -88,7 +88,7 @@ def __init__(self, filename, mode="r", *, compresslevel=9):
8888
raise TypeError("filename must be a str, bytes, file or PathLike object")
8989

9090
if self._mode == _MODE_READ:
91-
raw = _compression.DecompressReader(self._fp,
91+
raw = _streams.DecompressReader(self._fp,
9292
BZ2Decompressor, trailing_error=OSError)
9393
self._buffer = io.BufferedReader(raw)
9494
else:
@@ -248,7 +248,7 @@ def writelines(self, seq):
248248
249249
Line separators are not added between the written byte strings.
250250
"""
251-
return _compression.BaseStream.writelines(self, seq)
251+
return _streams.BaseStream.writelines(self, seq)
252252

253253
def seek(self, offset, whence=io.SEEK_SET):
254254
"""Change the file position.

Lib/compression/__init__.py

Whitespace-only changes.

Lib/_compression.py renamed to Lib/compression/_common/_streams.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Internal classes used by the gzip, lzma and bz2 modules"""
1+
"""Internal classes used by compression modules"""
22

33
import io
44
import sys

Lib/compression/bz2/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import bz2
2+
__doc__ = bz2.__doc__
3+
del bz2
4+
5+
from bz2 import *

Lib/compression/gzip/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import gzip
2+
__doc__ = gzip.__doc__
3+
del gzip
4+
5+
from gzip import *

Lib/compression/lzma/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import lzma
2+
__doc__ = lzma.__doc__
3+
del lzma
4+
5+
from lzma import *

Lib/compression/zlib/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import zlib
2+
__doc__ = zlib.__doc__
3+
del zlib
4+
5+
from zlib import *

Lib/gzip.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
# based on Andrew Kuchling's minigzip.py distributed with the zlib module
77

8-
import _compression
98
import builtins
109
import io
1110
import os
@@ -14,6 +13,7 @@
1413
import time
1514
import weakref
1615
import zlib
16+
from compression._common import _streams
1717

1818
__all__ = ["BadGzipFile", "GzipFile", "open", "compress", "decompress"]
1919

@@ -144,7 +144,7 @@ def writable(self):
144144
return True
145145

146146

147-
class GzipFile(_compression.BaseStream):
147+
class GzipFile(_streams.BaseStream):
148148
"""The GzipFile class simulates most of the methods of a file object with
149149
the exception of the truncate() method.
150150
@@ -523,7 +523,7 @@ def _read_gzip_header(fp):
523523
return last_mtime
524524

525525

526-
class _GzipReader(_compression.DecompressReader):
526+
class _GzipReader(_streams.DecompressReader):
527527
def __init__(self, fp):
528528
super().__init__(_PaddedFile(fp), zlib._ZlibDecompressor,
529529
wbits=-zlib.MAX_WBITS)

Lib/lzma.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
import builtins
2525
import io
2626
import os
27+
from compression._common import _streams
2728
from _lzma import *
2829
from _lzma import _encode_filter_properties, _decode_filter_properties # noqa: F401
29-
import _compression
3030

3131

3232
# Value 0 no longer used
@@ -35,7 +35,7 @@
3535
_MODE_WRITE = 3
3636

3737

38-
class LZMAFile(_compression.BaseStream):
38+
class LZMAFile(_streams.BaseStream):
3939

4040
"""A file object providing transparent LZMA (de)compression.
4141
@@ -127,7 +127,7 @@ def __init__(self, filename=None, mode="r", *,
127127
raise TypeError("filename must be a str, bytes, file or PathLike object")
128128

129129
if self._mode == _MODE_READ:
130-
raw = _compression.DecompressReader(self._fp, LZMADecompressor,
130+
raw = _streams.DecompressReader(self._fp, LZMADecompressor,
131131
trailing_error=LZMAError, format=format, filters=filters)
132132
self._buffer = io.BufferedReader(raw)
133133

Lib/test/test_bz2.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from test.support import import_helper
1717
from test.support import threading_helper
1818
from test.support.os_helper import unlink, FakePath
19-
import _compression
19+
from compression._common import _streams
2020
import sys
2121

2222

@@ -126,15 +126,15 @@ def testReadMultiStream(self):
126126
def testReadMonkeyMultiStream(self):
127127
# Test BZ2File.read() on a multi-stream archive where a stream
128128
# boundary coincides with the end of the raw read buffer.
129-
buffer_size = _compression.BUFFER_SIZE
130-
_compression.BUFFER_SIZE = len(self.DATA)
129+
buffer_size = _streams.BUFFER_SIZE
130+
_streams.BUFFER_SIZE = len(self.DATA)
131131
try:
132132
self.createTempFile(streams=5)
133133
with BZ2File(self.filename) as bz2f:
134134
self.assertRaises(TypeError, bz2f.read, float())
135135
self.assertEqual(bz2f.read(), self.TEXT * 5)
136136
finally:
137-
_compression.BUFFER_SIZE = buffer_size
137+
_streams.BUFFER_SIZE = buffer_size
138138

139139
def testReadTrailingJunk(self):
140140
self.createTempFile(suffix=self.BAD_DATA)
@@ -742,7 +742,7 @@ def testOpenPathLikeFilename(self):
742742
def testDecompressLimited(self):
743743
"""Decompressed data buffering should be limited"""
744744
bomb = bz2.compress(b'\0' * int(2e6), compresslevel=9)
745-
self.assertLess(len(bomb), _compression.BUFFER_SIZE)
745+
self.assertLess(len(bomb), _streams.BUFFER_SIZE)
746746

747747
decomp = BZ2File(BytesIO(bomb))
748748
self.assertEqual(decomp.read(1), b'\0')

Lib/test/test_lzma.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import _compression
21
import array
32
from io import BytesIO, UnsupportedOperation, DEFAULT_BUFFER_SIZE
43
import os
@@ -7,6 +6,7 @@
76
import sys
87
from test import support
98
import unittest
9+
from compression._common import _streams
1010

1111
from test.support import _4G, bigmemtest
1212
from test.support.import_helper import import_module
@@ -861,13 +861,13 @@ def test_read_multistream(self):
861861
def test_read_multistream_buffer_size_aligned(self):
862862
# Test the case where a stream boundary coincides with the end
863863
# of the raw read buffer.
864-
saved_buffer_size = _compression.BUFFER_SIZE
865-
_compression.BUFFER_SIZE = len(COMPRESSED_XZ)
864+
saved_buffer_size = _streams.BUFFER_SIZE
865+
_streams.BUFFER_SIZE = len(COMPRESSED_XZ)
866866
try:
867867
with LZMAFile(BytesIO(COMPRESSED_XZ * 5)) as f:
868868
self.assertEqual(f.read(), INPUT * 5)
869869
finally:
870-
_compression.BUFFER_SIZE = saved_buffer_size
870+
_streams.BUFFER_SIZE = saved_buffer_size
871871

872872
def test_read_trailing_junk(self):
873873
with LZMAFile(BytesIO(COMPRESSED_XZ + COMPRESSED_BOGUS)) as f:
@@ -1066,7 +1066,7 @@ def test_readlines(self):
10661066
def test_decompress_limited(self):
10671067
"""Decompressed data buffering should be limited"""
10681068
bomb = lzma.compress(b'\0' * int(2e6), preset=6)
1069-
self.assertLess(len(bomb), _compression.BUFFER_SIZE)
1069+
self.assertLess(len(bomb), _streams.BUFFER_SIZE)
10701070

10711071
decomp = LZMAFile(BytesIO(bomb))
10721072
self.assertEqual(decomp.read(1), b'\0')

Python/stdlib_module_names.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)