summaryrefslogtreecommitdiffstats
path: root/Lib/gzip.py
diff options
context:
space:
mode:
authorNadeem Vawda <nadeem.vawda@gmail.com>2012-05-06 13:04:01 (GMT)
committerNadeem Vawda <nadeem.vawda@gmail.com>2012-05-06 13:04:01 (GMT)
commit7e126205e615518bc921132918c0ed7f48b31c85 (patch)
tree02d4ca50befae0f0f08494be89c519c12dc51a35 /Lib/gzip.py
parent54c74ece28d707176fa375ab65f5be229fc4db0a (diff)
downloadcpython-7e126205e615518bc921132918c0ed7f48b31c85.zip
cpython-7e126205e615518bc921132918c0ed7f48b31c85.tar.gz
cpython-7e126205e615518bc921132918c0ed7f48b31c85.tar.bz2
Closes #13989: Add support for text modes to gzip.open().
Also, add tests for gzip.open().
Diffstat (limited to 'Lib/gzip.py')
-rw-r--r--Lib/gzip.py42
1 files changed, 33 insertions, 9 deletions
diff --git a/Lib/gzip.py b/Lib/gzip.py
index 85c3e15..2f53aa8 100644
--- a/Lib/gzip.py
+++ b/Lib/gzip.py
@@ -16,6 +16,39 @@ FTEXT, FHCRC, FEXTRA, FNAME, FCOMMENT = 1, 2, 4, 8, 16
READ, WRITE = 1, 2
+def open(filename, mode="rb", compresslevel=9,
+ encoding=None, errors=None, newline=None):
+ """Open a gzip-compressed file in binary or text mode.
+
+ The mode argument can be "r", "rb", "w", "wb", "a" or "ab" for binary mode,
+ or "rt", "wt" or "at" for text mode. The default mode is "rb", and the
+ default compresslevel is 9.
+
+ For binary mode, this function is equivalent to the GzipFile constructor:
+ GzipFile(filename, mode, compresslevel). In this case, the encoding, errors
+ and newline arguments must not be provided.
+
+ For text mode, a GzipFile object is created, and wrapped in an
+ io.TextIOWrapper instance with the specified encoding, error handling
+ behavior, and line ending(s).
+
+ """
+ if "t" in mode:
+ if "b" in mode:
+ raise ValueError("Invalid mode: %r" % (mode,))
+ else:
+ if encoding is not None:
+ raise ValueError("Argument 'encoding' not supported in binary mode")
+ if errors is not None:
+ raise ValueError("Argument 'errors' not supported in binary mode")
+ if newline is not None:
+ raise ValueError("Argument 'newline' not supported in binary mode")
+ binary_file = GzipFile(filename, mode.replace("t", ""), compresslevel)
+ if "t" in mode:
+ return io.TextIOWrapper(binary_file, encoding, errors, newline)
+ else:
+ return binary_file
+
def write32u(output, value):
# The L format writes the bit pattern correctly whether signed
# or unsigned.
@@ -24,15 +57,6 @@ def write32u(output, value):
def read32(input):
return struct.unpack("<I", input.read(4))[0]
-def open(filename, mode="rb", compresslevel=9):
- """Shorthand for GzipFile(filename, mode, compresslevel).
-
- The filename argument is required; mode defaults to 'rb'
- and compresslevel defaults to 9.
-
- """
- return GzipFile(filename, mode, compresslevel)
-
class _PaddedFile:
"""Minimal read-only file object that prepends a string to the contents
of an actual file. Shouldn't be used outside of gzip.py, as it lacks