Skip to content

Commit df59b64

Browse files
miss-islingtoncfbolzEclips4brettcannon
authored
[3.12] GH-126606: don't write incomplete pyc files (GH-126627) (GH-126810)
GH-126606: don't write incomplete pyc files (GH-126627) (cherry picked from commit c695e37) Co-authored-by: CF Bolz-Tereick <[email protected]> Co-authored-by: Kirill Podoprigora <[email protected]> Co-authored-by: Brett Cannon <[email protected]>
1 parent 9e86d21 commit df59b64

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

Lib/importlib/_bootstrap_external.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,11 @@ def _write_atomic(path, data, mode=0o666):
204204
# We first write data to a temporary file, and then use os.replace() to
205205
# perform an atomic rename.
206206
with _io.FileIO(fd, 'wb') as file:
207-
file.write(data)
207+
bytes_written = file.write(data)
208+
if bytes_written != len(data):
209+
# Raise an OSError so the 'except' below cleans up the partially
210+
# written file.
211+
raise OSError("os.write() didn't write the full pyc file")
208212
_os.replace(path_tmp, path)
209213
except OSError:
210214
try:

Lib/test/test_importlib/test_util.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
importlib_util = util.import_importlib('importlib.util')
77

88
import importlib.util
9+
from importlib import _bootstrap_external
910
import os
1011
import pathlib
1112
import re
1213
import string
1314
import sys
1415
from test import support
16+
from test.support import os_helper
1517
import textwrap
1618
import types
1719
import unittest
@@ -758,5 +760,35 @@ def test_complete_multi_phase_init_module(self):
758760
self.run_with_own_gil(script)
759761

760762

763+
class MiscTests(unittest.TestCase):
764+
def test_atomic_write_should_notice_incomplete_writes(self):
765+
import _pyio
766+
767+
oldwrite = os.write
768+
seen_write = False
769+
770+
truncate_at_length = 100
771+
772+
# Emulate an os.write that only writes partial data.
773+
def write(fd, data):
774+
nonlocal seen_write
775+
seen_write = True
776+
return oldwrite(fd, data[:truncate_at_length])
777+
778+
# Need to patch _io to be _pyio, so that io.FileIO is affected by the
779+
# os.write patch.
780+
with (support.swap_attr(_bootstrap_external, '_io', _pyio),
781+
support.swap_attr(os, 'write', write)):
782+
with self.assertRaises(OSError):
783+
# Make sure we write something longer than the point where we
784+
# truncate.
785+
content = b'x' * (truncate_at_length * 2)
786+
_bootstrap_external._write_atomic(os_helper.TESTFN, content)
787+
assert seen_write
788+
789+
with self.assertRaises(OSError):
790+
os.stat(support.os_helper.TESTFN) # Check that the file did not get written.
791+
792+
761793
if __name__ == '__main__':
762794
unittest.main()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix :mod:`importlib` to not write an incomplete .pyc files when a ulimit or some
2+
other operating system mechanism is preventing the write to go through
3+
fully.

0 commit comments

Comments
 (0)