summaryrefslogtreecommitdiffstats
path: root/Lib/bz2.py
diff options
context:
space:
mode:
authorBerker Peksag <berker.peksag@gmail.com>2016-10-02 17:07:06 (GMT)
committerBerker Peksag <berker.peksag@gmail.com>2016-10-02 17:07:06 (GMT)
commit8bdd4480c46bea97788c45323329ebd54c91242f (patch)
treeab289912cc95bcb9e40d9a703e20aae5e506553a /Lib/bz2.py
parent03020cfa97b6c2c80f50fb2d07025aff49c3513c (diff)
downloadcpython-8bdd4480c46bea97788c45323329ebd54c91242f.zip
cpython-8bdd4480c46bea97788c45323329ebd54c91242f.tar.gz
cpython-8bdd4480c46bea97788c45323329ebd54c91242f.tar.bz2
Issue #28225: bz2 module now supports pathlib
Initial patch by Ethan Furman.
Diffstat (limited to 'Lib/bz2.py')
-rw-r--r--Lib/bz2.py16
1 files changed, 9 insertions, 7 deletions
diff --git a/Lib/bz2.py b/Lib/bz2.py
index bc78c54..6f56328 100644
--- a/Lib/bz2.py
+++ b/Lib/bz2.py
@@ -11,6 +11,7 @@ __author__ = "Nadeem Vawda <nadeem.vawda@gmail.com>"
from builtins import open as _builtin_open
import io
+import os
import warnings
import _compression
@@ -42,9 +43,9 @@ class BZ2File(_compression.BaseStream):
def __init__(self, filename, mode="r", buffering=None, compresslevel=9):
"""Open a bzip2-compressed file.
- If filename is a str or bytes object, it gives the name
- of the file to be opened. Otherwise, it should be a file object,
- which will be used to read or write the compressed data.
+ If filename is a str, bytes, or PathLike object, it gives the
+ name of the file to be opened. Otherwise, it should be a file
+ object, which will be used to read or write the compressed data.
mode can be 'r' for reading (default), 'w' for (over)writing,
'x' for creating exclusively, or 'a' for appending. These can
@@ -91,7 +92,7 @@ class BZ2File(_compression.BaseStream):
else:
raise ValueError("Invalid mode: %r" % (mode,))
- if isinstance(filename, (str, bytes)):
+ if isinstance(filename, (str, bytes, os.PathLike)):
self._fp = _builtin_open(filename, mode)
self._closefp = True
self._mode = mode_code
@@ -99,7 +100,7 @@ class BZ2File(_compression.BaseStream):
self._fp = filename
self._mode = mode_code
else:
- raise TypeError("filename must be a str or bytes object, or a file")
+ raise TypeError("filename must be a str, bytes, file or PathLike object")
if self._mode == _MODE_READ:
raw = _compression.DecompressReader(self._fp,
@@ -289,8 +290,9 @@ def open(filename, mode="rb", compresslevel=9,
encoding=None, errors=None, newline=None):
"""Open a bzip2-compressed file in binary or text mode.
- The filename argument can be an actual filename (a str or bytes
- object), or an existing file object to read from or write to.
+ The filename argument can be an actual filename (a str, bytes, or
+ PathLike object), or an existing file object to read from or write
+ to.
The mode argument can be "r", "rb", "w", "wb", "x", "xb", "a" or
"ab" for binary mode, or "rt", "wt", "xt" or "at" for text mode.