summaryrefslogtreecommitdiffstats
path: root/Lib/bz2.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2024-04-21 08:46:39 (GMT)
committerGitHub <noreply@github.com>2024-04-21 08:46:39 (GMT)
commit51ef89cd9acbfbfa7fd8d82f0c6baa388bb650c9 (patch)
treeeb6f52baa37360fb86c7401d12c47f51ad3795af /Lib/bz2.py
parentccda73828473576c57d1bb31774f56542d6e8964 (diff)
downloadcpython-51ef89cd9acbfbfa7fd8d82f0c6baa388bb650c9.zip
cpython-51ef89cd9acbfbfa7fd8d82f0c6baa388bb650c9.tar.gz
cpython-51ef89cd9acbfbfa7fd8d82f0c6baa388bb650c9.tar.bz2
gh-115961: Add name and mode attributes for compressed file-like objects (GH-116036)
* Add name and mode attributes for compressed and archived file-like objects in modules bz2, lzma, tarfile and zipfile. * Change the value of the mode attribute of GzipFile from integer (1 or 2) to string ('rb' or 'wb'). * Change the value of the mode attribute of ZipExtFile from 'r' to 'rb'.
Diffstat (limited to 'Lib/bz2.py')
-rw-r--r--Lib/bz2.py18
1 files changed, 13 insertions, 5 deletions
diff --git a/Lib/bz2.py b/Lib/bz2.py
index fabe4f7..2420cd0 100644
--- a/Lib/bz2.py
+++ b/Lib/bz2.py
@@ -17,7 +17,7 @@ import _compression
from _bz2 import BZ2Compressor, BZ2Decompressor
-_MODE_CLOSED = 0
+# Value 0 no longer used
_MODE_READ = 1
# Value 2 no longer used
_MODE_WRITE = 3
@@ -54,7 +54,7 @@ class BZ2File(_compression.BaseStream):
"""
self._fp = None
self._closefp = False
- self._mode = _MODE_CLOSED
+ self._mode = None
if not (1 <= compresslevel <= 9):
raise ValueError("compresslevel must be between 1 and 9")
@@ -100,7 +100,7 @@ class BZ2File(_compression.BaseStream):
May be called more than once without error. Once the file is
closed, any other operation on it will raise a ValueError.
"""
- if self._mode == _MODE_CLOSED:
+ if self.closed:
return
try:
if self._mode == _MODE_READ:
@@ -115,13 +115,21 @@ class BZ2File(_compression.BaseStream):
finally:
self._fp = None
self._closefp = False
- self._mode = _MODE_CLOSED
self._buffer = None
@property
def closed(self):
"""True if this file is closed."""
- return self._mode == _MODE_CLOSED
+ return self._fp is None
+
+ @property
+ def name(self):
+ self._check_not_closed()
+ return self._fp.name
+
+ @property
+ def mode(self):
+ return 'wb' if self._mode == _MODE_WRITE else 'rb'
def fileno(self):
"""Return the file descriptor for the underlying file."""