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

* Use open() to write the bytecode
* Convert to unittest style asserts
* Tweak news, thanks @vstinner
* Tidy
* reword NEWS, avoid word "retried"
This commit is contained in:
Stefano Rivera 2025-11-27 11:17:59 -08:00 committed by GitHub
parent 69f54ce452
commit 656a64b37f
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: