From 0b01d322023ef1cb9d308dff3b84d0366fa9b687 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?tonghuaroot=20=28=E7=AB=A5=E8=AF=9D=29?= Date: Thu, 12 Nov 2026 15:45:22 +0800 Subject: [PATCH] gh-127172: Honor umask and default ACL when installing venv activation scripts (GH-154850) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit venv wrote the activation scripts with a fixed mode copied from the packaged templates, so they ignored the process umask and any default ACL, unlike every other file venv creates. Copy only the data and let the new files inherit the umask and ACL. (cherry picked from commit 10862056f51eed3a332be3eefd51d4d6e871dade) Co-authored-by: tonghuaroot (童话) --- Lib/test/test_venv.py | 21 +++++++++++++++++++ Lib/venv/__init__.py | 3 +-- ...-07-29-12-20-39.gh-issue-127172.Kf3Xa9.rst | 4 ++++ 3 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-07-29-12-20-39.gh-issue-127172.Kf3Xa9.rst diff --git a/Lib/test/test_venv.py b/Lib/test/test_venv.py index 4b7860e757d79ff..27b609285746950 100644 --- a/Lib/test/test_venv.py +++ b/Lib/test/test_venv.py @@ -383,6 +383,27 @@ def test_install_scripts_selinux(self): venv.create(self.env_dir) listxattr_mock.assert_not_called() + @unittest.skipIf(os.name == 'nt', 'POSIX-specific permission test') + def test_install_scripts_honors_umask(self): + """ + gh-127172: Activation scripts must inherit the process umask (and any + default ACL) like every other created file, rather than a fixed mode + copied from the source template. + """ + old_umask = os.umask(0o002) + try: + venv.create(self.env_dir, with_pip=False) + bindir = os.path.join(self.env_dir, self.bindir) + control = os.path.join(bindir, 'control') + with open(control, 'w'): + pass + expected = os.stat(control).st_mode & 0o777 + for name in ('activate', 'activate.csh', 'activate.fish'): + mode = os.stat(os.path.join(bindir, name)).st_mode & 0o777 + self.assertEqual(mode, expected) + finally: + os.umask(old_umask) + def test_overwrite_existing(self): """ Test creating environment in an existing directory. diff --git a/Lib/venv/__init__.py b/Lib/venv/__init__.py index 0653a43a8b17766..e6c508977d988af 100644 --- a/Lib/venv/__init__.py +++ b/Lib/venv/__init__.py @@ -584,11 +584,10 @@ def skip_file(f): 'may be binary: %s', srcfile, e) continue if new_data == data: - shutil.copy(srcfile, dstfile) + shutil.copyfile(srcfile, dstfile) else: with open(dstfile, 'wb') as f: f.write(new_data) - shutil.copymode(srcfile, dstfile) def upgrade_dependencies(self, context): logger.debug( diff --git a/Misc/NEWS.d/next/Library/2026-07-29-12-20-39.gh-issue-127172.Kf3Xa9.rst b/Misc/NEWS.d/next/Library/2026-07-29-12-20-39.gh-issue-127172.Kf3Xa9.rst new file mode 100644 index 000000000000000..c4bf1f07a2ea577 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-07-29-12-20-39.gh-issue-127172.Kf3Xa9.rst @@ -0,0 +1,4 @@ +Creating a virtual environment with :mod:`venv` now writes the activation +scripts using permissions derived from the process umask (and any default +ACL), consistent with the other files it creates, instead of a fixed mode +copied from the packaged templates. Patch by tonghuaroot.