[3.13] GH-126606: don't write incomplete pyc files (GH-126627) (GH-126809)

GH-126606: don't write incomplete pyc files (GH-126627)
(cherry picked from commit c695e37a3f)

Co-authored-by: CF Bolz-Tereick <cfbolz@gmx.de>
Co-authored-by: Kirill Podoprigora <kirill.bast9@mail.ru>
Co-authored-by: Brett Cannon <brett@python.org>
This commit is contained in:
Miss Islington (bot) 2024-11-13 23:49:09 +01:00 committed by GitHub
parent 942f807f70
commit ad1b23bf29
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 40 additions and 1 deletions

View file

@ -209,7 +209,11 @@ def _write_atomic(path, data, mode=0o666):
# We first write data to a temporary file, and then use os.replace() to
# perform an atomic rename.
with _io.FileIO(fd, 'wb') as file:
file.write(data)
bytes_written = file.write(data)
if bytes_written != len(data):
# Raise an OSError so the 'except' below cleans up the partially
# written file.
raise OSError("os.write() didn't write the full pyc file")
_os.replace(path_tmp, path)
except OSError:
try: