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 d
(self, filename=None, mode=None,
compresslevel=_COMPRESS_LEVEL_BEST, fileobj=None, mtime=None)
| 132 | myfileobj = None |
| 133 | |
| 134 | def __init__(self, filename=None, mode=None, |
| 135 | compresslevel=_COMPRESS_LEVEL_BEST, fileobj=None, mtime=None): |
| 136 | """Constructor for the GzipFile class. |
| 137 | |
| 138 | At least one of fileobj and filename must be given a |
| 139 | non-trivial value. |
| 140 | |
| 141 | The new class instance is based on fileobj, which can be a regular |
| 142 | file, an io.BytesIO object, or any other object which simulates a file. |
| 143 | It defaults to None, in which case filename is opened to provide |
| 144 | a file object. |
| 145 | |
| 146 | When fileobj is not None, the filename argument is only used to be |
| 147 | included in the gzip file header, which may include the original |
| 148 | filename of the uncompressed file. It defaults to the filename of |
| 149 | fileobj, if discernible; otherwise, it defaults to the empty string, |
| 150 | and in this case the original filename is not included in the header. |
| 151 | |
| 152 | The mode argument can be any of 'r', 'rb', 'a', 'ab', 'w', 'wb', 'x', or |
| 153 | 'xb' depending on whether the file will be read or written. The default |
| 154 | is the mode of fileobj if discernible; otherwise, the default is 'rb'. |
| 155 | A mode of 'r' is equivalent to one of 'rb', and similarly for 'w' and |
| 156 | 'wb', 'a' and 'ab', and 'x' and 'xb'. |
| 157 | |
| 158 | The compresslevel argument is an integer from 0 to 9 controlling the |
| 159 | level of compression; 1 is fastest and produces the least compression, |
| 160 | and 9 is slowest and produces the most compression. 0 is no compression |
| 161 | at all. The default is 9. |
| 162 | |
| 163 | The mtime argument is an optional numeric timestamp to be written |
| 164 | to the last modification time field in the stream when compressing. |
| 165 | If omitted or None, the current time is used. |
| 166 | |
| 167 | """ |
| 168 | |
| 169 | if mode and ('t' in mode or 'U' in mode): |
| 170 | raise ValueError("Invalid mode: {!r}".format(mode)) |
| 171 | if mode and 'b' not in mode: |
| 172 | mode += 'b' |
| 173 | if fileobj is None: |
| 174 | fileobj = self.myfileobj = builtins.open(filename, mode or 'rb') |
| 175 | if filename is None: |
| 176 | filename = getattr(fileobj, 'name', '') |
| 177 | if not isinstance(filename, (str, bytes)): |
| 178 | filename = '' |
| 179 | else: |
| 180 | filename = os.fspath(filename) |
| 181 | origmode = mode |
| 182 | if mode is None: |
| 183 | mode = getattr(fileobj, 'mode', 'rb') |
| 184 | |
| 185 | if mode.startswith('r'): |
| 186 | self.mode = READ |
| 187 | raw = _GzipReader(fileobj) |
| 188 | self._buffer = io.BufferedReader(raw) |
| 189 | self.name = filename |
| 190 | |
| 191 | elif mode.startswith(('w', 'a', 'x')): |
no test coverage detected