Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8bdd448

Browse files
committed
Issue python#28225: bz2 module now supports pathlib
Initial patch by Ethan Furman.
1 parent 03020cf commit 8bdd448

4 files changed

Lines changed: 25 additions & 7 deletions

File tree

Doc/library/bz2.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ All of the classes in this module may safely be accessed from multiple threads.
6161
.. versionchanged:: 3.4
6262
The ``'x'`` (exclusive creation) mode was added.
6363

64+
.. versionchanged:: 3.6
65+
Accepts a :term:`path-like object`.
66+
6467

6568
.. class:: BZ2File(filename, mode='r', buffering=None, compresslevel=9)
6669

@@ -128,6 +131,9 @@ All of the classes in this module may safely be accessed from multiple threads.
128131
The :meth:`~io.BufferedIOBase.read` method now accepts an argument of
129132
``None``.
130133

134+
.. versionchanged:: 3.6
135+
Accepts a :term:`path-like object`.
136+
131137

132138
Incremental (de)compression
133139
---------------------------

Lib/bz2.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
from builtins import open as _builtin_open
1313
import io
14+
import os
1415
import warnings
1516
import _compression
1617

@@ -42,9 +43,9 @@ class BZ2File(_compression.BaseStream):
4243
def __init__(self, filename, mode="r", buffering=None, compresslevel=9):
4344
"""Open a bzip2-compressed file.
4445
45-
If filename is a str or bytes object, it gives the name
46-
of the file to be opened. Otherwise, it should be a file object,
47-
which will be used to read or write the compressed data.
46+
If filename is a str, bytes, or PathLike object, it gives the
47+
name of the file to be opened. Otherwise, it should be a file
48+
object, which will be used to read or write the compressed data.
4849
4950
mode can be 'r' for reading (default), 'w' for (over)writing,
5051
'x' for creating exclusively, or 'a' for appending. These can
@@ -91,15 +92,15 @@ def __init__(self, filename, mode="r", buffering=None, compresslevel=9):
9192
else:
9293
raise ValueError("Invalid mode: %r" % (mode,))
9394

94-
if isinstance(filename, (str, bytes)):
95+
if isinstance(filename, (str, bytes, os.PathLike)):
9596
self._fp = _builtin_open(filename, mode)
9697
self._closefp = True
9798
self._mode = mode_code
9899
elif hasattr(filename, "read") or hasattr(filename, "write"):
99100
self._fp = filename
100101
self._mode = mode_code
101102
else:
102-
raise TypeError("filename must be a str or bytes object, or a file")
103+
raise TypeError("filename must be a str, bytes, file or PathLike object")
103104

104105
if self._mode == _MODE_READ:
105106
raw = _compression.DecompressReader(self._fp,
@@ -289,8 +290,9 @@ def open(filename, mode="rb", compresslevel=9,
289290
encoding=None, errors=None, newline=None):
290291
"""Open a bzip2-compressed file in binary or text mode.
291292
292-
The filename argument can be an actual filename (a str or bytes
293-
object), or an existing file object to read from or write to.
293+
The filename argument can be an actual filename (a str, bytes, or
294+
PathLike object), or an existing file object to read from or write
295+
to.
294296
295297
The mode argument can be "r", "rb", "w", "wb", "x", "xb", "a" or
296298
"ab" for binary mode, or "rt", "wt", "xt" or "at" for text mode.

Lib/test/test_bz2.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import os
77
import pickle
88
import glob
9+
import pathlib
910
import random
1011
import subprocess
1112
import sys
@@ -560,6 +561,13 @@ def testOpenBytesFilename(self):
560561
with BZ2File(str_filename, "rb") as f:
561562
self.assertEqual(f.read(), self.DATA)
562563

564+
def testOpenPathLikeFilename(self):
565+
filename = pathlib.Path(self.filename)
566+
with BZ2File(filename, "wb") as f:
567+
f.write(self.DATA)
568+
with BZ2File(filename, "rb") as f:
569+
self.assertEqual(f.read(), self.DATA)
570+
563571
def testDecompressLimited(self):
564572
"""Decompressed data buffering should be limited"""
565573
bomb = bz2.compress(b'\0' * int(2e6), compresslevel=9)

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ Core and Builtins
4646
Library
4747
-------
4848

49+
- Issue #28225: bz2 module now supports pathlib. Initial patch by Ethan Furman.
50+
4951
- Issue #28227: gzip now supports pathlib. Patch by Ethan Furman.
5052

5153
- Issue #27358: Optimized merging var-keyword arguments and improved error

0 commit comments

Comments
 (0)