@@ -666,45 +666,66 @@ def test_stdin_devnull(self):
666
666
p .wait ()
667
667
self .assertEqual (p .stdin , None )
668
668
669
+ @unittest .skipUnless (fcntl and hasattr (fcntl , 'F_GETPIPE_SZ' ),
670
+ 'fcntl.F_GETPIPE_SZ required for test.' )
669
671
def test_pipesizes (self ):
670
- # stdin redirection
671
- pipesize = 16 * 1024
672
- p = subprocess .Popen ([sys .executable , "-c" ,
673
- 'import sys; sys.stdin.read(); sys.stdout.write("out"); sys.stderr.write("error!")' ],
674
- stdin = subprocess .PIPE ,
675
- stdout = subprocess .PIPE ,
676
- stderr = subprocess .PIPE ,
677
- pipesize = pipesize )
678
- # We only assert pipe size has changed on platforms that support it.
679
- if sys .platform != "win32" and hasattr (fcntl , "F_GETPIPE_SZ" ):
672
+ test_pipe_r , test_pipe_w = os .pipe ()
673
+ try :
674
+ # Get the default pipesize with F_GETPIPE_SZ
675
+ pipesize_default = fcntl .fcntl (test_pipe_w , fcntl .F_GETPIPE_SZ )
676
+ finally :
677
+ os .close (test_pipe_r )
678
+ os .close (test_pipe_w )
679
+ pipesize = pipesize_default // 2
680
+ if pipesize < 512 : # the POSIX minimum
681
+ raise unittest .SkitTest (
682
+ 'default pipesize too small to perform test.' )
683
+ p = subprocess .Popen (
684
+ [sys .executable , "-c" ,
685
+ 'import sys; sys.stdin.read(); sys.stdout.write("out"); '
686
+ 'sys.stderr.write("error!")' ],
687
+ stdin = subprocess .PIPE , stdout = subprocess .PIPE ,
688
+ stderr = subprocess .PIPE , pipesize = pipesize )
689
+ try :
680
690
for fifo in [p .stdin , p .stdout , p .stderr ]:
681
- self .assertEqual (fcntl .fcntl (fifo .fileno (), fcntl .F_GETPIPE_SZ ), pipesize )
682
- # Windows pipe size can be acquired with the GetNamedPipeInfoFunction
683
- # https://docs.microsoft.com/en-us/windows/win32/api/namedpipeapi/nf-namedpipeapi-getnamedpipeinfo
684
- # However, this function is not yet in _winapi.
685
- p .stdin .write (b"pear" )
686
- p .stdin .close ()
687
- p .wait ()
691
+ self .assertEqual (
692
+ fcntl .fcntl (fifo .fileno (), fcntl .F_GETPIPE_SZ ),
693
+ pipesize )
694
+ # Windows pipe size can be acquired via GetNamedPipeInfoFunction
695
+ # https://docs.microsoft.com/en-us/windows/win32/api/namedpipeapi/nf-namedpipeapi-getnamedpipeinfo
696
+ # However, this function is not yet in _winapi.
697
+ p .stdin .write (b"pear" )
698
+ p .stdin .close ()
699
+ finally :
700
+ p .kill ()
701
+ p .wait ()
688
702
703
+ @unittest .skipUnless (fcntl and hasattr (fcntl , 'F_GETPIPE_SZ' ),
704
+ 'fcntl.F_GETPIPE_SZ required for test.' )
689
705
def test_pipesize_default (self ):
690
- p = subprocess .Popen ([sys .executable , "-c" ,
691
- 'import sys; sys.stdin.read(); sys.stdout.write("out");'
692
- ' sys.stderr.write("error!")' ],
693
- stdin = subprocess .PIPE ,
694
- stdout = subprocess .PIPE ,
695
- stderr = subprocess .PIPE ,
696
- pipesize = - 1 )
697
- # UNIX tests using fcntl
698
- if sys .platform != "win32" and hasattr (fcntl , "F_GETPIPE_SZ" ):
706
+ p = subprocess .Popen (
707
+ [sys .executable , "-c" ,
708
+ 'import sys; sys.stdin.read(); sys.stdout.write("out"); '
709
+ 'sys.stderr.write("error!")' ],
710
+ stdin = subprocess .PIPE , stdout = subprocess .PIPE ,
711
+ stderr = subprocess .PIPE , pipesize = - 1 )
712
+ try :
699
713
fp_r , fp_w = os .pipe ()
700
- default_pipesize = fcntl .fcntl (fp_w , fcntl .F_GETPIPE_SZ )
701
- for fifo in [p .stdin , p .stdout , p .stderr ]:
702
- self .assertEqual (
703
- fcntl .fcntl (fifo .fileno (), fcntl .F_GETPIPE_SZ ), default_pipesize )
704
- # On other platforms we cannot test the pipe size (yet). But above code
705
- # using pipesize=-1 should not crash.
706
- p .stdin .close ()
707
- p .wait ()
714
+ try :
715
+ default_pipesize = fcntl .fcntl (fp_w , fcntl .F_GETPIPE_SZ )
716
+ for fifo in [p .stdin , p .stdout , p .stderr ]:
717
+ self .assertEqual (
718
+ fcntl .fcntl (fifo .fileno (), fcntl .F_GETPIPE_SZ ),
719
+ default_pipesize )
720
+ finally :
721
+ os .close (fp_r )
722
+ os .close (fp_w )
723
+ # On other platforms we cannot test the pipe size (yet). But above
724
+ # code using pipesize=-1 should not crash.
725
+ p .stdin .close ()
726
+ finally :
727
+ p .kill ()
728
+ p .wait ()
708
729
709
730
def test_env (self ):
710
731
newenv = os .environ .copy ()
0 commit comments