Skip to content

Commit a92a4d1

Browse files
cmaloneyclaude
andauthored
gh-163124: Stop using _pyio to test importlib and zipfile (#163125)
The importlib tests of _write_atomic() used _pyio so they could patch os.write(). Replace _io.open() with a BufferedWriter over a FileIO whose writes are cut short instead. Also check the written contents and that no temporary file is left behind after a failure. The zipfile test only needs a BytesIO subclass that counts reads, which io.BytesIO supports. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 18911bc commit a92a4d1

2 files changed

Lines changed: 27 additions & 64 deletions

File tree

‎Lib/test/test_importlib/test_util.py‎

Lines changed: 26 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import importlib.util
99
from importlib import _bootstrap_external
10+
import contextlib
11+
import io
1012
import os
1113
import pathlib
1214
import string
@@ -788,73 +790,35 @@ def test_complete_multi_phase_init_module(self):
788790
self.run_with_own_gil(script)
789791

790792

791-
class PatchAtomicWrites:
792-
def __init__(self, truncate_at_length, never_complete=False):
793-
self.truncate_at_length = truncate_at_length
794-
self.never_complete = never_complete
795-
self.seen_write = False
796-
self._children = []
797-
798-
def __enter__(self):
799-
import _pyio
800-
801-
oldwrite = os.write
802-
803-
# Emulate an os.write that only writes partial data.
804-
def write(fd, data):
805-
if self.seen_write and self.never_complete:
806-
return None
807-
self.seen_write = True
808-
return oldwrite(fd, data[:self.truncate_at_length])
809-
810-
# Need to patch _io to be _pyio, so that io.FileIO is affected by the
811-
# os.write patch.
812-
self.children = [
813-
support.swap_attr(_bootstrap_external, '_io', _pyio),
814-
support.swap_attr(os, 'write', write)
815-
]
816-
for child in self.children:
817-
child.__enter__()
818-
return self
819-
820-
def __exit__(self, exc_type, exc_val, exc_tb):
821-
for child in self.children:
822-
child.__exit__(exc_type, exc_val, exc_tb)
823-
824-
825793
class MiscTests(unittest.TestCase):
826794

827-
def test_atomic_write_retries_incomplete_writes(self):
828-
truncate_at_length = 100
829-
length = truncate_at_length * 2
795+
@contextlib.contextmanager
796+
def limit_writes(self, lengths):
797+
"""Writes are cut short to each length in turn, then fail."""
798+
class PartialFileIO(io.FileIO):
799+
def write(self, data):
800+
if not lengths:
801+
raise OSError('disk full')
802+
return super().write(data[:lengths.pop(0)])
803+
804+
with support.swap_attr(
805+
_bootstrap_external._io, 'open',
806+
lambda fd, mode: io.BufferedWriter(PartialFileIO(fd, mode))):
807+
yield
808+
self.assertEqual(lengths, [])
830809

831-
with PatchAtomicWrites(truncate_at_length=truncate_at_length) as cm:
832-
# Make sure we write something longer than the point where we
833-
# truncate.
834-
content = b'x' * length
835-
_bootstrap_external._write_atomic(os_helper.TESTFN, content)
836-
self.assertTrue(cm.seen_write)
837-
838-
self.assertEqual(os.stat(support.os_helper.TESTFN).st_size, length)
839-
os.unlink(support.os_helper.TESTFN)
810+
def test_atomic_write_retries_incomplete_writes(self):
811+
with os_helper.temp_cwd(None):
812+
with self.limit_writes([3, 3]):
813+
_bootstrap_external._write_atomic('file', b'abcdef')
814+
self.assertEqual(pathlib.Path('file').read_bytes(), b'abcdef')
840815

841816
def test_atomic_write_errors_if_unable_to_complete(self):
842-
truncate_at_length = 100
843-
844-
with (
845-
PatchAtomicWrites(
846-
truncate_at_length=truncate_at_length, never_complete=True,
847-
) as cm,
848-
self.assertRaises(OSError)
849-
):
850-
# Make sure we write something longer than the point where we
851-
# truncate.
852-
content = b'x' * (truncate_at_length * 2)
853-
_bootstrap_external._write_atomic(os_helper.TESTFN, content)
854-
self.assertTrue(cm.seen_write)
855-
856-
with self.assertRaises(OSError):
857-
os.stat(support.os_helper.TESTFN) # Check that the file did not get written.
817+
with os_helper.temp_cwd(None):
818+
with self.limit_writes([3]), self.assertRaises(OSError):
819+
_bootstrap_external._write_atomic('file', b'abcdef')
820+
# Neither the target nor the temporary file is left behind.
821+
self.assertEqual(os.listdir(), [])
858822

859823

860824
if __name__ == '__main__':

‎Lib/test/test_zipfile/test_core.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import _pyio
21
import array
32
import contextlib
43
import errno
@@ -6167,7 +6166,7 @@ def test_too_short(self):
61676166
b"zzz", zipfile._Extra.strip(b"zzz", (self.ZIP64_EXTRA,)))
61686167

61696168

6170-
class StatIO(_pyio.BytesIO):
6169+
class StatIO(io.BytesIO):
61716170
"""Buffer which remembers the number of bytes that were read."""
61726171

61736172
def __init__(self):

0 commit comments

Comments
 (0)