@@ -498,59 +498,65 @@ class UnseekableWriter(self.MockUnseekableIO):
498498 (text_reader , "r" ), (text_writer , "w" ),
499499 (self .BytesIO , "rws" ), (self .StringIO , "rws" ),
500500 )
501- for [test , abilities ] in tests :
502- if test == pipe_writer and not threading_helper .can_start_thread :
501+
502+ def do_test (obj ):
503+ readable = "r" in abilities
504+ self .assertEqual (obj .readable (), readable )
505+ writable = "w" in abilities
506+ self .assertEqual (obj .writable (), writable )
507+
508+ if isinstance (obj , self .TextIOBase ):
509+ data = "3"
510+ elif isinstance (obj , (self .BufferedIOBase , self .RawIOBase )):
511+ data = b"3"
512+ else :
513+ self .fail ("Unknown base class" )
514+
515+ if "f" in abilities :
516+ obj .fileno ()
517+ else :
518+ self .assertRaises (OSError , obj .fileno )
519+
520+ if readable :
521+ obj .read (1 )
522+ obj .read ()
523+ else :
524+ self .assertRaises (OSError , obj .read , 1 )
525+ self .assertRaises (OSError , obj .read )
526+
527+ if writable :
528+ obj .write (data )
529+ else :
530+ self .assertRaises (OSError , obj .write , data )
531+
532+ if sys .platform .startswith ("win" ) and test in (
533+ pipe_reader , pipe_writer ):
534+ # Pipes seem to appear as seekable on Windows
503535 continue
504- with self .subTest (test ), test () as obj :
505- readable = "r" in abilities
506- self .assertEqual (obj .readable (), readable )
507- writable = "w" in abilities
508- self .assertEqual (obj .writable (), writable )
509-
510- if isinstance (obj , self .TextIOBase ):
511- data = "3"
512- elif isinstance (obj , (self .BufferedIOBase , self .RawIOBase )):
513- data = b"3"
514- else :
515- self .fail ("Unknown base class" )
536+ seekable = "s" in abilities
537+ self .assertEqual (obj .seekable (), seekable )
516538
517- if "f" in abilities :
518- obj .fileno ()
519- else :
520- self .assertRaises (OSError , obj .fileno )
539+ if seekable :
540+ obj .tell ()
541+ obj .seek (0 )
542+ else :
543+ self .assertRaises (OSError , obj .tell )
544+ self .assertRaises (OSError , obj .seek , 0 )
521545
522- if readable :
523- obj .read ( 1 )
524- obj .read ( )
525- else :
526- self .assertRaises (OSError , obj .read , 1 )
527- self .assertRaises (OSError , obj .read )
546+ if writable and seekable :
547+ obj .truncate ( )
548+ obj .truncate ( 0 )
549+ else :
550+ self .assertRaises (OSError , obj .truncate )
551+ self .assertRaises (OSError , obj .truncate , 0 )
528552
529- if writable :
530- obj .write (data )
531- else :
532- self .assertRaises (OSError , obj .write , data )
533-
534- if sys .platform .startswith ("win" ) and test in (
535- pipe_reader , pipe_writer ):
536- # Pipes seem to appear as seekable on Windows
537- continue
538- seekable = "s" in abilities
539- self .assertEqual (obj .seekable (), seekable )
540-
541- if seekable :
542- obj .tell ()
543- obj .seek (0 )
544- else :
545- self .assertRaises (OSError , obj .tell )
546- self .assertRaises (OSError , obj .seek , 0 )
553+ for [test , abilities ] in tests :
554+ with self .subTest (test ):
555+ if test == pipe_writer and not threading_helper .can_start_thread :
556+ skipTest ()
557+ with test () as obj :
558+ do_test (obj )
547559
548- if writable and seekable :
549- obj .truncate ()
550- obj .truncate (0 )
551- else :
552- self .assertRaises (OSError , obj .truncate )
553- self .assertRaises (OSError , obj .truncate , 0 )
554560
555561 def test_open_handles_NUL_chars (self ):
556562 fn_with_NUL = 'foo\0 bar'
0 commit comments