summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBerker Peksag <berker.peksag@gmail.com>2016-10-02 13:49:05 +0300
committerBerker Peksag <berker.peksag@gmail.com>2016-10-02 13:49:05 +0300
commitc8ce8434e08e95b94ba9a11518b0e5388d0accd5 (patch)
treeb538b1b84fc75460e4e3e1856e7e072eba6c54c4
parent419968c235ce0a03041470366d3f5ca57c657433 (diff)
parent03020cfa97b6c2c80f50fb2d07025aff49c3513c (diff)
downloadcpython-git-c8ce8434e08e95b94ba9a11518b0e5388d0accd5.tar.gz
Issue #28227: Merge from 3.6
-rw-r--r--Doc/library/gzip.rst5
-rw-r--r--Lib/gzip.py4
-rw-r--r--Lib/test/test_gzip.py22
-rw-r--r--Misc/NEWS2
4 files changed, 32 insertions, 1 deletions
diff --git a/Doc/library/gzip.rst b/Doc/library/gzip.rst
index 792d57a8a3..9c6b722371 100644
--- a/Doc/library/gzip.rst
+++ b/Doc/library/gzip.rst
@@ -56,6 +56,8 @@ The module defines the following items:
.. 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)
@@ -151,6 +153,9 @@ The module defines the following items:
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)
diff --git a/Lib/gzip.py b/Lib/gzip.py
index ddf7668d90..76ab497853 100644
--- a/Lib/gzip.py
+++ b/Lib/gzip.py
@@ -49,7 +49,7 @@ def open(filename, mode="rb", compresslevel=9,
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)
@@ -165,6 +165,8 @@ class GzipFile(_compression.BaseStream):
filename = getattr(fileobj, 'name', '')
if not isinstance(filename, (str, bytes)):
filename = ''
+ else:
+ filename = os.fspath(filename)
if mode is None:
mode = getattr(fileobj, 'mode', 'rb')
diff --git a/Lib/test/test_gzip.py b/Lib/test/test_gzip.py
index 07a9f6ff44..b457bd3f44 100644
--- a/Lib/test/test_gzip.py
+++ b/Lib/test/test_gzip.py
@@ -5,6 +5,7 @@ import unittest
from test import support
from test.support import bigmemtest, _4G
import os
+import pathlib
import io
import struct
import array
@@ -67,6 +68,18 @@ class TestGzip(BaseTest):
# 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)
+ self.assertIsInstance(f.name, str)
+ 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.
@@ -521,6 +534,15 @@ class TestOpen(BaseTest):
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
diff --git a/Misc/NEWS b/Misc/NEWS
index 7d13a33a9c..03036a8e28 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -54,6 +54,8 @@ Core and Builtins
Library
-------
+- Issue #28227: gzip now supports pathlib. Patch by Ethan Furman.
+
- Issue #28332: Deprecated silent truncations in socket.htons and socket.ntohs.
Original patch by Oren Milman.