From 68721019efb16ba8acad036c331a9a195d6f7da0 Mon Sep 17 00:00:00 2001 From: Nadeem Vawda Date: Mon, 4 Jun 2012 23:21:38 +0200 Subject: Add fileobj support to gzip.open(). --- Lib/gzip.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'Lib/gzip.py') diff --git a/Lib/gzip.py b/Lib/gzip.py index 2f53aa8aacf..412bf05495a 100644 --- a/Lib/gzip.py +++ b/Lib/gzip.py @@ -20,6 +20,9 @@ def open(filename, mode="rb", compresslevel=9, encoding=None, errors=None, newline=None): """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", "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. @@ -43,7 +46,15 @@ def open(filename, mode="rb", compresslevel=9, 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) + + gz_mode = mode.replace("t", "") + if isinstance(filename, (str, bytes)): + binary_file = GzipFile(filename, gz_mode, compresslevel) + elif hasattr(filename, "read") or hasattr(filename, "write"): + binary_file = GzipFile(None, gz_mode, compresslevel, filename) + else: + raise TypeError("filename must be a str or bytes object, or a file") + if "t" in mode: return io.TextIOWrapper(binary_file, encoding, errors, newline) else: -- cgit v1.2.3