diff --git a/Doc/library/gzip.rst b/Doc/library/gzip.rst --- a/Doc/library/gzip.rst +++ b/Doc/library/gzip.rst @@ -51,16 +51,18 @@ The module defines the following items: .. versionchanged:: 3.3 Added support for *filename* being a file object, support for text mode, and the *encoding*, *errors* and *newline* arguments. .. versionchanged:: 3.4 Added support for the ``'x'``, ``'xb'`` and ``'xt'`` modes. + .. versionchanged:: 3.6 + Accepts a :term:`path-like object`. .. class:: GzipFile(filename=None, mode=None, compresslevel=9, fileobj=None, mtime=None) Constructor for the :class:`GzipFile` class, which simulates most of the methods of a :term:`file object`, with the exception of the :meth:`truncate` method. At least one of *fileobj* and *filename* must be given a non-trivial value. @@ -146,16 +148,19 @@ The module defines the following items: Added support for the ``'x'`` and ``'xb'`` modes. .. versionchanged:: 3.5 Added support for writing arbitrary :term:`bytes-like objects `. The :meth:`~io.BufferedIOBase.read` method now accepts an argument of ``None``. + .. versionchanged:: 3.6 + Accepts a :term:`path-like object`. + .. function:: compress(data, compresslevel=9) Compress the *data*, returning a :class:`bytes` object containing the compressed data. *compresslevel* has the same meaning as in the :class:`GzipFile` constructor above. .. versionadded:: 3.2 diff --git a/Lib/gzip.py b/Lib/gzip.py --- a/Lib/gzip.py +++ b/Lib/gzip.py @@ -44,17 +44,17 @@ def open(filename, mode="rb", compressle 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") gz_mode = mode.replace("t", "") - if isinstance(filename, (str, bytes)): + if isinstance(filename, (str, bytes, os.PathLike)): 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) @@ -167,17 +167,17 @@ class GzipFile(_compression.BaseStream): filename = '' 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 + self.name = os.fspath(filename) elif mode.startswith(('w', 'a', 'x')): self.mode = WRITE self._init_write(filename) self.compress = zlib.compressobj(compresslevel, zlib.DEFLATED, -zlib.MAX_WBITS, zlib.DEF_MEM_LEVEL, diff --git a/Lib/test/test_gzip.py b/Lib/test/test_gzip.py --- a/Lib/test/test_gzip.py +++ b/Lib/test/test_gzip.py @@ -1,15 +1,16 @@ """Test script for the gzip module. """ import unittest from test import support from test.support import bigmemtest, _4G import os +import pathlib import io import struct import array gzip = support.import_module('gzip') data1 = b""" int length=DEFAULTALLOC, err = Z_OK; PyObject *RetVal; int flushmode = Z_FINISH; @@ -62,16 +63,27 @@ class TestGzip(BaseTest): f.fileno() if hasattr(os, 'fsync'): os.fsync(f.fileno()) f.close() # Test multiple close() calls. f.close() + def test_write_read_with_pathlike_file(self): + filename = pathlib.Path(self.filename) + with gzip.GzipFile(filename, 'w') as f: + f.write(data1 * 50) + with gzip.GzipFile(filename, 'a') as f: + f.write(data1) + with gzip.GzipFile(filename) as f: + d = f.read() + self.assertEqual(d, data1 * 51) + self.assertIsInstance(f.name, str) + # The following test_write_xy methods test that write accepts # the corresponding bytes-like object type as input # and that the data written equals bytes(xy) in all cases. def test_write_memoryview(self): self.write_and_read_back(memoryview(data1 * 50)) m = memoryview(bytes(range(256))) data = m.cast('B', shape=[8,8,4]) self.write_and_read_back(data) @@ -516,16 +528,25 @@ class TestOpen(BaseTest): gzip.open(self.filename, "xb") support.unlink(self.filename) with gzip.open(self.filename, "xb") as f: f.write(uncompressed) with open(self.filename, "rb") as f: file_data = gzip.decompress(f.read()) self.assertEqual(file_data, uncompressed) + def test_pathlike_file(self): + filename = pathlib.Path(self.filename) + with gzip.open(filename, "wb") as f: + f.write(data1 * 50) + with gzip.open(filename, "ab") as f: + f.write(data1) + with gzip.open(filename) as f: + self.assertEqual(f.read(), data1 * 51) + def test_implicit_binary_modes(self): # Test implicit binary modes (no "b" or "t" in mode string). uncompressed = data1 * 50 with gzip.open(self.filename, "w") as f: f.write(uncompressed) with open(self.filename, "rb") as f: file_data = gzip.decompress(f.read())