summaryrefslogtreecommitdiffstats
path: root/Lib/bz2.py
diff options
context:
space:
mode:
authorNadeem Vawda <nadeem.vawda@gmail.com>2012-06-04 21:31:20 (GMT)
committerNadeem Vawda <nadeem.vawda@gmail.com>2012-06-04 21:31:20 (GMT)
commitaebcdba8297bf5174ecf4e8687ad23883e35b14b (patch)
treeb9b55d8bb72a27f439da47f98d676f56876d8237 /Lib/bz2.py
parent68721019efb16ba8acad036c331a9a195d6f7da0 (diff)
downloadcpython-aebcdba8297bf5174ecf4e8687ad23883e35b14b.zip
cpython-aebcdba8297bf5174ecf4e8687ad23883e35b14b.tar.gz
cpython-aebcdba8297bf5174ecf4e8687ad23883e35b14b.tar.bz2
Make BZ2File's fileobj support easier to use.
The fileobj argument was added during the 3.3 development cycle, so this change does not break backward compatibility with 3.2.
Diffstat (limited to 'Lib/bz2.py')
-rw-r--r--Lib/bz2.py17
1 files changed, 8 insertions, 9 deletions
diff --git a/Lib/bz2.py b/Lib/bz2.py
index 51b9ac4..ae59407 100644
--- a/Lib/bz2.py
+++ b/Lib/bz2.py
@@ -39,13 +39,12 @@ class BZ2File(io.BufferedIOBase):
returned as bytes, and data to be written should be given as bytes.
"""
- def __init__(self, filename=None, mode="r", buffering=None,
- compresslevel=9, *, fileobj=None):
+ def __init__(self, filename, mode="r", buffering=None, compresslevel=9):
"""Open a bzip2-compressed file.
- If filename is given, open the named file. Otherwise, operate on
- the file object given by fileobj. Exactly one of these two
- parameters should be provided.
+ If filename is a str or bytes object, is 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, or
'a' for appending.
@@ -91,15 +90,15 @@ class BZ2File(io.BufferedIOBase):
else:
raise ValueError("Invalid mode: {!r}".format(mode))
- if filename is not None and fileobj is None:
+ if isinstance(filename, (str, bytes)):
self._fp = open(filename, mode)
self._closefp = True
self._mode = mode_code
- elif fileobj is not None and filename is None:
- self._fp = fileobj
+ elif hasattr(filename, "read") or hasattr(filename, "write"):
+ self._fp = filename
self._mode = mode_code
else:
- raise ValueError("Must give exactly one of filename and fileobj")
+ raise TypeError("filename must be a str or bytes object, or a file")
def close(self):
"""Flush and close the file.