summaryrefslogtreecommitdiff
path: root/Lib
diff options
context:
space:
mode:
authorNadeem Vawda <nadeem.vawda@gmail.com>2012-06-04 23:36:24 +0200
committerNadeem Vawda <nadeem.vawda@gmail.com>2012-06-04 23:36:24 +0200
commit6cbb20cdf61329ebfa6afcacad21ee6252fb5be5 (patch)
treea0493b69a4f5d2ac3cafa4e0b20efa5f3a856cef /Lib
parent33c34da5745f2e3fdc315e5098295621d8023674 (diff)
downloadcpython-git-6cbb20cdf61329ebfa6afcacad21ee6252fb5be5.tar.gz
Allow LZMAFile to accept modes with a "b" suffix.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/lzma.py10
-rw-r--r--Lib/test/test_lzma.py19
2 files changed, 23 insertions, 6 deletions
diff --git a/Lib/lzma.py b/Lib/lzma.py
index 8fb3f03328..07906910c5 100644
--- a/Lib/lzma.py
+++ b/Lib/lzma.py
@@ -54,7 +54,8 @@ class LZMAFile(io.BufferedIOBase):
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.
+ "a" for appending. These can equivalently be given as "rb", "wb",
+ and "ab" respectively.
format specifies the container format to use for the file.
If mode is "r", this defaults to FORMAT_AUTO. Otherwise, the
@@ -93,7 +94,7 @@ class LZMAFile(io.BufferedIOBase):
self._pos = 0
self._size = -1
- if mode == "r":
+ if mode in ("r", "rb"):
if check != -1:
raise ValueError("Cannot specify an integrity check "
"when opening a file for reading")
@@ -109,7 +110,7 @@ class LZMAFile(io.BufferedIOBase):
self._init_args = {"format":format, "filters":filters}
self._decompressor = LZMADecompressor(**self._init_args)
self._buffer = None
- elif mode in ("w", "a"):
+ elif mode in ("w", "wb", "a", "ab"):
if format is None:
format = FORMAT_XZ
mode_code = _MODE_WRITE
@@ -119,7 +120,8 @@ class LZMAFile(io.BufferedIOBase):
raise ValueError("Invalid mode: {!r}".format(mode))
if isinstance(filename, (str, bytes)):
- mode += "b"
+ if "b" not in mode:
+ mode += "b"
self._fp = open(filename, mode)
self._closefp = True
self._mode = mode_code
diff --git a/Lib/test/test_lzma.py b/Lib/test/test_lzma.py
index aee7921ca4..e4d2cb1ff3 100644
--- a/Lib/test/test_lzma.py
+++ b/Lib/test/test_lzma.py
@@ -374,6 +374,21 @@ class FileTestCase(unittest.TestCase):
with LZMAFile(TESTFN, "a") as f:
pass
+ def test_init_mode(self):
+ with TempFile(TESTFN):
+ with LZMAFile(TESTFN, "r"):
+ pass
+ with LZMAFile(TESTFN, "rb"):
+ pass
+ with LZMAFile(TESTFN, "w"):
+ pass
+ with LZMAFile(TESTFN, "wb"):
+ pass
+ with LZMAFile(TESTFN, "a"):
+ pass
+ with LZMAFile(TESTFN, "ab"):
+ pass
+
def test_init_bad_mode(self):
with self.assertRaises(ValueError):
LZMAFile(BytesIO(COMPRESSED_XZ), (3, "x"))
@@ -382,11 +397,11 @@ class FileTestCase(unittest.TestCase):
with self.assertRaises(ValueError):
LZMAFile(BytesIO(COMPRESSED_XZ), "x")
with self.assertRaises(ValueError):
- LZMAFile(BytesIO(COMPRESSED_XZ), "rb")
+ LZMAFile(BytesIO(COMPRESSED_XZ), "rt")
with self.assertRaises(ValueError):
LZMAFile(BytesIO(COMPRESSED_XZ), "r+")
with self.assertRaises(ValueError):
- LZMAFile(BytesIO(COMPRESSED_XZ), "wb")
+ LZMAFile(BytesIO(COMPRESSED_XZ), "wt")
with self.assertRaises(ValueError):
LZMAFile(BytesIO(COMPRESSED_XZ), "w+")
with self.assertRaises(ValueError):