|
11 | 11 |
|
12 | 12 | from builtins import open as _builtin_open |
13 | 13 | import io |
| 14 | +import os |
14 | 15 | import warnings |
15 | 16 | import _compression |
16 | 17 |
|
@@ -42,9 +43,9 @@ class BZ2File(_compression.BaseStream): |
42 | 43 | def __init__(self, filename, mode="r", buffering=None, compresslevel=9): |
43 | 44 | """Open a bzip2-compressed file. |
44 | 45 |
|
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. |
48 | 49 |
|
49 | 50 | mode can be 'r' for reading (default), 'w' for (over)writing, |
50 | 51 | 'x' for creating exclusively, or 'a' for appending. These can |
@@ -91,15 +92,15 @@ def __init__(self, filename, mode="r", buffering=None, compresslevel=9): |
91 | 92 | else: |
92 | 93 | raise ValueError("Invalid mode: %r" % (mode,)) |
93 | 94 |
|
94 | | - if isinstance(filename, (str, bytes)): |
| 95 | + if isinstance(filename, (str, bytes, os.PathLike)): |
95 | 96 | self._fp = _builtin_open(filename, mode) |
96 | 97 | self._closefp = True |
97 | 98 | self._mode = mode_code |
98 | 99 | elif hasattr(filename, "read") or hasattr(filename, "write"): |
99 | 100 | self._fp = filename |
100 | 101 | self._mode = mode_code |
101 | 102 | 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") |
103 | 104 |
|
104 | 105 | if self._mode == _MODE_READ: |
105 | 106 | raw = _compression.DecompressReader(self._fp, |
@@ -289,8 +290,9 @@ def open(filename, mode="rb", compresslevel=9, |
289 | 290 | encoding=None, errors=None, newline=None): |
290 | 291 | """Open a bzip2-compressed file in binary or text mode. |
291 | 292 |
|
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. |
294 | 296 |
|
295 | 297 | The mode argument can be "r", "rb", "w", "wb", "x", "xb", "a" or |
296 | 298 | "ab" for binary mode, or "rt", "wt", "xt" or "at" for text mode. |
|
0 commit comments