gh-74696: Do not change the current working directory in shutil.make_… · python/cpython@c1bfff4

GitHub

GitHub CopilotWrite better code with AI | MCP RegistryIntegrate external tools | ActionsAutomate any workflow | CodespacesInstant dev environments | IssuesPlan and track work | Code ReviewManage code changes | Code QualityEnforce quality at merge | Why GitHub | Marketplace | View all features | Enterprises | Small and medium teams | Startups | View all use cases | View all industries | View all solutions | AI | Software Development | DevOps | Security | View all topics | Customer stories | Events & webinars | Ebooks & reports | Business insights | Trust center | Partners | View all resources

@@ -51,6 +51,9 @@

5151exceptImportError:

5252_winapi=None

535354+no_chdir=unittest.mock.patch('os.chdir',

55+side_effect=AssertionError("shouldn't call os.chdir()"))

56+5457def_fake_rename(*args, **kwargs):

5558# Pretend the destination path is on a different filesystem.

5659raiseOSError(getattr(errno, 'EXDEV', 18), "Invalid cross-device link")

@@ -1342,7 +1345,7 @@ def test_make_tarball(self):

13421345work_dir=os.path.dirname(tmpdir2)

13431346rel_base_name=os.path.join(os.path.basename(tmpdir2), 'archive')

134413471345-withos_helper.change_cwd(work_dir):

1348+withos_helper.change_cwd(work_dir), no_chdir:

13461349base_name=os.path.abspath(rel_base_name)

13471350tarball=make_archive(rel_base_name, 'gztar', root_dir, '.')

13481351@@ -1356,7 +1359,7 @@ def test_make_tarball(self):

13561359'./file1', './file2', './sub/file3'])

1357136013581361# trying an uncompressed one

1359-withos_helper.change_cwd(work_dir):

1362+withos_helper.change_cwd(work_dir), no_chdir:

13601363tarball=make_archive(rel_base_name, 'tar', root_dir, '.')

13611364self.assertEqual(tarball, base_name+'.tar')

13621365self.assertTrue(os.path.isfile(tarball))

@@ -1392,7 +1395,8 @@ def _create_files(self, base_dir='dist'):

13921395deftest_tarfile_vs_tar(self):

13931396root_dir, base_dir=self._create_files()

13941397base_name=os.path.join(self.mkdtemp(), 'archive')

1395-tarball=make_archive(base_name, 'gztar', root_dir, base_dir)

1398+withno_chdir:

1399+tarball=make_archive(base_name, 'gztar', root_dir, base_dir)

1396140013971401# check if the compressed tarball was created

13981402self.assertEqual(tarball, base_name+'.tar.gz')

@@ -1409,13 +1413,15 @@ def test_tarfile_vs_tar(self):

14091413self.assertEqual(self._tarinfo(tarball), self._tarinfo(tarball2))

1410141414111415# trying an uncompressed one

1412-tarball=make_archive(base_name, 'tar', root_dir, base_dir)

1416+withno_chdir:

1417+tarball=make_archive(base_name, 'tar', root_dir, base_dir)

14131418self.assertEqual(tarball, base_name+'.tar')

14141419self.assertTrue(os.path.isfile(tarball))

1415142014161421# now for a dry_run

1417-tarball=make_archive(base_name, 'tar', root_dir, base_dir,

1418-dry_run=True)

1422+withno_chdir:

1423+tarball=make_archive(base_name, 'tar', root_dir, base_dir,

1424+dry_run=True)

14191425self.assertEqual(tarball, base_name+'.tar')

14201426self.assertTrue(os.path.isfile(tarball))

14211427@@ -1431,7 +1437,7 @@ def test_make_zipfile(self):

14311437work_dir=os.path.dirname(tmpdir2)

14321438rel_base_name=os.path.join(os.path.basename(tmpdir2), 'archive')

143314391434-withos_helper.change_cwd(work_dir):

1440+withos_helper.change_cwd(work_dir), no_chdir:

14351441base_name=os.path.abspath(rel_base_name)

14361442res=make_archive(rel_base_name, 'zip', root_dir)

14371443@@ -1444,7 +1450,7 @@ def test_make_zipfile(self):

14441450'dist/file1', 'dist/file2', 'dist/sub/file3',

14451451'outer'])

144614521447-withos_helper.change_cwd(work_dir):

1453+withos_helper.change_cwd(work_dir), no_chdir:

14481454base_name=os.path.abspath(rel_base_name)

14491455res=make_archive(rel_base_name, 'zip', root_dir, base_dir)

14501456@@ -1462,7 +1468,8 @@ def test_make_zipfile(self):

14621468deftest_zipfile_vs_zip(self):

14631469root_dir, base_dir=self._create_files()

14641470base_name=os.path.join(self.mkdtemp(), 'archive')

1465-archive=make_archive(base_name, 'zip', root_dir, base_dir)

1471+withno_chdir:

1472+archive=make_archive(base_name, 'zip', root_dir, base_dir)

1466147314671474# check if ZIP file was created

14681475self.assertEqual(archive, base_name+'.zip')

@@ -1488,7 +1495,8 @@ def test_zipfile_vs_zip(self):

14881495deftest_unzip_zipfile(self):

14891496root_dir, base_dir=self._create_files()

14901497base_name=os.path.join(self.mkdtemp(), 'archive')

1491-archive=make_archive(base_name, 'zip', root_dir, base_dir)

1498+withno_chdir:

1499+archive=make_archive(base_name, 'zip', root_dir, base_dir)

1492150014931501# check if ZIP file was created

14941502self.assertEqual(archive, base_name+'.zip')

@@ -1546,7 +1554,7 @@ def test_tarfile_root_owner(self):

15461554base_name=os.path.join(self.mkdtemp(), 'archive')

15471555group=grp.getgrgid(0)[0]

15481556owner=pwd.getpwuid(0)[0]

1549-withos_helper.change_cwd(root_dir):

1557+withos_helper.change_cwd(root_dir), no_chdir:

15501558archive_name=make_archive(base_name, 'gztar', root_dir, 'dist',

15511559owner=owner, group=group)

15521560@@ -1564,31 +1572,38 @@ def test_tarfile_root_owner(self):

1564157215651573deftest_make_archive_cwd(self):

15661574current_dir=os.getcwd()

1575+root_dir=self.mkdtemp()

15671576def_breaks(*args, **kw):

15681577raiseRuntimeError()

1578+dirs= []

1579+def_chdir(path):

1580+dirs.append(path)

1581+orig_chdir(path)

1569158215701583register_archive_format('xxx', _breaks, [], 'xxx file')

15711584try:

1572-try:

1573-make_archive('xxx', 'xxx', root_dir=self.mkdtemp())

1574-exceptException:

1575-pass

1585+withsupport.swap_attr(os, 'chdir', _chdir) asorig_chdir:

1586+try:

1587+make_archive('xxx', 'xxx', root_dir=root_dir)

1588+exceptException:

1589+pass

15761590self.assertEqual(os.getcwd(), current_dir)

1591+self.assertEqual(dirs, [root_dir, current_dir])

15771592finally:

15781593unregister_archive_format('xxx')

1579159415801595deftest_make_tarfile_in_curdir(self):

15811596# Issue #21280

15821597root_dir=self.mkdtemp()

1583-withos_helper.change_cwd(root_dir):

1598+withos_helper.change_cwd(root_dir), no_chdir:

15841599self.assertEqual(make_archive('test', 'tar'), 'test.tar')

15851600self.assertTrue(os.path.isfile('test.tar'))

[email protected]_zlib()

15881603deftest_make_zipfile_in_curdir(self):

15891604# Issue #21280

15901605root_dir=self.mkdtemp()

1591-withos_helper.change_cwd(root_dir):

1606+withos_helper.change_cwd(root_dir), no_chdir:

15921607self.assertEqual(make_archive('test', 'zip'), 'test.zip')

15931608self.assertTrue(os.path.isfile('test.zip'))

15941609