@@ -317,6 +317,45 @@ class PyMockNonBlockWriterIO(MockNonBlockWriterIO, pyio.RawIOBase):
317
317
BlockingIOError = pyio .BlockingIOError
318
318
319
319
320
+ # Build classes which point to all the right mocks per io implementation
321
+ class CTestCase (unittest .TestCase ):
322
+ io = io
323
+ is_C = True
324
+
325
+ MockRawIO = CMockRawIO
326
+ MisbehavedRawIO = CMisbehavedRawIO
327
+ MockFileIO = CMockFileIO
328
+ CloseFailureIO = CCloseFailureIO
329
+ MockNonBlockWriterIO = CMockNonBlockWriterIO
330
+ MockUnseekableIO = CMockUnseekableIO
331
+ MockRawIOWithoutRead = CMockRawIOWithoutRead
332
+ SlowFlushRawIO = CSlowFlushRawIO
333
+ MockCharPseudoDevFileIO = CMockCharPseudoDevFileIO
334
+
335
+ # Use the class as a proxy to the io module members.
336
+ def __getattr__ (self , name ):
337
+ return getattr (io , name )
338
+
339
+
340
+ class PyTestCase (unittest .TestCase ):
341
+ io = pyio
342
+ is_C = False
343
+
344
+ MockRawIO = PyMockRawIO
345
+ MisbehavedRawIO = PyMisbehavedRawIO
346
+ MockFileIO = PyMockFileIO
347
+ CloseFailureIO = PyCloseFailureIO
348
+ MockNonBlockWriterIO = PyMockNonBlockWriterIO
349
+ MockUnseekableIO = PyMockUnseekableIO
350
+ MockRawIOWithoutRead = PyMockRawIOWithoutRead
351
+ SlowFlushRawIO = PySlowFlushRawIO
352
+ MockCharPseudoDevFileIO = PyMockCharPseudoDevFileIO
353
+
354
+ # Use the class as a proxy to the _pyio module members.
355
+ def __getattr__ (self , name ):
356
+ return getattr (pyio , name )
357
+
358
+
320
359
class IOTest (unittest .TestCase ):
321
360
322
361
def setUp (self ):
@@ -1083,7 +1122,7 @@ def reader(file, barrier):
1083
1122
write_count * thread_count )
1084
1123
1085
1124
1086
- class CIOTest (IOTest ):
1125
+ class CIOTest (IOTest , CTestCase ):
1087
1126
1088
1127
def test_IOBase_finalize (self ):
1089
1128
# Issue #12149: segmentation fault on _PyIOBase_finalize when both a
@@ -1207,7 +1246,7 @@ def test_stringio_setstate(self):
1207
1246
obj .__setstate__ (('' , '' , 0 , {}))
1208
1247
self .assertEqual (obj .getvalue (), '' )
1209
1248
1210
- class PyIOTest (IOTest ):
1249
+ class PyIOTest (IOTest , PyTestCase ):
1211
1250
pass
1212
1251
1213
1252
@@ -1438,7 +1477,7 @@ def test_buffer_freeing(self) :
1438
1477
bufio .close ()
1439
1478
self .assertEqual (sys .getsizeof (bufio ), size )
1440
1479
1441
- class BufferedReaderTest (unittest . TestCase , CommonBufferedTests ):
1480
+ class BufferedReaderTest (CommonBufferedTests ):
1442
1481
read_mode = "rb"
1443
1482
1444
1483
def test_constructor (self ):
@@ -1773,7 +1812,7 @@ def test_seek_character_device_file(self):
1773
1812
self .assertEqual (buf .seek (0 , io .SEEK_CUR ), 0 )
1774
1813
1775
1814
1776
- class CBufferedReaderTest (BufferedReaderTest , SizeofTest ):
1815
+ class CBufferedReaderTest (BufferedReaderTest , SizeofTest , CTestCase ):
1777
1816
tp = io .BufferedReader
1778
1817
1779
1818
def test_initialization (self ):
@@ -1832,11 +1871,11 @@ def test_bad_readinto_type(self):
1832
1871
self .assertIsInstance (cm .exception .__cause__ , TypeError )
1833
1872
1834
1873
1835
- class PyBufferedReaderTest (BufferedReaderTest ):
1874
+ class PyBufferedReaderTest (BufferedReaderTest , PyTestCase ):
1836
1875
tp = pyio .BufferedReader
1837
1876
1838
1877
1839
- class BufferedWriterTest (unittest . TestCase , CommonBufferedTests ):
1878
+ class BufferedWriterTest (CommonBufferedTests ):
1840
1879
write_mode = "wb"
1841
1880
1842
1881
def test_constructor (self ):
@@ -2131,8 +2170,7 @@ def test_slow_close_from_thread(self):
2131
2170
t .join ()
2132
2171
2133
2172
2134
-
2135
- class CBufferedWriterTest (BufferedWriterTest , SizeofTest ):
2173
+ class CBufferedWriterTest (BufferedWriterTest , SizeofTest , CTestCase ):
2136
2174
tp = io .BufferedWriter
2137
2175
2138
2176
def test_initialization (self ):
@@ -2172,10 +2210,10 @@ def test_args_error(self):
2172
2210
self .tp (self .BytesIO (), 1024 , 1024 , 1024 )
2173
2211
2174
2212
2175
- class PyBufferedWriterTest (BufferedWriterTest ):
2213
+ class PyBufferedWriterTest (BufferedWriterTest , PyTestCase ):
2176
2214
tp = pyio .BufferedWriter
2177
2215
2178
- class BufferedRWPairTest ( unittest . TestCase ) :
2216
+ class BufferedRWPairTest :
2179
2217
2180
2218
def test_constructor (self ):
2181
2219
pair = self .tp (self .MockRawIO (), self .MockRawIO ())
@@ -2204,14 +2242,14 @@ def test_constructor_max_buffer_size_removal(self):
2204
2242
self .tp (self .MockRawIO (), self .MockRawIO (), 8 , 12 )
2205
2243
2206
2244
def test_constructor_with_not_readable (self ):
2207
- class NotReadable (MockRawIO ):
2245
+ class NotReadable (self . MockRawIO ):
2208
2246
def readable (self ):
2209
2247
return False
2210
2248
2211
2249
self .assertRaises (OSError , self .tp , NotReadable (), self .MockRawIO ())
2212
2250
2213
2251
def test_constructor_with_not_writeable (self ):
2214
- class NotWriteable (MockRawIO ):
2252
+ class NotWriteable (self . MockRawIO ):
2215
2253
def writable (self ):
2216
2254
return False
2217
2255
@@ -2357,9 +2395,9 @@ def writer_close():
2357
2395
writer .close = lambda : None
2358
2396
2359
2397
def test_isatty (self ):
2360
- class SelectableIsAtty (MockRawIO ):
2398
+ class SelectableIsAtty (self . MockRawIO ):
2361
2399
def __init__ (self , isatty ):
2362
- MockRawIO .__init__ (self )
2400
+ super () .__init__ ()
2363
2401
self ._isatty = isatty
2364
2402
2365
2403
def isatty (self ):
@@ -2383,10 +2421,10 @@ def test_weakref_clearing(self):
2383
2421
brw = None
2384
2422
ref = None # Shouldn't segfault.
2385
2423
2386
- class CBufferedRWPairTest (BufferedRWPairTest ):
2424
+ class CBufferedRWPairTest (BufferedRWPairTest , CTestCase ):
2387
2425
tp = io .BufferedRWPair
2388
2426
2389
- class PyBufferedRWPairTest (BufferedRWPairTest ):
2427
+ class PyBufferedRWPairTest (BufferedRWPairTest , PyTestCase ):
2390
2428
tp = pyio .BufferedRWPair
2391
2429
2392
2430
@@ -2645,7 +2683,7 @@ def test_interleaved_readline_write(self):
2645
2683
test_truncate_on_read_only = None
2646
2684
2647
2685
2648
- class CBufferedRandomTest (BufferedRandomTest , SizeofTest ):
2686
+ class CBufferedRandomTest (BufferedRandomTest , SizeofTest , CTestCase ):
2649
2687
tp = io .BufferedRandom
2650
2688
2651
2689
def test_garbage_collection (self ):
@@ -2658,7 +2696,7 @@ def test_args_error(self):
2658
2696
self .tp (self .BytesIO (), 1024 , 1024 , 1024 )
2659
2697
2660
2698
2661
- class PyBufferedRandomTest (BufferedRandomTest ):
2699
+ class PyBufferedRandomTest (BufferedRandomTest , PyTestCase ):
2662
2700
tp = pyio .BufferedRandom
2663
2701
2664
2702
@@ -4058,8 +4096,7 @@ def _to_memoryview(buf):
4058
4096
return memoryview (arr )
4059
4097
4060
4098
4061
- class CTextIOWrapperTest (TextIOWrapperTest ):
4062
- io = io
4099
+ class CTextIOWrapperTest (TextIOWrapperTest , CTestCase ):
4063
4100
shutdown_error = "LookupError: unknown encoding: ascii"
4064
4101
4065
4102
def test_initialization (self ):
@@ -4159,8 +4196,7 @@ def write(self, data):
4159
4196
buf ._write_stack )
4160
4197
4161
4198
4162
- class PyTextIOWrapperTest (TextIOWrapperTest ):
4163
- io = pyio
4199
+ class PyTextIOWrapperTest (TextIOWrapperTest , PyTestCase ):
4164
4200
shutdown_error = "LookupError: unknown encoding: ascii"
4165
4201
4166
4202
@@ -4282,6 +4318,8 @@ def test_translate(self):
4282
4318
self .assertEqual (decoder .decode (b"\r \r \n " ), "\r \r \n " )
4283
4319
4284
4320
class CIncrementalNewlineDecoderTest (IncrementalNewlineDecoderTest ):
4321
+ IncrementalNewlineDecoder = io .IncrementalNewlineDecoder
4322
+
4285
4323
@support .cpython_only
4286
4324
def test_uninitialized (self ):
4287
4325
uninitialized = self .IncrementalNewlineDecoder .__new__ (
@@ -4293,7 +4331,7 @@ def test_uninitialized(self):
4293
4331
4294
4332
4295
4333
class PyIncrementalNewlineDecoderTest (IncrementalNewlineDecoderTest ):
4296
- pass
4334
+ IncrementalNewlineDecoder = pyio . IncrementalNewlineDecoder
4297
4335
4298
4336
4299
4337
# XXX Tests for open()
@@ -4662,8 +4700,7 @@ def test_text_encoding(self):
4662
4700
self .assertEqual (b"utf-8" , proc .out .strip ())
4663
4701
4664
4702
4665
- class CMiscIOTest (MiscIOTest ):
4666
- io = io
4703
+ class CMiscIOTest (MiscIOTest , CTestCase ):
4667
4704
name_of_module = "io" , "_io"
4668
4705
extra_exported = "BlockingIOError" ,
4669
4706
@@ -4728,8 +4765,7 @@ def test_daemon_threads_shutdown_stderr_deadlock(self):
4728
4765
self .check_daemon_threads_shutdown_deadlock ('stderr' )
4729
4766
4730
4767
4731
- class PyMiscIOTest (MiscIOTest ):
4732
- io = pyio
4768
+ class PyMiscIOTest (MiscIOTest , PyTestCase ):
4733
4769
name_of_module = "_pyio" , "io"
4734
4770
extra_exported = "BlockingIOError" , "open_code" ,
4735
4771
not_exported = "valid_seek_flags" ,
@@ -4990,11 +5026,11 @@ def test_interrupted_write_retry_text(self):
4990
5026
self .check_interrupted_write_retry ("x" , mode = "w" , encoding = "latin1" )
4991
5027
4992
5028
4993
- class CSignalsTest (SignalsTest ):
4994
- io = io
5029
+ class CSignalsTest (SignalsTest , CTestCase ):
5030
+ pass
4995
5031
4996
- class PySignalsTest (SignalsTest ):
4997
- io = pyio
5032
+ class PySignalsTest (SignalsTest , PyTestCase ):
5033
+ pass
4998
5034
4999
5035
# Handling reentrancy issues would slow down _pyio even more, so the
5000
5036
# tests are disabled.
@@ -5034,27 +5070,6 @@ def load_tests(loader, tests, pattern):
5034
5070
ProtocolsTest ,
5035
5071
)
5036
5072
5037
- # Put the namespaces of the IO module we are testing and some useful mock
5038
- # classes in the __dict__ of each test.
5039
- mocks = (MockRawIO , MisbehavedRawIO , MockFileIO , CloseFailureIO ,
5040
- MockNonBlockWriterIO , MockUnseekableIO , MockRawIOWithoutRead ,
5041
- SlowFlushRawIO , MockCharPseudoDevFileIO )
5042
- all_members = io .__all__
5043
- c_io_ns = {name : getattr (io , name ) for name in all_members }
5044
- py_io_ns = {name : getattr (pyio , name ) for name in all_members }
5045
- globs = globals ()
5046
- c_io_ns .update ((x .__name__ , globs ["C" + x .__name__ ]) for x in mocks )
5047
- py_io_ns .update ((x .__name__ , globs ["Py" + x .__name__ ]) for x in mocks )
5048
- for test in tests :
5049
- if test .__name__ .startswith ("C" ):
5050
- for name , obj in c_io_ns .items ():
5051
- setattr (test , name , obj )
5052
- test .is_C = True
5053
- elif test .__name__ .startswith ("Py" ):
5054
- for name , obj in py_io_ns .items ():
5055
- setattr (test , name , obj )
5056
- test .is_C = False
5057
-
5058
5073
suite = loader .suiteClass ()
5059
5074
for test in tests :
5060
5075
suite .addTest (loader .loadTestsFromTestCase (test ))
0 commit comments