diff options
author | shadchin <shadchin@yandex-team.com> | 2025-06-13 00:05:26 +0300 |
---|---|---|
committer | shadchin <shadchin@yandex-team.com> | 2025-06-13 00:35:30 +0300 |
commit | 796b9088366b10b4cd42885101fc20c0b5709b07 (patch) | |
tree | f287eacb0b95ffd7cabf95b16cafb4788645dc38 /contrib/tools/python3/Lib/gzip.py | |
parent | c72bca862651e507d2ff4980ef7f4ff7267a7227 (diff) | |
download | ydb-796b9088366b10b4cd42885101fc20c0b5709b07.tar.gz |
Update Python 3 to 3.12.10
commit_hash:dd2398e159fe1d72ea6b12da52fccc933a41a785
Diffstat (limited to 'contrib/tools/python3/Lib/gzip.py')
-rw-r--r-- | contrib/tools/python3/Lib/gzip.py | 123 |
1 files changed, 70 insertions, 53 deletions
diff --git a/contrib/tools/python3/Lib/gzip.py b/contrib/tools/python3/Lib/gzip.py index 177f9080dc5..ecfe3a9cd7e 100644 --- a/contrib/tools/python3/Lib/gzip.py +++ b/contrib/tools/python3/Lib/gzip.py @@ -5,11 +5,15 @@ but random access is not allowed.""" # based on Andrew Kuchling's minigzip.py distributed with the zlib module -import struct, sys, time, os -import zlib +import _compression import builtins import io -import _compression +import os +import struct +import sys +import time +import weakref +import zlib __all__ = ["BadGzipFile", "GzipFile", "open", "compress", "decompress"] @@ -124,10 +128,13 @@ class BadGzipFile(OSError): class _WriteBufferStream(io.RawIOBase): """Minimal object to pass WriteBuffer flushes into GzipFile""" def __init__(self, gzip_file): - self.gzip_file = gzip_file + self.gzip_file = weakref.ref(gzip_file) def write(self, data): - return self.gzip_file._write_raw(data) + gzip_file = self.gzip_file() + if gzip_file is None: + raise RuntimeError("lost gzip_file") + return gzip_file._write_raw(data) def seekable(self): return False @@ -188,51 +195,58 @@ class GzipFile(_compression.BaseStream): raise ValueError("Invalid mode: {!r}".format(mode)) if mode and 'b' not in mode: mode += 'b' - if fileobj is None: - fileobj = self.myfileobj = builtins.open(filename, mode or 'rb') - if filename is None: - filename = getattr(fileobj, 'name', '') - if not isinstance(filename, (str, bytes)): - filename = '' - else: - filename = os.fspath(filename) - origmode = mode - if mode is None: - mode = getattr(fileobj, 'mode', 'rb') - - - if mode.startswith('r'): - self.mode = READ - raw = _GzipReader(fileobj) - self._buffer = io.BufferedReader(raw) - self.name = filename - - elif mode.startswith(('w', 'a', 'x')): - if origmode is None: - import warnings - warnings.warn( - "GzipFile was opened for writing, but this will " - "change in future Python releases. " - "Specify the mode argument for opening it for writing.", - FutureWarning, 2) - self.mode = WRITE - self._init_write(filename) - self.compress = zlib.compressobj(compresslevel, - zlib.DEFLATED, - -zlib.MAX_WBITS, - zlib.DEF_MEM_LEVEL, - 0) - self._write_mtime = mtime - self._buffer_size = _WRITE_BUFFER_SIZE - self._buffer = io.BufferedWriter(_WriteBufferStream(self), - buffer_size=self._buffer_size) - else: - raise ValueError("Invalid mode: {!r}".format(mode)) - self.fileobj = fileobj + try: + if fileobj is None: + fileobj = self.myfileobj = builtins.open(filename, mode or 'rb') + if filename is None: + filename = getattr(fileobj, 'name', '') + if not isinstance(filename, (str, bytes)): + filename = '' + else: + filename = os.fspath(filename) + origmode = mode + if mode is None: + mode = getattr(fileobj, 'mode', 'rb') + + + if mode.startswith('r'): + self.mode = READ + raw = _GzipReader(fileobj) + self._buffer = io.BufferedReader(raw) + self.name = filename + + elif mode.startswith(('w', 'a', 'x')): + if origmode is None: + import warnings + warnings.warn( + "GzipFile was opened for writing, but this will " + "change in future Python releases. " + "Specify the mode argument for opening it for writing.", + FutureWarning, 2) + self.mode = WRITE + self._init_write(filename) + self.compress = zlib.compressobj(compresslevel, + zlib.DEFLATED, + -zlib.MAX_WBITS, + zlib.DEF_MEM_LEVEL, + 0) + self._write_mtime = mtime + self._buffer_size = _WRITE_BUFFER_SIZE + self._buffer = io.BufferedWriter(_WriteBufferStream(self), + buffer_size=self._buffer_size) + else: + raise ValueError("Invalid mode: {!r}".format(mode)) - if self.mode == WRITE: - self._write_gzip_header(compresslevel) + self.fileobj = fileobj + + if self.mode == WRITE: + self._write_gzip_header(compresslevel) + except: + # Avoid a ResourceWarning if the write fails, + # eg read-only file or KeyboardInterrupt + self._close() + raise @property def mtime(self): @@ -361,11 +375,14 @@ class GzipFile(_compression.BaseStream): elif self.mode == READ: self._buffer.close() finally: - self.fileobj = None - myfileobj = self.myfileobj - if myfileobj: - self.myfileobj = None - myfileobj.close() + self._close() + + def _close(self): + self.fileobj = None + myfileobj = self.myfileobj + if myfileobj is not None: + self.myfileobj = None + myfileobj.close() def flush(self,zlib_mode=zlib.Z_SYNC_FLUSH): self._check_not_closed() |