[3.14] gh-141930: Use the regular IO stack to write .pyc files for a better error message on failure (GH-141931) (#142021)

Co-authored-by: Stefano Rivera <stefano@rivera.za.net>
This commit is contained in:
Miss Islington (bot) 2025-12-01 17:43:50 +01:00 committed by GitHub
parent 521fb36561
commit afce34fbf0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 59 additions and 22 deletions

View file

@ -208,12 +208,8 @@ def _write_atomic(path, data, mode=0o666):
try:
# 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:
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")
with _io.open(fd, 'wb') as file:
file.write(data)
_os.replace(path_tmp, path)
except OSError:
try: