MCPcopy Index your code
hub / github.com/python/cpython / open

Function open

Lib/gzip.py:33–76  ·  view source on GitHub ↗

Open a gzip-compressed file in binary or text mode. The filename argument can be an actual filename (a str or bytes object), or an existing file object to read from or write to. The mode argument can be "r", "rb", "w", "wb", "x", "xb", "a" or "ab" for binary mode, or "rt", "wt", "x

(filename, mode="rb", compresslevel=_COMPRESS_LEVEL_TRADEOFF,
         encoding=None, errors=None, newline=None)

Source from the content-addressed store, hash-verified

31
32
33def open(filename, mode="rb", compresslevel=_COMPRESS_LEVEL_TRADEOFF,
34 encoding=None, errors=None, newline=None):
35 """Open a gzip-compressed file in binary or text mode.
36
37 The filename argument can be an actual filename (a str or bytes object), or
38 an existing file object to read from or write to.
39
40 The mode argument can be "r", "rb", "w", "wb", "x", "xb", "a" or "ab" for
41 binary mode, or "rt", "wt", "xt" or "at" for text mode. The default mode is
42 "rb", and the default compresslevel is 9.
43
44 For binary mode, this function is equivalent to the GzipFile constructor:
45 GzipFile(filename, mode, compresslevel). In this case, the encoding, errors
46 and newline arguments must not be provided.
47
48 For text mode, a GzipFile object is created, and wrapped in an
49 io.TextIOWrapper instance with the specified encoding, error handling
50 behavior, and line ending(s).
51
52 """
53 if "t" in mode:
54 if "b" in mode:
55 raise ValueError("Invalid mode: %r" % (mode,))
56 else:
57 if encoding is not None:
58 raise ValueError("Argument 'encoding' not supported in binary mode")
59 if errors is not None:
60 raise ValueError("Argument 'errors' not supported in binary mode")
61 if newline is not None:
62 raise ValueError("Argument 'newline' not supported in binary mode")
63
64 gz_mode = mode.replace("t", "")
65 if isinstance(filename, (str, bytes, os.PathLike)):
66 binary_file = GzipFile(filename, gz_mode, compresslevel)
67 elif hasattr(filename, "read") or hasattr(filename, "write"):
68 binary_file = GzipFile(None, gz_mode, compresslevel, filename)
69 else:
70 raise TypeError("filename must be a str or bytes object, or a file")
71
72 if "t" in mode:
73 encoding = io.text_encoding(encoding)
74 return io.TextIOWrapper(binary_file, encoding, errors, newline)
75 else:
76 return binary_file
77
78def write32u(output, value):
79 # The L format writes the bit pattern correctly whether signed

Callers 1

mainFunction · 0.70

Calls 2

GzipFileClass · 0.85
replaceMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…