| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent ccda738 commit 51ef89c
17 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -91,7 +91,7 @@ The :mod:`bz2` module contains: | |||
| 91 | 91 | and :meth:`~io.IOBase.truncate`. | |
| 92 | 92 | Iteration and the :keyword:`with` statement are supported. | |
| 93 | 93 | ||
| 94 | - :class:`BZ2File` also provides the following methods: | ||
| 94 | + :class:`BZ2File` also provides the following methods and attributes: | ||
| 95 | 95 | ||
| 96 | 96 | .. method:: peek([n]) | |
| 97 | 97 | ||
@@ -148,6 +148,19 @@ The :mod:`bz2` module contains: | |||
| 148 | 148 | ||
| 149 | 149 | .. versionadded:: 3.3 | |
| 150 | 150 | ||
| 151 | + .. attribute:: mode | ||
| 152 | + | ||
| 153 | + ``'rb'`` for reading and ``'wb'`` for writing. | ||
| 154 | + | ||
| 155 | + .. versionadded:: 3.13 | ||
| 156 | + | ||
| 157 | + .. attribute:: name | ||
| 158 | + | ||
| 159 | + The bzip2 file name. Equivalent to the :attr:`~io.FileIO.name` | ||
| 160 | + attribute of the underlying :term:`file object`. | ||
| 161 | + | ||
| 162 | + .. versionadded:: 3.13 | ||
| 163 | + | ||
| 151 | 164 | ||
| 152 | 165 | .. versionchanged:: 3.1 | |
| 153 | 166 | Support for the :keyword:`with` statement was added. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -133,6 +133,13 @@ The module defines the following items: | |||
| 133 | 133 | ||
| 134 | 134 | .. versionadded:: 3.2 | |
| 135 | 135 | ||
| 136 | + .. attribute:: mode | ||
| 137 | + | ||
| 138 | + ``'rb'`` for reading and ``'wb'`` for writing. | ||
| 139 | + | ||
| 140 | + .. versionchanged:: 3.13 | ||
| 141 | + In previous versions it was an integer ``1`` or ``2``. | ||
| 142 | + | ||
| 136 | 143 | .. attribute:: mtime | |
| 137 | 144 | ||
| 138 | 145 | When decompressing, this attribute is set to the last timestamp in the most | |
@@ -168,14 +175,14 @@ The module defines the following items: | |||
| 168 | 175 | .. versionchanged:: 3.6 | |
| 169 | 176 | Accepts a :term:`path-like object`. | |
| 170 | 177 | ||
| 171 | - .. versionchanged:: 3.12 | ||
| 172 | - Remove the ``filename`` attribute, use the :attr:`~GzipFile.name` | ||
| 173 | - attribute instead. | ||
| 174 | - | ||
| 175 | 178 | .. deprecated:: 3.9 | |
| 176 | 179 | Opening :class:`GzipFile` for writing without specifying the *mode* | |
| 177 | 180 | argument is deprecated. | |
| 178 | 181 | ||
| 182 | + .. versionchanged:: 3.12 | ||
| 183 | + Remove the ``filename`` attribute, use the :attr:`~GzipFile.name` | ||
| 184 | + attribute instead. | ||
| 185 | + | ||
| 179 | 186 | ||
| 180 | 187 | .. function:: compress(data, compresslevel=9, *, mtime=None) | |
| 181 | 188 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -104,7 +104,7 @@ Reading and writing compressed files | |||
| 104 | 104 | and :meth:`~io.IOBase.truncate`. | |
| 105 | 105 | Iteration and the :keyword:`with` statement are supported. | |
| 106 | 106 | ||
| 107 | - The following method is also provided: | ||
| 107 | + The following method and attributes are also provided: | ||
| 108 | 108 | ||
| 109 | 109 | .. method:: peek(size=-1) | |
| 110 | 110 | ||
@@ -117,6 +117,20 @@ Reading and writing compressed files | |||
| 117 | 117 | file object (e.g. if the :class:`LZMAFile` was constructed by passing a | |
| 118 | 118 | file object for *filename*). | |
| 119 | 119 | ||
| 120 | + .. attribute:: mode | ||
| 121 | + | ||
| 122 | + ``'rb'`` for reading and ``'wb'`` for writing. | ||
| 123 | + | ||
| 124 | + .. versionadded:: 3.13 | ||
| 125 | + | ||
| 126 | + .. attribute:: name | ||
| 127 | + | ||
| 128 | + The lzma file name. Equivalent to the :attr:`~io.FileIO.name` | ||
| 129 | + attribute of the underlying :term:`file object`. | ||
| 130 | + | ||
| 131 | + .. versionadded:: 3.13 | ||
| 132 | + | ||
| 133 | + | ||
| 120 | 134 | .. versionchanged:: 3.4 | |
| 121 | 135 | Added support for the ``"x"`` and ``"xb"`` modes. | |
| 122 | 136 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -565,6 +565,10 @@ be finalized; only the internally used file object will be closed. See the | |||
| 565 | 565 | .. versionchanged:: 3.3 | |
| 566 | 566 | Return an :class:`io.BufferedReader` object. | |
| 567 | 567 | ||
| 568 | + .. versionchanged:: 3.13 | ||
| 569 | + The returned :class:`io.BufferedReader` object has the :attr:`!mode` | ||
| 570 | + attribute which is always equal to ``'rb'``. | ||
| 571 | + | ||
| 568 | 572 | .. attribute:: TarFile.errorlevel | |
| 569 | 573 | :type: int | |
| 570 | 574 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -301,6 +301,10 @@ ZipFile Objects | |||
| 301 | 301 | attempting to read or write other files in the ZIP file will raise a | |
| 302 | 302 | :exc:`ValueError`. | |
| 303 | 303 | ||
| 304 | + In both cases the file-like object has also attributes :attr:`!name`, | ||
| 305 | + which is equivalent to the name of a file within the archive, and | ||
| 306 | + :attr:`!mode`, which is ``'rb'`` or ``'wb'`` depending on the input mode. | ||
| 307 | + | ||
| 304 | 308 | When writing a file, if the file size is not known in advance but may exceed | |
| 305 | 309 | 2 GiB, pass ``force_zip64=True`` to ensure that the header format is | |
| 306 | 310 | capable of supporting large files. If the file size is known in advance, | |
@@ -325,6 +329,12 @@ ZipFile Objects | |||
| 325 | 329 | Calling :meth:`.open` on a closed ZipFile will raise a :exc:`ValueError`. | |
| 326 | 330 | Previously, a :exc:`RuntimeError` was raised. | |
| 327 | 331 | ||
| 332 | + .. versionchanged:: 3.13 | ||
| 333 | + Added attributes :attr:`!name` and :attr:`!mode` for the writeable | ||
| 334 | + file-like object. | ||
| 335 | + The value of the :attr:`!mode` attribute for the readable file-like | ||
| 336 | + object was changed from ``'r'`` to ``'rb'``. | ||
| 337 | + | ||
| 328 | 338 | ||
| 329 | 339 | .. method:: ZipFile.extract(member, path=None, pwd=None) | |
| 330 | 340 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -206,6 +206,11 @@ Other Language Changes | |||
| 206 | 206 | ||
| 207 | 207 | (Contributed by Victor Stinner in :gh:`114570`.) | |
| 208 | 208 | ||
| 209 | + * Added :attr:`!name` and :attr:`!mode` attributes for compressed | ||
| 210 | + and archived file-like objects in modules :mod:`bz2`, :mod:`lzma`, | ||
| 211 | + :mod:`tarfile` and :mod:`zipfile`. | ||
| 212 | + (Contributed by Serhiy Storchaka in :gh:`115961`.) | ||
| 213 | + | ||
| 209 | 214 | * Allow controlling Expat >=2.6.0 reparse deferral (:cve:`2023-52425`) | |
| 210 | 215 | by adding five new methods: | |
| 211 | 216 | ||
@@ -1605,6 +1610,12 @@ Changes in the Python API | |||
| 1605 | 1610 | than directories only. Users may add a trailing slash to match only | |
| 1606 | 1611 | directories. | |
| 1607 | 1612 | ||
| 1613 | + * The value of the :attr:`!mode` attribute of :class:`gzip.GzipFile` was | ||
| 1614 | + changed from integer (``1`` or ``2``) to string (``'rb'`` or ``'wb'``). | ||
| 1615 | + The value of the :attr:`!mode` attribute of the readable file-like object | ||
| 1616 | + returned by :meth:`zipfile.ZipFile.open` was changed from ``'r'`` to ``'rb'``. | ||
| 1617 | + (Contributed by Serhiy Storchaka in :gh:`115961`.) | ||
| 1618 | + | ||
| 1608 | 1619 | * :c:func:`!PyCode_GetFirstFree` is an unstable API now and has been renamed | |
| 1609 | 1620 | to :c:func:`PyUnstable_Code_GetFirstFree`. | |
| 1610 | 1621 | (Contributed by Bogdan Romanyuk in :gh:`115781`.) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -17,7 +17,7 @@ | |||
| 17 | 17 | from _bz2 import BZ2Compressor, BZ2Decompressor | |
| 18 | 18 | ||
| 19 | 19 | ||
| 20 | - _MODE_CLOSED = 0 | ||
| 20 | + # Value 0 no longer used | ||
| 21 | 21 | _MODE_READ = 1 | |
| 22 | 22 | # Value 2 no longer used | |
| 23 | 23 | _MODE_WRITE = 3 | |
@@ -54,7 +54,7 @@ def __init__(self, filename, mode="r", *, compresslevel=9): | |||
| 54 | 54 | """ | |
| 55 | 55 | self._fp = None | |
| 56 | 56 | self._closefp = False | |
| 57 | - self._mode = _MODE_CLOSED | ||
| 57 | + self._mode = None | ||
| 58 | 58 | ||
| 59 | 59 | if not (1 <= compresslevel <= 9): | |
| 60 | 60 | raise ValueError("compresslevel must be between 1 and 9") | |
@@ -100,7 +100,7 @@ def close(self): | |||
| 100 | 100 | May be called more than once without error. Once the file is | |
| 101 | 101 | closed, any other operation on it will raise a ValueError. | |
| 102 | 102 | """ | |
| 103 | - if self._mode == _MODE_CLOSED: | ||
| 103 | + if self.closed: | ||
| 104 | 104 | return | |
| 105 | 105 | try: | |
| 106 | 106 | if self._mode == _MODE_READ: | |
@@ -115,13 +115,21 @@ def close(self): | |||
| 115 | 115 | finally: | |
| 116 | 116 | self._fp = None | |
| 117 | 117 | self._closefp = False | |
| 118 | - self._mode = _MODE_CLOSED | ||
| 119 | 118 | self._buffer = None | |
| 120 | 119 | ||
| 121 | 120 | @property | |
| 122 | 121 | def closed(self): | |
| 123 | 122 | """True if this file is closed.""" | |
| 124 | - return self._mode == _MODE_CLOSED | ||
| 123 | + return self._fp is None | ||
| 124 | + | ||
| 125 | + @property | ||
| 126 | + def name(self): | ||
| 127 | + self._check_not_closed() | ||
| 128 | + return self._fp.name | ||
| 129 | + | ||
| 130 | + @property | ||
| 131 | + def mode(self): | ||
| 132 | + return 'wb' if self._mode == _MODE_WRITE else 'rb' | ||
| 125 | 133 | ||
| 126 | 134 | def fileno(self): | |
| 127 | 135 | """Return the file descriptor for the underlying file.""" | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -15,7 +15,8 @@ | |||
| 15 | 15 | ||
| 16 | 16 | FTEXT, FHCRC, FEXTRA, FNAME, FCOMMENT = 1, 2, 4, 8, 16 | |
| 17 | 17 | ||
| 18 | - READ, WRITE = 1, 2 | ||
| 18 | + READ = 'rb' | ||
| 19 | + WRITE = 'wb' | ||
| 19 | 20 | ||
| 20 | 21 | _COMPRESS_LEVEL_FAST = 1 | |
| 21 | 22 | _COMPRESS_LEVEL_TRADEOFF = 6 | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -29,7 +29,7 @@ | |||
| 29 | 29 | import _compression | |
| 30 | 30 | ||
| 31 | 31 | ||
| 32 | - _MODE_CLOSED = 0 | ||
| 32 | + # Value 0 no longer used | ||
| 33 | 33 | _MODE_READ = 1 | |
| 34 | 34 | # Value 2 no longer used | |
| 35 | 35 | _MODE_WRITE = 3 | |
@@ -92,7 +92,7 @@ def __init__(self, filename=None, mode="r", *, | |||
| 92 | 92 | """ | |
| 93 | 93 | self._fp = None | |
| 94 | 94 | self._closefp = False | |
| 95 | - self._mode = _MODE_CLOSED | ||
| 95 | + self._mode = None | ||
| 96 | 96 | ||
| 97 | 97 | if mode in ("r", "rb"): | |
| 98 | 98 | if check != -1: | |
@@ -137,7 +137,7 @@ def close(self): | |||
| 137 | 137 | May be called more than once without error. Once the file is | |
| 138 | 138 | closed, any other operation on it will raise a ValueError. | |
| 139 | 139 | """ | |
| 140 | - if self._mode == _MODE_CLOSED: | ||
| 140 | + if self.closed: | ||
| 141 | 141 | return | |
| 142 | 142 | try: | |
| 143 | 143 | if self._mode == _MODE_READ: | |
@@ -153,12 +153,20 @@ def close(self): | |||
| 153 | 153 | finally: | |
| 154 | 154 | self._fp = None | |
| 155 | 155 | self._closefp = False | |
| 156 | - self._mode = _MODE_CLOSED | ||
| 157 | 156 | ||
| 158 | 157 | @property | |
| 159 | 158 | def closed(self): | |
| 160 | 159 | """True if this file is closed.""" | |
| 161 | - return self._mode == _MODE_CLOSED | ||
| 160 | + return self._fp is None | ||
| 161 | + | ||
| 162 | + @property | ||
| 163 | + def name(self): | ||
| 164 | + self._check_not_closed() | ||
| 165 | + return self._fp.name | ||
| 166 | + | ||
| 167 | + @property | ||
| 168 | + def mode(self): | ||
| 169 | + return 'wb' if self._mode == _MODE_WRITE else 'rb' | ||
| 162 | 170 | ||
| 163 | 171 | def fileno(self): | |
| 164 | 172 | """Return the file descriptor for the underlying file.""" | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -636,6 +636,10 @@ def __init__(self, fileobj, offset, size, name, blockinfo=None): | |||
| 636 | 636 | def flush(self): | |
| 637 | 637 | pass | |
| 638 | 638 | ||
| 639 | + @property | ||
| 640 | + def mode(self): | ||
| 641 | + return 'rb' | ||
| 642 | + | ||
| 639 | 643 | def readable(self): | |
| 640 | 644 | return True | |
| 641 | 645 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments