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.
diff --git a/Lib/bz2.py b/Lib/bz2.py
index 21e8ff4..a499ca3 100644
--- a/Lib/bz2.py
+++ b/Lib/bz2.py
@@ -24,8 +24,6 @@
# Value 2 no longer used
_MODE_WRITE = 3
-_sentinel = object()
-
class BZ2File(_compression.BaseStream):
@@ -38,7 +36,7 @@
returned as bytes, and data to be written should be given as bytes.
"""
- def __init__(self, filename, mode="r", buffering=_sentinel, compresslevel=9):
+ def __init__(self, filename, mode="r", *, compresslevel=9):
"""Open a bzip2-compressed file.
If filename is a str, bytes, or PathLike object, it gives the
@@ -65,12 +63,6 @@
self._closefp = False
self._mode = _MODE_CLOSED
- if buffering is not _sentinel:
- warnings.warn("Use of 'buffering' argument is deprecated and ignored "
- "since Python 3.0.",
- DeprecationWarning,
- stacklevel=2)
-
if not (1 <= compresslevel <= 9):
raise ValueError("compresslevel must be between 1 and 9")
diff --git a/Lib/test/test_bz2.py b/Lib/test/test_bz2.py
index eb2f72e..030d564 100644
--- a/Lib/test/test_bz2.py
+++ b/Lib/test/test_bz2.py
@@ -100,6 +100,9 @@
self.assertRaises(ValueError, BZ2File, os.devnull, compresslevel=0)
self.assertRaises(ValueError, BZ2File, os.devnull, compresslevel=10)
+ # compresslevel is keyword-only
+ self.assertRaises(TypeError, BZ2File, os.devnull, "r", 3)
+
def testRead(self):
self.createTempFile()
with BZ2File(self.filename) as bz2f: