diff --git a/Lib/test/test_importlib/test_util.py b/Lib/test/test_importlib/test_util.py index 8a911c1b751a4b..1639bb34fe1052 100644 --- a/Lib/test/test_importlib/test_util.py +++ b/Lib/test/test_importlib/test_util.py @@ -7,6 +7,8 @@ import importlib.util from importlib import _bootstrap_external +import contextlib +import io import os import pathlib import string @@ -788,73 +790,35 @@ def test_complete_multi_phase_init_module(self): self.run_with_own_gil(script) -class PatchAtomicWrites: - def __init__(self, truncate_at_length, never_complete=False): - self.truncate_at_length = truncate_at_length - self.never_complete = never_complete - self.seen_write = False - self._children = [] - - def __enter__(self): - import _pyio - - oldwrite = os.write - - # Emulate an os.write that only writes partial data. - def write(fd, data): - if self.seen_write and self.never_complete: - return None - self.seen_write = True - return oldwrite(fd, data[:self.truncate_at_length]) - - # Need to patch _io to be _pyio, so that io.FileIO is affected by the - # os.write patch. - self.children = [ - support.swap_attr(_bootstrap_external, '_io', _pyio), - support.swap_attr(os, 'write', write) - ] - for child in self.children: - child.__enter__() - return self - - def __exit__(self, exc_type, exc_val, exc_tb): - for child in self.children: - child.__exit__(exc_type, exc_val, exc_tb) - - class MiscTests(unittest.TestCase): - def test_atomic_write_retries_incomplete_writes(self): - truncate_at_length = 100 - length = truncate_at_length * 2 + @contextlib.contextmanager + def limit_writes(self, lengths): + """Writes are cut short to each length in turn, then fail.""" + class PartialFileIO(io.FileIO): + def write(self, data): + if not lengths: + raise OSError('disk full') + return super().write(data[:lengths.pop(0)]) + + with support.swap_attr( + _bootstrap_external._io, 'open', + lambda fd, mode: io.BufferedWriter(PartialFileIO(fd, mode))): + yield + self.assertEqual(lengths, []) - with PatchAtomicWrites(truncate_at_length=truncate_at_length) as cm: - # Make sure we write something longer than the point where we - # truncate. - content = b'x' * length - _bootstrap_external._write_atomic(os_helper.TESTFN, content) - self.assertTrue(cm.seen_write) - - self.assertEqual(os.stat(support.os_helper.TESTFN).st_size, length) - os.unlink(support.os_helper.TESTFN) + def test_atomic_write_retries_incomplete_writes(self): + with os_helper.temp_cwd(None): + with self.limit_writes([3, 3]): + _bootstrap_external._write_atomic('file', b'abcdef') + self.assertEqual(pathlib.Path('file').read_bytes(), b'abcdef') def test_atomic_write_errors_if_unable_to_complete(self): - truncate_at_length = 100 - - with ( - PatchAtomicWrites( - truncate_at_length=truncate_at_length, never_complete=True, - ) as cm, - self.assertRaises(OSError) - ): - # Make sure we write something longer than the point where we - # truncate. - content = b'x' * (truncate_at_length * 2) - _bootstrap_external._write_atomic(os_helper.TESTFN, content) - self.assertTrue(cm.seen_write) - - with self.assertRaises(OSError): - os.stat(support.os_helper.TESTFN) # Check that the file did not get written. + with os_helper.temp_cwd(None): + with self.limit_writes([3]), self.assertRaises(OSError): + _bootstrap_external._write_atomic('file', b'abcdef') + # Neither the target nor the temporary file is left behind. + self.assertEqual(os.listdir(), []) if __name__ == '__main__': diff --git a/Lib/test/test_zipfile/test_core.py b/Lib/test/test_zipfile/test_core.py index ccb8b87023eb5e..8320029cb17f26 100644 --- a/Lib/test/test_zipfile/test_core.py +++ b/Lib/test/test_zipfile/test_core.py @@ -1,4 +1,3 @@ -import _pyio import array import contextlib import errno @@ -6167,7 +6166,7 @@ def test_too_short(self): b"zzz", zipfile._Extra.strip(b"zzz", (self.ZIP64_EXTRA,))) -class StatIO(_pyio.BytesIO): +class StatIO(io.BytesIO): """Buffer which remembers the number of bytes that were read.""" def __init__(self):