From c3a2a9ffe216c9f44931dadbd5094d14d3ec6aa9 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Sun, 30 Apr 2017 14:58:50 -0700 Subject: [PATCH 1/5] bpo-30218: support PathLike in shutil.unpack_archive --- Lib/shutil.py | 2 ++ Lib/test/test_shutil.py | 21 +++++++++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/Lib/shutil.py b/Lib/shutil.py index 31536fe6327069..b50310e9df1454 100644 --- a/Lib/shutil.py +++ b/Lib/shutil.py @@ -958,6 +958,8 @@ def unpack_archive(filename, extract_dir=None, format=None): if extract_dir is None: extract_dir = os.getcwd() + filename = os.fspath(filename) + if format is not None: try: format_info = _UNPACK_FORMATS[format] diff --git a/Lib/test/test_shutil.py b/Lib/test/test_shutil.py index c7f7d1d3e6157f..f21e6adf4abad0 100644 --- a/Lib/test/test_shutil.py +++ b/Lib/test/test_shutil.py @@ -10,6 +10,7 @@ import os.path import errno import functools +import pathlib import subprocess from shutil import (make_archive, register_archive_format, unregister_archive_format, @@ -1223,6 +1224,18 @@ def test_register_archive_format(self): self.assertNotIn('xxx', formats) def check_unpack_archive(self, format): + self.check_unpack_archive_with_converter(format, lambda path: path) + self.check_unpack_archive_with_converter(format, pathlib.Path) + + class MyPath: + def __init__(self, path): + self.path = path + def __fspath__(self): + return self.path + + self.check_unpack_archive_with_converter(format, MyPath) + + def check_unpack_archive_with_converter(self, format, converter): root_dir, base_dir = self._create_files() expected = rlistdir(root_dir) expected.remove('outer') @@ -1232,16 +1245,16 @@ def check_unpack_archive(self, format): # let's try to unpack it now tmpdir2 = self.mkdtemp() - unpack_archive(filename, tmpdir2) + unpack_archive(converter(filename), converter(tmpdir2)) self.assertEqual(rlistdir(tmpdir2), expected) # and again, this time with the format specified tmpdir3 = self.mkdtemp() - unpack_archive(filename, tmpdir3, format=format) + unpack_archive(converter(filename), converter(tmpdir3), format=format) self.assertEqual(rlistdir(tmpdir3), expected) - self.assertRaises(shutil.ReadError, unpack_archive, TESTFN) - self.assertRaises(ValueError, unpack_archive, TESTFN, format='xxx') + self.assertRaises(shutil.ReadError, unpack_archive, converter(TESTFN)) + self.assertRaises(ValueError, unpack_archive, converter(TESTFN), format='xxx') def test_unpack_archive_tar(self): self.check_unpack_archive('tar') From 3bd1c432a97a99d2d3dde6079a9a7d348ca27027 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Tue, 2 May 2017 10:55:09 -0700 Subject: [PATCH 2/5] call fspath on extract_dir too --- Lib/shutil.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/shutil.py b/Lib/shutil.py index b50310e9df1454..464ee912f5ccd8 100644 --- a/Lib/shutil.py +++ b/Lib/shutil.py @@ -958,6 +958,7 @@ def unpack_archive(filename, extract_dir=None, format=None): if extract_dir is None: extract_dir = os.getcwd() + extract_dir = os.fspath(extract_dir) filename = os.fspath(filename) if format is not None: From e3dcfadabd51f999ffb88f2b57385cbdf3eac94e Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Tue, 2 May 2017 20:16:19 -0700 Subject: [PATCH 3/5] add Misc/NEWS entry --- Misc/NEWS | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Misc/NEWS b/Misc/NEWS index 026beff1954e5e..0df8fd1008492d 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -33,6 +33,9 @@ Core and Builtins ValueError always is raised rather than OverflowError for negative counts. Shifting zero with non-negative count always returns zero. +- bpo-30218: Fix PathLike support for shutil.unpack_archive. Patch by Jelle + Zijlstra. + - bpo-24821: Fixed the slowing down to 25 times in the searching of some unlucky Unicode characters. From 62931747305dec19391645869ab6306ee89d9490 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Tue, 2 May 2017 21:18:53 -0700 Subject: [PATCH 4/5] put it in the right section --- Misc/NEWS | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Misc/NEWS b/Misc/NEWS index 0df8fd1008492d..11e77ea619f870 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -33,9 +33,6 @@ Core and Builtins ValueError always is raised rather than OverflowError for negative counts. Shifting zero with non-negative count always returns zero. -- bpo-30218: Fix PathLike support for shutil.unpack_archive. Patch by Jelle - Zijlstra. - - bpo-24821: Fixed the slowing down to 25 times in the searching of some unlucky Unicode characters. @@ -338,6 +335,9 @@ Library Fixed possible other errors caused by not checking results of PyObject_Size(), PySequence_Size(), or PyMapping_Size(). +- bpo-30218: Fix PathLike support for shutil.unpack_archive. Patch by Jelle + Zijlstra. + - bpo-10076: Compiled regular expression and match objects in the re module now support copy.copy() and copy.deepcopy() (they are considered atomic). From 11a987a71017a6e361fa3715fac09411bca1cc68 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Thu, 4 May 2017 21:25:24 -0700 Subject: [PATCH 5/5] add doc note --- Doc/library/shutil.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Doc/library/shutil.rst b/Doc/library/shutil.rst index 41e5bafa53d468..2b84fa2937284c 100644 --- a/Doc/library/shutil.rst +++ b/Doc/library/shutil.rst @@ -550,6 +550,9 @@ provided. They rely on the :mod:`zipfile` and :mod:`tarfile` modules. registered for that extension. In case none is found, a :exc:`ValueError` is raised. + .. versionchanged:: 3.7 + Accepts a :term:`path-like object` for *filename* and *extract_dir*. + .. function:: register_unpack_format(name, extensions, function[, extra_args[, description]])