bpo-30218: support path-like objects in shutil.unpack_archive() (GH-1… · python/cpython@a12df7b
@@ -10,6 +10,7 @@
1010import os.path
1111import errno
1212import functools
13+import pathlib
1314import subprocess
1415from shutil import (make_archive,
1516register_archive_format, unregister_archive_format,
@@ -1223,6 +1224,18 @@ def test_register_archive_format(self):
12231224self.assertNotIn('xxx', formats)
1224122512251226def 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):
12261239root_dir, base_dir = self._create_files()
12271240expected = rlistdir(root_dir)
12281241expected.remove('outer')
@@ -1232,16 +1245,16 @@ def check_unpack_archive(self, format):
1232124512331246# let's try to unpack it now
12341247tmpdir2 = self.mkdtemp()
1235-unpack_archive(filename, tmpdir2)
1248+unpack_archive(converter(filename), converter(tmpdir2))
12361249self.assertEqual(rlistdir(tmpdir2), expected)
1237125012381251# and again, this time with the format specified
12391252tmpdir3 = self.mkdtemp()
1240-unpack_archive(filename, tmpdir3, format=format)
1253+unpack_archive(converter(filename), converter(tmpdir3), format=format)
12411254self.assertEqual(rlistdir(tmpdir3), expected)
124212551243-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')
1245125812461259def test_unpack_archive_tar(self):
12471260self.check_unpack_archive('tar')