Skip to content

Commit a12df7b

Browse files
JelleZijlstrabrettcannon
authored andcommitted
bpo-30218: support path-like objects in shutil.unpack_archive() (pythonGH-1367)
Thanks to Jelle Zijlstra for the patch.
1 parent a1054c3 commit a12df7b

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

Doc/library/shutil.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,9 @@ provided. They rely on the :mod:`zipfile` and :mod:`tarfile` modules.
550550
registered for that extension. In case none is found,
551551
a :exc:`ValueError` is raised.
552552

553+
.. versionchanged:: 3.7
554+
Accepts a :term:`path-like object` for *filename* and *extract_dir*.
555+
553556

554557
.. function:: register_unpack_format(name, extensions, function[, extra_args[, description]])
555558

Lib/shutil.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,9 @@ def unpack_archive(filename, extract_dir=None, format=None):
958958
if extract_dir is None:
959959
extract_dir = os.getcwd()
960960

961+
extract_dir = os.fspath(extract_dir)
962+
filename = os.fspath(filename)
963+
961964
if format is not None:
962965
try:
963966
format_info = _UNPACK_FORMATS[format]

Lib/test/test_shutil.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import os.path
1111
import errno
1212
import functools
13+
import pathlib
1314
import subprocess
1415
from shutil import (make_archive,
1516
register_archive_format, unregister_archive_format,
@@ -1223,6 +1224,18 @@ def test_register_archive_format(self):
12231224
self.assertNotIn('xxx', formats)
12241225

12251226
def check_unpack_archive(self, format):
1227+
self.check_unpack_archive_with_converter(format, lambda path: path)
1228+
self.check_unpack_archive_with_converter(format, pathlib.Path)
1229+
1230+
class MyPath:
1231+
def __init__(self, path):
1232+
self.path = path
1233+
def __fspath__(self):
1234+
return self.path
1235+
1236+
self.check_unpack_archive_with_converter(format, MyPath)
1237+
1238+
def check_unpack_archive_with_converter(self, format, converter):
12261239
root_dir, base_dir = self._create_files()
12271240
expected = rlistdir(root_dir)
12281241
expected.remove('outer')
@@ -1232,16 +1245,16 @@ def check_unpack_archive(self, format):
12321245

12331246
# let's try to unpack it now
12341247
tmpdir2 = self.mkdtemp()
1235-
unpack_archive(filename, tmpdir2)
1248+
unpack_archive(converter(filename), converter(tmpdir2))
12361249
self.assertEqual(rlistdir(tmpdir2), expected)
12371250

12381251
# and again, this time with the format specified
12391252
tmpdir3 = self.mkdtemp()
1240-
unpack_archive(filename, tmpdir3, format=format)
1253+
unpack_archive(converter(filename), converter(tmpdir3), format=format)
12411254
self.assertEqual(rlistdir(tmpdir3), expected)
12421255

1243-
self.assertRaises(shutil.ReadError, unpack_archive, TESTFN)
1244-
self.assertRaises(ValueError, unpack_archive, TESTFN, format='xxx')
1256+
self.assertRaises(shutil.ReadError, unpack_archive, converter(TESTFN))
1257+
self.assertRaises(ValueError, unpack_archive, converter(TESTFN), format='xxx')
12451258

12461259
def test_unpack_archive_tar(self):
12471260
self.check_unpack_archive('tar')

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,9 @@ Library
362362
Fixed possible other errors caused by not checking results of PyObject_Size(),
363363
PySequence_Size(), or PyMapping_Size().
364364

365+
- bpo-30218: Fix PathLike support for shutil.unpack_archive. Patch by Jelle
366+
Zijlstra.
367+
365368
- bpo-10076: Compiled regular expression and match objects in the re module
366369
now support copy.copy() and copy.deepcopy() (they are considered atomic).
367370

0 commit comments

Comments
 (0)