diff -r 54be46e5110b Lib/lzma.py --- a/Lib/lzma.py Wed Oct 09 13:29:42 2013 -0500 +++ b/Lib/lzma.py Thu Oct 10 18:32:49 2013 +0800 @@ -112,7 +112,7 @@ self._decompressor = LZMADecompressor(**self._init_args) self._buffer = b"" self._buffer_offset = 0 - elif mode in ("w", "wb", "a", "ab"): + elif mode in ("w", "wb", "a", "ab", "x", "xb"): if format is None: format = FORMAT_XZ mode_code = _MODE_WRITE diff -r 54be46e5110b Lib/test/test_lzma.py --- a/Lib/test/test_lzma.py Wed Oct 09 13:29:42 2013 -0500 +++ b/Lib/test/test_lzma.py Thu Oct 10 18:32:49 2013 +0800 @@ -364,8 +364,13 @@ pass with LZMAFile(BytesIO(), "a") as f: pass + with LZMAFile(BytesIO(), "x") as f: + pass def test_init_with_filename(self): + unlink(TESTFN) + with LZMAFile(TESTFN, "x") as f: + pass with TempFile(TESTFN, COMPRESSED_XZ): with LZMAFile(TESTFN) as f: pass @@ -373,6 +378,9 @@ pass with LZMAFile(TESTFN, "a") as f: pass + with self.assertRaises(FileExistsError): + with LZMAFile(TESTFN, "x") as f: + pass def test_init_mode(self): with TempFile(TESTFN): @@ -388,6 +396,9 @@ pass with LZMAFile(TESTFN, "ab"): pass + with self.assertRaises(FileExistsError): + with LZMAFile(TESTFN, "x"): + pass def test_init_bad_mode(self): with self.assertRaises(ValueError): @@ -395,7 +406,13 @@ with self.assertRaises(ValueError): LZMAFile(BytesIO(COMPRESSED_XZ), "") with self.assertRaises(ValueError): - LZMAFile(BytesIO(COMPRESSED_XZ), "x") + LZMAFile(BytesIO(COMPRESSED_XZ), "xt") + with self.assertRaises(ValueError): + LZMAFile(BytesIO(COMPRESSED_XZ), "x+") + with self.assertRaises(ValueError): + LZMAFile(BytesIO(COMPRESSED_XZ), "rx") + with self.assertRaises(ValueError): + LZMAFile(BytesIO(COMPRESSED_XZ), "wx") with self.assertRaises(ValueError): LZMAFile(BytesIO(COMPRESSED_XZ), "rt") with self.assertRaises(ValueError): @@ -978,30 +995,32 @@ def test_binary_modes(self): with lzma.open(BytesIO(COMPRESSED_XZ), "rb") as f: self.assertEqual(f.read(), INPUT) - with BytesIO() as bio: - with lzma.open(bio, "wb") as f: - f.write(INPUT) - file_data = lzma.decompress(bio.getvalue()) - self.assertEqual(file_data, INPUT) - with lzma.open(bio, "ab") as f: - f.write(INPUT) - file_data = lzma.decompress(bio.getvalue()) - self.assertEqual(file_data, INPUT * 2) + for mode in ("w", "x"): + with BytesIO() as bio: + with lzma.open(bio, mode + "b") as f: + f.write(INPUT) + file_data = lzma.decompress(bio.getvalue()) + self.assertEqual(file_data, INPUT) + with lzma.open(bio, "ab") as f: + f.write(INPUT) + file_data = lzma.decompress(bio.getvalue()) + self.assertEqual(file_data, INPUT * 2) def test_text_modes(self): uncompressed = INPUT.decode("ascii") uncompressed_raw = uncompressed.replace("\n", os.linesep) with lzma.open(BytesIO(COMPRESSED_XZ), "rt") as f: self.assertEqual(f.read(), uncompressed) - with BytesIO() as bio: - with lzma.open(bio, "wt") as f: - f.write(uncompressed) - file_data = lzma.decompress(bio.getvalue()).decode("ascii") - self.assertEqual(file_data, uncompressed_raw) - with lzma.open(bio, "at") as f: - f.write(uncompressed) - file_data = lzma.decompress(bio.getvalue()).decode("ascii") - self.assertEqual(file_data, uncompressed_raw * 2) + for mode in ("w", "x"): + with BytesIO() as bio: + with lzma.open(bio, mode + "t") as f: + f.write(uncompressed) + file_data = lzma.decompress(bio.getvalue()).decode("ascii") + self.assertEqual(file_data, uncompressed_raw) + with lzma.open(bio, "at") as f: + f.write(uncompressed) + file_data = lzma.decompress(bio.getvalue()).decode("ascii") + self.assertEqual(file_data, uncompressed_raw * 2) def test_filename(self): with TempFile(TESTFN): @@ -1016,13 +1035,16 @@ f.write(INPUT) with lzma.open(TESTFN, "rb") as f: self.assertEqual(f.read(), INPUT * 2) + with self.assertRaises(FileExistsError): + with lzma.open(TESTFN, "xb") as f: + f.write(INPUT) def test_bad_params(self): # Test invalid parameter combinations. with self.assertRaises(ValueError): lzma.open(TESTFN, "") with self.assertRaises(ValueError): - lzma.open(TESTFN, "x") + lzma.open(TESTFN, "xbt") with self.assertRaises(ValueError): lzma.open(TESTFN, "rbt") with self.assertRaises(ValueError): @@ -1037,24 +1059,26 @@ options = {"format": lzma.FORMAT_RAW, "filters": FILTERS_RAW_1} with lzma.open(BytesIO(COMPRESSED_RAW_1), "rb", **options) as f: self.assertEqual(f.read(), INPUT) - with BytesIO() as bio: - with lzma.open(bio, "wb", **options) as f: - f.write(INPUT) - file_data = lzma.decompress(bio.getvalue(), **options) - self.assertEqual(file_data, INPUT) + for mode in ("w", "x"): + with BytesIO() as bio: + with lzma.open(bio, mode + "b", **options) as f: + f.write(INPUT) + file_data = lzma.decompress(bio.getvalue(), **options) + self.assertEqual(file_data, INPUT) def test_encoding(self): # Test non-default encoding. uncompressed = INPUT.decode("ascii") uncompressed_raw = uncompressed.replace("\n", os.linesep) - with BytesIO() as bio: - with lzma.open(bio, "wt", encoding="utf-16-le") as f: - f.write(uncompressed) - file_data = lzma.decompress(bio.getvalue()).decode("utf-16-le") - self.assertEqual(file_data, uncompressed_raw) - bio.seek(0) - with lzma.open(bio, "rt", encoding="utf-16-le") as f: - self.assertEqual(f.read(), uncompressed) + for mode in ("w", "x"): + with BytesIO() as bio: + with lzma.open(bio, mode + "t", encoding="utf-16-le") as f: + f.write(uncompressed) + file_data = lzma.decompress(bio.getvalue()).decode("utf-16-le") + self.assertEqual(file_data, uncompressed_raw) + bio.seek(0) + with lzma.open(bio, "rt", encoding="utf-16-le") as f: + self.assertEqual(f.read(), uncompressed) def test_encoding_error_handler(self): # Test wih non-default encoding error handler. @@ -1065,12 +1089,13 @@ def test_newline(self): # Test with explicit newline (universal newline mode disabled). text = INPUT.decode("ascii") - with BytesIO() as bio: - with lzma.open(bio, "wt", newline="\n") as f: - f.write(text) - bio.seek(0) - with lzma.open(bio, "rt", newline="\r") as f: - self.assertEqual(f.readlines(), [text]) + for mode in ("w", "x"): + with BytesIO() as bio: + with lzma.open(bio, mode + "t", newline="\n") as f: + f.write(text) + bio.seek(0) + with lzma.open(bio, "rt", newline="\r") as f: + self.assertEqual(f.readlines(), [text]) class MiscellaneousTestCase(unittest.TestCase):