Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 26 additions & 17 deletions Lib/test/test_free_threading/test_io.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import io
import _pyio as pyio
import threading
from unittest import TestCase
from test.support import threading_helper
from random import randint
from io import BytesIO
from sys import getsizeof


class TestBytesIO(TestCase):
class ThreadSafetyMixin:
# Test pretty much everything that can break under free-threading.
# Non-deterministic, but at least one of these things will fail if
# BytesIO object is not free-thread safe.
Expand Down Expand Up @@ -90,20 +91,28 @@ def sizeof(barrier, b, *ignore):
barrier.wait()
getsizeof(b)

self.check([write] * 10, BytesIO())
self.check([writelines] * 10, BytesIO())
self.check([write] * 10 + [truncate] * 10, BytesIO())
self.check([truncate] + [read] * 10, BytesIO(b'0\n'*204800))
self.check([truncate] + [read1] * 10, BytesIO(b'0\n'*204800))
self.check([truncate] + [readline] * 10, BytesIO(b'0\n'*20480))
self.check([truncate] + [readlines] * 10, BytesIO(b'0\n'*20480))
self.check([truncate] + [readinto] * 10, BytesIO(b'0\n'*204800), bytearray(b'0\n'*204800))
self.check([close] + [write] * 10, BytesIO())
self.check([truncate] + [getvalue] * 10, BytesIO(b'0\n'*204800))
self.check([truncate] + [getbuffer] * 10, BytesIO(b'0\n'*204800))
self.check([truncate] + [iter] * 10, BytesIO(b'0\n'*20480))
self.check([truncate] + [getstate] * 10, BytesIO(b'0\n'*204800))
self.check([truncate] + [setstate] * 10, BytesIO(b'0\n'*204800), (b'123', 0, None))
self.check([truncate] + [sizeof] * 10, BytesIO(b'0\n'*204800))
self.check([write] * 10, self.ioclass())
self.check([writelines] * 10, self.ioclass())
self.check([write] * 10 + [truncate] * 10, self.ioclass())
self.check([truncate] + [read] * 10, self.ioclass(b'0\n'*204800))
self.check([truncate] + [read1] * 10, self.ioclass(b'0\n'*204800))
self.check([truncate] + [readline] * 10, self.ioclass(b'0\n'*20480))
self.check([truncate] + [readlines] * 10, self.ioclass(b'0\n'*20480))
self.check([truncate] + [readinto] * 10, self.ioclass(b'0\n'*204800), bytearray(b'0\n'*204800))
self.check([close] + [write] * 10, self.ioclass())
self.check([truncate] + [getvalue] * 10, self.ioclass(b'0\n'*204800))
self.check([truncate] + [getbuffer] * 10, self.ioclass(b'0\n'*204800))
self.check([truncate] + [iter] * 10, self.ioclass(b'0\n'*20480))
self.check([truncate] + [getstate] * 10, self.ioclass(b'0\n'*204800))
# _pyio uses default __setstate__
if hasattr(self.ioclass, '__setstate__'):
self.check([truncate] + [setstate] * 10, self.ioclass(b'0\n'*204800), (b'123', 0, None))
self.check([truncate] + [sizeof] * 10, self.ioclass(b'0\n'*204800))

# no tests for seek or tell because they don't break anything

class CBytesIOTest(ThreadSafetyMixin, TestCase):
ioclass = io.BytesIO

class PyBytesIOTest(ThreadSafetyMixin, TestCase):
ioclass = pyio.BytesIO
1 change: 1 addition & 0 deletions Lib/test/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# * test_univnewlines - tests universal newline support
# * test_largefile - tests operations on a file greater than 2**32 bytes
# (only enabled with -ulargefile)
# * test_free_threading/test_io - tests thread safety of io objects

################################################################################
# ATTENTION TEST WRITERS!!!
Expand Down
Loading