Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 2 additions & 8 deletions Lib/test/test_io/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Tests for the io module and its implementations (_io and _pyio)
"""Tests for the io module

Tests are split across multiple files to increase
parallelism and focus on specific implementation pieces.
Expand All @@ -9,14 +9,8 @@
* test_builtin, test_print - open(), input() and print()
* test_cmd_line, test_subprocess - stdio buffering and newlines at startup
and through pipes
* test_utf8_mode - UTF-8 mode encodings, including with _pyio
* test_utf8_mode - UTF-8 mode encodings
* test_os, test_eintr - fd-level I/O and EINTR retries

.. attention::
When writing tests for io, it's important to test both the C and Python
implementations. This is usually done by writing a base test that refers to
the type it is testing as an attribute. Then it provides custom subclasses to
test both implementations. This directory contains lots of examples.
"""

import os
Expand Down
24 changes: 1 addition & 23 deletions Lib/test/test_io/test_bufferedio.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
from itertools import cycle, count
from test import support
from test.support import check_sanitizer, os_helper, threading_helper
from .utils import byteslike, CTestCase, PyTestCase
from .utils import byteslike, CTestCase


import io # C implementation.
import _pyio as pyio # Python implementation.


class CommonBufferedTests:
Expand Down Expand Up @@ -595,13 +594,10 @@ def test_initialization(self):
def test_misbehaved_io_read(self):
rawio = self.MisbehavedRawIO((b"abc", b"d", b"efg"))
bufio = self.tp(rawio)
# _pyio.BufferedReader seems to implement reading different, so that
# checking this is not so easy.
self.assertRaises(OSError, bufio.read, 10)

def test_garbage_collection(self):
# C BufferedReader objects are collected.
# The Python version has __del__, so it ends into gc.garbage instead
self.addCleanup(os_helper.unlink, os_helper.TESTFN)
# Note that using warnings_helper.check_warnings() will keep the
# file alive due to the `source` argument to warn(). So, use
Expand Down Expand Up @@ -657,10 +653,6 @@ def test_read1_error_does_not_cause_reentrant_failure(self):
self.assertEqual(bufio.read1(1), b"h")


class PyBufferedReaderTest(BufferedReaderTest, PyTestCase):
tp = pyio.BufferedReader


class BufferedWriterTest(CommonBufferedTests):
write_mode = "wb"

Expand Down Expand Up @@ -972,7 +964,6 @@ def test_initialization(self):
def test_garbage_collection(self):
# C BufferedWriter objects are collected, and collecting them flushes
# all data to disk.
# The Python version has __del__, so it ends into gc.garbage instead
self.addCleanup(os_helper.unlink, os_helper.TESTFN)
# Note that using warnings_helper.check_warnings() will keep the
# file alive due to the `source` argument to warn(). So, use
Expand Down Expand Up @@ -1017,9 +1008,6 @@ def closed(self):
self.assertRaisesRegex(ValueError, "test", bufio.close)


class PyBufferedWriterTest(BufferedWriterTest, PyTestCase):
tp = pyio.BufferedWriter

class BufferedRWPairTest:

def test_constructor(self):
Expand Down Expand Up @@ -1273,9 +1261,6 @@ def test_weakref_clearing(self):
class CBufferedRWPairTest(BufferedRWPairTest, CTestCase):
tp = io.BufferedRWPair

class PyBufferedRWPairTest(BufferedRWPairTest, PyTestCase):
tp = pyio.BufferedRWPair


class BufferedRandomTest(BufferedReaderTest, BufferedWriterTest):
read_mode = "rb+"
Expand Down Expand Up @@ -1570,10 +1555,6 @@ def flush():
self.assertFalse(rawio.closed)


class PyBufferedRandomTest(BufferedRandomTest, PyTestCase):
tp = pyio.BufferedRandom


# Simple test to ensure that optimizations in the IO library deliver the
# expected results. For best testing, run this under a debug-build Python too
# (to exercise asserts in the C code).
Expand Down Expand Up @@ -1635,9 +1616,6 @@ def test_nullpat(self):
class CBufferSizeTest(BufferSizeTest, unittest.TestCase):
open = io.open

class PyBufferSizeTest(BufferSizeTest, unittest.TestCase):
open = staticmethod(pyio.open)


if __name__ == "__main__":
unittest.main()
7 changes: 0 additions & 7 deletions Lib/test/test_io/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from weakref import proxy

import io
import _pyio as pyio

from test.support import gc_collect
from test.support.os_helper import TESTFN
Expand Down Expand Up @@ -135,9 +134,6 @@ def testReadWhenWriting(self):
class CAutoFileTests(AutoFileTests, unittest.TestCase):
open = io.open

class PyAutoFileTests(AutoFileTests, unittest.TestCase):
open = staticmethod(pyio.open)


class OtherFileTests:

Expand Down Expand Up @@ -354,9 +350,6 @@ def testIteration(self):
class COtherFileTests(OtherFileTests, unittest.TestCase):
open = io.open

class PyOtherFileTests(OtherFileTests, unittest.TestCase):
open = staticmethod(pyio.open)


if __name__ == '__main__':
unittest.main()
10 changes: 0 additions & 10 deletions Lib/test/test_io/test_file_eintr.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

# Test import all of the things we're about to try testing up front.
import _io # noqa: F401
import _pyio # noqa: F401

@unittest.skipUnless(os.name == 'posix', 'tests requires a posix system.')
class TestFileIOSignalInterrupt:
Expand Down Expand Up @@ -186,9 +185,6 @@ def test_readall(self):
class CTestFileIOSignalInterrupt(TestFileIOSignalInterrupt, unittest.TestCase):
modname = '_io'

class PyTestFileIOSignalInterrupt(TestFileIOSignalInterrupt, unittest.TestCase):
modname = '_pyio'


class TestBufferedIOSignalInterrupt(TestFileIOSignalInterrupt):
def _generate_infile_setup_code(self):
Expand All @@ -208,9 +204,6 @@ def test_readall(self):
class CTestBufferedIOSignalInterrupt(TestBufferedIOSignalInterrupt, unittest.TestCase):
modname = '_io'

class PyTestBufferedIOSignalInterrupt(TestBufferedIOSignalInterrupt, unittest.TestCase):
modname = '_pyio'


class TestTextIOSignalInterrupt(TestFileIOSignalInterrupt):
def _generate_infile_setup_code(self):
Expand Down Expand Up @@ -247,9 +240,6 @@ def test_readall(self):
class CTestTextIOSignalInterrupt(TestTextIOSignalInterrupt, unittest.TestCase):
modname = '_io'

class PyTestTextIOSignalInterrupt(TestTextIOSignalInterrupt, unittest.TestCase):
modname = '_pyio'


if __name__ == '__main__':
unittest.main()
24 changes: 0 additions & 24 deletions Lib/test/test_io/test_fileio.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from collections import UserList

import _io # C implementation of io
import _pyio # Python implementation of io


_strace_flags=["--trace=%file,%desc"]
Expand Down Expand Up @@ -524,11 +523,6 @@ def testFinalizing(self):
self.f._finalizing = False


class PyAutoFileTests(AutoFileTests, unittest.TestCase):
FileIO = _pyio.FileIO
modulename = '_pyio'


class OtherFileTests:

def testAbles(self):
Expand Down Expand Up @@ -779,24 +773,6 @@ def test_open_code(self):
self.assertEqual(expected, actual)


class PyOtherFileTests(OtherFileTests, unittest.TestCase):
FileIO = _pyio.FileIO
modulename = '_pyio'
open = _pyio.open

def test_open_code(self):
# Check that the default behaviour of open_code matches
# open("rb")
with self.FileIO(__file__, "rb") as f:
expected = f.read()
with check_warnings(quiet=True) as w:
# Always test _open_code_with_warning
with _pyio._open_code_with_warning(__file__) as f:
actual = f.read()
self.assertEqual(expected, actual)
self.assertNotEqual(w.warnings, [])


def tearDownModule():
# Historically, these tests have been sloppy about removing TESTFN.
# So get rid of it no matter what.
Expand Down
4 changes: 0 additions & 4 deletions Lib/test/test_io/test_free_threading.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import codecs
import io
import _pyio as pyio
import threading
from unittest import SkipTest, TestCase
from test.support import Py_GIL_DISABLED, threading_helper
Expand Down Expand Up @@ -182,9 +181,6 @@ def reader():
with threading_helper.start_threads(threads):
pass

class PyBytesIOTest(ThreadSafetyMixin, TestCase):
ioclass = pyio.BytesIO


class IncrementalNewlineDecoderTest(TestCase):
def make_decoder(self):
Expand Down
27 changes: 1 addition & 26 deletions Lib/test/test_io/test_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@
import_helper, is_apple, os_helper, threading_helper, warnings_helper,
)
from test.support.os_helper import FakePath
from .utils import byteslike, CTestCase, PyTestCase
from .utils import byteslike, CTestCase

import io # C implementation of io
import _pyio as pyio # Python implementation of io


class IOTest:
Expand Down Expand Up @@ -1007,24 +1006,6 @@ def test_stringio_setstate(self):
obj.__setstate__(('', '', 0, {}))
self.assertEqual(obj.getvalue(), '')

class PyIOTest(IOTest, PyTestCase):
pass


@support.cpython_only
class APIMismatchTest(unittest.TestCase):

def test_RawIOBase_io_in_pyio_match(self):
"""Test that pyio RawIOBase class has all c RawIOBase methods"""
mismatch = support.detect_api_mismatch(pyio.RawIOBase, io.RawIOBase,
ignore=('__weakref__', '__static_attributes__'))
self.assertEqual(mismatch, set(), msg='Python RawIOBase does not have all C RawIOBase methods')

def test_RawIOBase_pyio_in_io_match(self):
"""Test that c RawIOBase class has all pyio RawIOBase methods"""
mismatch = support.detect_api_mismatch(io.RawIOBase, pyio.RawIOBase)
self.assertEqual(mismatch, set(), msg='C RawIOBase does not have all Python RawIOBase methods')


# XXX Tests for open()

Expand Down Expand Up @@ -1489,12 +1470,6 @@ def test_daemon_threads_shutdown_stderr_deadlock(self):
self.check_daemon_threads_shutdown_deadlock('stderr')


class PyMiscIOTest(MiscIOTest, PyTestCase):
name_of_module = "_pyio", "io"
extra_exported = "BlockingIOError", "open_code",
not_exported = "valid_seek_flags",


class ProtocolsTest(unittest.TestCase):
class MyReader:
def read(self, sz=-1):
Expand Down
5 changes: 0 additions & 5 deletions Lib/test/test_io/test_largefile.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from test.support import socket_helper
from test.support.os_helper import TESTFN, unlink
import io # C implementation of io
import _pyio as pyio # Python implementation of io

# size of file to create (>2 GiB; 2 GiB == 2,147,483,648 bytes)
size = 2_500_000_000
Expand Down Expand Up @@ -297,10 +296,6 @@ class CLargeFileTest(TestFileMethods, unittest.TestCase):
open = staticmethod(io.open)


class PyLargeFileTest(TestFileMethods, unittest.TestCase):
open = staticmethod(pyio.open)


def tearDownModule():
unlink(TESTFN)
unlink(TESTFN2)
Expand Down
39 changes: 9 additions & 30 deletions Lib/test/test_io/test_memoryio.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import gc
import io
import _pyio as pyio
import pickle
import struct
import sys
Expand Down Expand Up @@ -436,15 +435,10 @@ def __init__(me, initvalue, foo):
del __main__.PickleTestMemIO


class PyBytesIOTest(MemoryTestMixin, MemorySeekTestMixin, unittest.TestCase):
# Test _pyio.BytesIO; class also inherited for testing C implementation

UnsupportedOperation = pyio.UnsupportedOperation

class BytesIOTest(MemoryTestMixin, MemorySeekTestMixin):
@staticmethod
def buftype(s):
return s.encode("ascii")
ioclass = pyio.BytesIO
EOF = b""

def test_getbuffer(self):
Expand Down Expand Up @@ -928,11 +922,8 @@ def test_newline_argument(self):
self.ioclass(newline=newline)


class PyStringIOTest(MemoryTestMixin, MemorySeekTestMixin,
TextIOTestMixin, unittest.TestCase):
class StringIOTest(MemoryTestMixin, MemorySeekTestMixin, TextIOTestMixin):
buftype = str
ioclass = pyio.StringIO
UnsupportedOperation = pyio.UnsupportedOperation
EOF = ""

def test_lone_surrogates(self):
Expand All @@ -945,21 +936,7 @@ def test_lone_surrogates(self):
self.assertEqual(memio.getvalue(), '\ud800')


class PyStringIOPickleTest(TextIOTestMixin, unittest.TestCase):
"""Test if pickle restores properly the internal state of StringIO.
"""
buftype = str
UnsupportedOperation = pyio.UnsupportedOperation
EOF = ""

class ioclass(pyio.StringIO):
def __new__(cls, *args, **kwargs):
return pickle.loads(pickle.dumps(pyio.StringIO(*args, **kwargs)))
def __init__(self, *args, **kwargs):
pass


class CBytesIOTest(PyBytesIOTest):
class CBytesIOTest(BytesIOTest, unittest.TestCase):
ioclass = io.BytesIO
UnsupportedOperation = io.UnsupportedOperation

Expand Down Expand Up @@ -1152,7 +1129,7 @@ def test_write_with_export(self):
self.assertEqual(memio.getvalue(), b"abXd")


class CStringIOTest(PyStringIOTest):
class CStringIOTest(StringIOTest, unittest.TestCase):
ioclass = io.StringIO
UnsupportedOperation = io.UnsupportedOperation

Expand All @@ -1174,8 +1151,6 @@ def __index__(self):
self.assertEqual(stream.truncate(2**31), 2**31)
self.assertEqual(stream.getvalue(), "abc")

# XXX: For the Python version of io.StringIO, this is highly
# dependent on the encoding used for the underlying buffer.
def test_widechar(self):
buf = self.buftype("\U0002030a\U00020347")
memio = self.ioclass(buf)
Expand Down Expand Up @@ -1274,7 +1249,11 @@ def test_memory_error_consistent_state(self):
self.assertEqual(memio.getvalue(), text + "!")


class CStringIOPickleTest(PyStringIOPickleTest):
class CStringIOPickleTest(TextIOTestMixin, unittest.TestCase):
"""Test if pickle restores properly the internal state of StringIO.
"""
buftype = str
EOF = ""
UnsupportedOperation = io.UnsupportedOperation

class ioclass(io.StringIO):
Expand Down
Loading
Loading