From 9ca6ba11994687997492c78b967b1ec70fea6d22 Mon Sep 17 00:00:00 2001 From: Tony Mountifield Date: Wed, 3 Apr 2024 17:37:35 +0100 Subject: [PATCH] gh-117467: Add preserving of mailbox owner on flush --- Lib/mailbox.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Lib/mailbox.py b/Lib/mailbox.py index 746811bd559412..229d55bd00c2e0 100644 --- a/Lib/mailbox.py +++ b/Lib/mailbox.py @@ -750,9 +750,13 @@ def flush(self): _sync_close(new_file) # self._file is about to get replaced, so no need to sync. self._file.close() - # Make sure the new file's mode is the same as the old file's - mode = os.stat(self._path).st_mode - os.chmod(new_file.name, mode) + # Make sure the new file's mode and owner are the same as the old file's + info = os.stat(self._path) + os.chmod(new_file.name, info.st_mode) + try: + os.chown(new_file.name, info.st_uid, info.st_gid) + except: + pass try: os.rename(new_file.name, self._path) except FileExistsError: