diff options
author | Nadeem Vawda <nadeem.vawda@gmail.com> | 2012-06-04 21:34:07 (GMT) |
---|---|---|
committer | Nadeem Vawda <nadeem.vawda@gmail.com> | 2012-06-04 21:34:07 (GMT) |
commit | 33c34da5745f2e3fdc315e5098295621d8023674 (patch) | |
tree | 224765898477deda4a8d39dbf574c476752b16dd /Lib/lzma.py | |
parent | af518c198e249aae63a82a5a44c93c3e5cc140f4 (diff) | |
download | cpython-33c34da5745f2e3fdc315e5098295621d8023674.zip cpython-33c34da5745f2e3fdc315e5098295621d8023674.tar.gz cpython-33c34da5745f2e3fdc315e5098295621d8023674.tar.bz2 |
Simplify usage of LZMAFile's fileobj support, like with BZ2File.
Diffstat (limited to 'Lib/lzma.py')
-rw-r--r-- | Lib/lzma.py | 19 |
1 files changed, 9 insertions, 10 deletions
diff --git a/Lib/lzma.py b/Lib/lzma.py index 1c101f7..8fb3f03 100644 --- a/Lib/lzma.py +++ b/Lib/lzma.py @@ -46,13 +46,12 @@ class LZMAFile(io.BufferedIOBase): """ def __init__(self, filename=None, mode="r", *, - fileobj=None, format=None, check=-1, - preset=None, filters=None): - """Open an LZMA-compressed file. + format=None, check=-1, preset=None, filters=None): + """Open an LZMA-compressed file in binary mode. - 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. + filename can be either an actual file name (given as a str or + bytes object), in which case the named file is opened, or it can + be an existing file object to read from or write to. mode can be "r" for reading (default), "w" for (over)writing, or "a" for appending. @@ -119,16 +118,16 @@ class LZMAFile(io.BufferedIOBase): else: raise ValueError("Invalid mode: {!r}".format(mode)) - if filename is not None and fileobj is None: + if isinstance(filename, (str, bytes)): mode += "b" 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. |