Issue #19223: Add support for the 'x' mode to the bz2 module.
Patch by Tim Heaney and Vajrasky Kok.
diff --git a/Lib/bz2.py b/Lib/bz2.py
index 6e6a2b9..6bc611e 100644
--- a/Lib/bz2.py
+++ b/Lib/bz2.py
@@ -49,12 +49,12 @@
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. These can equivalently be given as 'rb',
- 'wb', and 'ab'.
+ 'x' for creating exclusively, or 'a' for appending. These can
+ equivalently be given as 'rb', 'wb', 'xb', and 'ab'.
buffering is ignored. Its use is deprecated.
- If mode is 'w' or 'a', compresslevel can be a number between 1
+ If mode is 'w', 'x' or 'a', compresslevel can be a number between 1
and 9 specifying the level of compression: 1 produces the least
compression, and 9 (default) produces the most compression.
@@ -87,6 +87,10 @@
mode = "wb"
mode_code = _MODE_WRITE
self._compressor = BZ2Compressor(compresslevel)
+ elif mode in ("x", "xb"):
+ mode = "xb"
+ mode_code = _MODE_WRITE
+ self._compressor = BZ2Compressor(compresslevel)
elif mode in ("a", "ab"):
mode = "ab"
mode_code = _MODE_WRITE
@@ -443,9 +447,9 @@
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 mode argument can be "r", "rb", "w", "wb", "a" or "ab" for
- binary mode, or "rt", "wt" or "at" for text mode. The default mode
- is "rb", and the default compresslevel is 9.
+ 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.
+ The default mode is "rb", and the default compresslevel is 9.
For binary mode, this function is equivalent to the BZ2File
constructor: BZ2File(filename, mode, compresslevel). In this case,