Skip to content

Commit 9baf242

Browse files
authored
bpo-39357: Remove buffering parameter of bz2.BZ2File (GH-18028)
Remove the buffering parameter of bz2.BZ2File. Since Python 3.0, it was ignored and using it was emitting a DeprecationWarning. Pass an open file object to control how the file is opened. The compresslevel parameter becomes keyword-only.
1 parent c5b7900 commit 9baf242

File tree

5 files changed

+26
-15
lines changed

5 files changed

+26
-15
lines changed

Doc/library/bz2.rst

+8-6
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ All of the classes in this module may safely be accessed from multiple threads.
6565
Accepts a :term:`path-like object`.
6666

6767

68-
.. class:: BZ2File(filename, mode='r', buffering=None, compresslevel=9)
68+
.. class:: BZ2File(filename, mode='r', *, compresslevel=9)
6969

7070
Open a bzip2-compressed file in binary mode.
7171

@@ -81,8 +81,6 @@ All of the classes in this module may safely be accessed from multiple threads.
8181
If *filename* is a file object (rather than an actual file name), a mode of
8282
``'w'`` does not truncate the file, and is instead equivalent to ``'a'``.
8383

84-
The *buffering* argument is ignored. Its use is deprecated since Python 3.0.
85-
8684
If *mode* is ``'w'`` or ``'a'``, *compresslevel* can be an integer between
8785
``1`` and ``9`` specifying the level of compression: ``1`` produces the
8886
least compression, and ``9`` (default) produces the most compression.
@@ -110,9 +108,6 @@ All of the classes in this module may safely be accessed from multiple threads.
110108
.. versionadded:: 3.3
111109

112110

113-
.. deprecated:: 3.0
114-
The keyword argument *buffering* was deprecated and is now ignored.
115-
116111
.. versionchanged:: 3.1
117112
Support for the :keyword:`with` statement was added.
118113

@@ -138,6 +133,13 @@ All of the classes in this module may safely be accessed from multiple threads.
138133
.. versionchanged:: 3.6
139134
Accepts a :term:`path-like object`.
140135

136+
.. versionchanged:: 3.9
137+
The *buffering* parameter has been removed. It was ignored and deprecated
138+
since Python 3.0. Pass an open file object to control how the file is
139+
opened.
140+
141+
The *compresslevel* parameter became keyword-only.
142+
141143

142144
Incremental (de)compression
143145
---------------------------

Doc/whatsnew/3.9.rst

+10
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,12 @@ Removed
420420
3.5 (:issue:`22486`): use :func:`math.gcd` instead.
421421
(Contributed by Victor Stinner in :issue:`39350`.)
422422

423+
* The *buffering* parameter of :class:`bz2.BZ2File` has been removed. Since
424+
Python 3.0, it was ignored and using it was emitting
425+
:exc:`DeprecationWarning`. Pass an open file object to control how the file
426+
is opened.
427+
(Contributed by Victor Stinner in :issue:`39357`.)
428+
423429

424430
Porting to Python 3.9
425431
=====================
@@ -451,6 +457,10 @@ Changes in the Python API
451457
:data:`~errno.EBADF` error.
452458
(Contributed by Victor Stinner in :issue:`39239`.)
453459

460+
* The *compresslevel* parameter of :class:`bz2.BZ2File` became keyword-only,
461+
since the *buffering* parameter has been removed.
462+
(Contributed by Victor Stinner in :issue:`39357`.)
463+
454464

455465
CPython bytecode changes
456466
------------------------

Lib/bz2.py

+1-9
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
# Value 2 no longer used
2525
_MODE_WRITE = 3
2626

27-
_sentinel = object()
28-
2927

3028
class BZ2File(_compression.BaseStream):
3129

@@ -38,7 +36,7 @@ class BZ2File(_compression.BaseStream):
3836
returned as bytes, and data to be written should be given as bytes.
3937
"""
4038

41-
def __init__(self, filename, mode="r", buffering=_sentinel, compresslevel=9):
39+
def __init__(self, filename, mode="r", *, compresslevel=9):
4240
"""Open a bzip2-compressed file.
4341
4442
If filename is a str, bytes, or PathLike object, it gives the
@@ -65,12 +63,6 @@ def __init__(self, filename, mode="r", buffering=_sentinel, compresslevel=9):
6563
self._closefp = False
6664
self._mode = _MODE_CLOSED
6765

68-
if buffering is not _sentinel:
69-
warnings.warn("Use of 'buffering' argument is deprecated and ignored "
70-
"since Python 3.0.",
71-
DeprecationWarning,
72-
stacklevel=2)
73-
7466
if not (1 <= compresslevel <= 9):
7567
raise ValueError("compresslevel must be between 1 and 9")
7668

Lib/test/test_bz2.py

+3
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ def testBadArgs(self):
100100
self.assertRaises(ValueError, BZ2File, os.devnull, compresslevel=0)
101101
self.assertRaises(ValueError, BZ2File, os.devnull, compresslevel=10)
102102

103+
# compresslevel is keyword-only
104+
self.assertRaises(TypeError, BZ2File, os.devnull, "r", 3)
105+
103106
def testRead(self):
104107
self.createTempFile()
105108
with BZ2File(self.filename) as bz2f:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Remove the *buffering* parameter of :class:`bz2.BZ2File`. Since Python 3.0, it
2+
was ignored and using it was emitting :exc:`DeprecationWarning`. Pass an open
3+
file object, to control how the file is opened. The *compresslevel* parameter
4+
becomes keyword-only.

0 commit comments

Comments
 (0)