Constructor for the GzipFile class. At least one of fileobj and filename must be given a non-trivial value. The new class instance is based on fileobj, which can be a regular file, an io.BytesIO object, or any other object which simulates a file. It defaults
(self, filename=None, mode=None,
compresslevel=_COMPRESS_LEVEL_TRADEOFF, fileobj=None, mtime=None)
| 158 | myfileobj = None |
| 159 | |
| 160 | def __init__(self, filename=None, mode=None, |
| 161 | compresslevel=_COMPRESS_LEVEL_TRADEOFF, fileobj=None, mtime=None): |
| 162 | """Constructor for the GzipFile class. |
| 163 | |
| 164 | At least one of fileobj and filename must be given a |
| 165 | non-trivial value. |
| 166 | |
| 167 | The new class instance is based on fileobj, which can be a regular |
| 168 | file, an io.BytesIO object, or any other object which simulates a file. |
| 169 | It defaults to None, in which case filename is opened to provide |
| 170 | a file object. |
| 171 | |
| 172 | When fileobj is not None, the filename argument is only used to be |
| 173 | included in the gzip file header, which may include the original |
| 174 | filename of the uncompressed file. It defaults to the filename of |
| 175 | fileobj, if discernible; otherwise, it defaults to the empty string, |
| 176 | and in this case the original filename is not included in the header. |
| 177 | |
| 178 | The mode argument can be any of 'r', 'rb', 'a', 'ab', 'w', 'wb', 'x', or |
| 179 | 'xb' depending on whether the file will be read or written. The default |
| 180 | is the mode of fileobj if discernible; otherwise, the default is 'rb'. |
| 181 | A mode of 'r' is equivalent to one of 'rb', and similarly for 'w' and |
| 182 | 'wb', 'a' and 'ab', and 'x' and 'xb'. |
| 183 | |
| 184 | The compresslevel argument is an integer from 0 to 9 controlling the |
| 185 | level of compression; 1 is fastest and produces the least compression, |
| 186 | and 9 is slowest and produces the most compression. 0 is no compression |
| 187 | at all. The default is 9. |
| 188 | |
| 189 | The optional mtime argument is the timestamp requested by gzip. The time |
| 190 | is in Unix format, i.e., seconds since 00:00:00 UTC, January 1, 1970. |
| 191 | If mtime is omitted or None, the current time is used. Use mtime = 0 |
| 192 | to generate a compressed stream that does not depend on creation time. |
| 193 | |
| 194 | """ |
| 195 | |
| 196 | # Ensure attributes exist at __del__ |
| 197 | self.mode = None |
| 198 | self.fileobj = None |
| 199 | self._buffer = None |
| 200 | |
| 201 | if mode and ('t' in mode or 'U' in mode): |
| 202 | raise ValueError("Invalid mode: {!r}".format(mode)) |
| 203 | if mode and 'b' not in mode: |
| 204 | mode += 'b' |
| 205 | |
| 206 | try: |
| 207 | if fileobj is None: |
| 208 | fileobj = self.myfileobj = builtins.open(filename, mode or 'rb') |
| 209 | if filename is None: |
| 210 | filename = getattr(fileobj, 'name', '') |
| 211 | if not isinstance(filename, (str, bytes)): |
| 212 | filename = '' |
| 213 | else: |
| 214 | filename = os.fspath(filename) |
| 215 | origmode = mode |
| 216 | if mode is None: |
| 217 | mode = getattr(fileobj, 'mode', 'rb') |
no test coverage detected