|
| 1 | +import io |
| 2 | +import _pyio as pyio |
1 | 3 | import threading |
2 | 4 | from unittest import TestCase |
3 | 5 | from test.support import threading_helper |
4 | 6 | from random import randint |
5 | | -from io import BytesIO |
6 | 7 | from sys import getsizeof |
7 | 8 |
|
8 | 9 |
|
9 | | -class TestBytesIO(TestCase): |
| 10 | +class ThreadSafetyMixin: |
10 | 11 | # Test pretty much everything that can break under free-threading. |
11 | 12 | # Non-deterministic, but at least one of these things will fail if |
12 | 13 | # BytesIO object is not free-thread safe. |
@@ -90,20 +91,28 @@ def sizeof(barrier, b, *ignore): |
90 | 91 | barrier.wait() |
91 | 92 | getsizeof(b) |
92 | 93 |
|
93 | | - self.check([write] * 10, BytesIO()) |
94 | | - self.check([writelines] * 10, BytesIO()) |
95 | | - self.check([write] * 10 + [truncate] * 10, BytesIO()) |
96 | | - self.check([truncate] + [read] * 10, BytesIO(b'0\n'*204800)) |
97 | | - self.check([truncate] + [read1] * 10, BytesIO(b'0\n'*204800)) |
98 | | - self.check([truncate] + [readline] * 10, BytesIO(b'0\n'*20480)) |
99 | | - self.check([truncate] + [readlines] * 10, BytesIO(b'0\n'*20480)) |
100 | | - self.check([truncate] + [readinto] * 10, BytesIO(b'0\n'*204800), bytearray(b'0\n'*204800)) |
101 | | - self.check([close] + [write] * 10, BytesIO()) |
102 | | - self.check([truncate] + [getvalue] * 10, BytesIO(b'0\n'*204800)) |
103 | | - self.check([truncate] + [getbuffer] * 10, BytesIO(b'0\n'*204800)) |
104 | | - self.check([truncate] + [iter] * 10, BytesIO(b'0\n'*20480)) |
105 | | - self.check([truncate] + [getstate] * 10, BytesIO(b'0\n'*204800)) |
106 | | - self.check([truncate] + [setstate] * 10, BytesIO(b'0\n'*204800), (b'123', 0, None)) |
107 | | - self.check([truncate] + [sizeof] * 10, BytesIO(b'0\n'*204800)) |
| 94 | + self.check([write] * 10, self.ioclass()) |
| 95 | + self.check([writelines] * 10, self.ioclass()) |
| 96 | + self.check([write] * 10 + [truncate] * 10, self.ioclass()) |
| 97 | + self.check([truncate] + [read] * 10, self.ioclass(b'0\n'*204800)) |
| 98 | + self.check([truncate] + [read1] * 10, self.ioclass(b'0\n'*204800)) |
| 99 | + self.check([truncate] + [readline] * 10, self.ioclass(b'0\n'*20480)) |
| 100 | + self.check([truncate] + [readlines] * 10, self.ioclass(b'0\n'*20480)) |
| 101 | + self.check([truncate] + [readinto] * 10, self.ioclass(b'0\n'*204800), bytearray(b'0\n'*204800)) |
| 102 | + self.check([close] + [write] * 10, self.ioclass()) |
| 103 | + self.check([truncate] + [getvalue] * 10, self.ioclass(b'0\n'*204800)) |
| 104 | + self.check([truncate] + [getbuffer] * 10, self.ioclass(b'0\n'*204800)) |
| 105 | + self.check([truncate] + [iter] * 10, self.ioclass(b'0\n'*20480)) |
| 106 | + self.check([truncate] + [getstate] * 10, self.ioclass(b'0\n'*204800)) |
| 107 | + # _pyio uses default __setstate__ |
| 108 | + if hasattr(self.ioclass, '__setstate__'): |
| 109 | + self.check([truncate] + [setstate] * 10, self.ioclass(b'0\n'*204800), (b'123', 0, None)) |
| 110 | + self.check([truncate] + [sizeof] * 10, self.ioclass(b'0\n'*204800)) |
108 | 111 |
|
109 | 112 | # no tests for seek or tell because they don't break anything |
| 113 | + |
| 114 | +class CBytesIOTest(ThreadSafetyMixin, TestCase): |
| 115 | + ioclass = io.BytesIO |
| 116 | + |
| 117 | +class PyBytesIOTest(ThreadSafetyMixin, TestCase): |
| 118 | + ioclass = pyio.BytesIO |
0 commit comments