@@ -2778,12 +2778,6 @@ def test_fpathconf_bad_fd(self):
27782778 self .check (os .pathconf , "PC_NAME_MAX" )
27792779 self .check (os .fpathconf , "PC_NAME_MAX" )
27802780
2781- @contextlib .contextmanager
2782- def check_for_ebadf (self , errnos = (errno .EBADF ,)):
2783- with self .assertRaises (OSError ) as ctx :
2784- yield
2785- self .assertIn (ctx .exception .errno , errnos )
2786-
27872781 @unittest .skipUnless (hasattr (os , 'pathconf' ), 'test needs os.pathconf()' )
27882782 @unittest .skipIf (
27892783 support .linked_to_musl (),
@@ -2793,53 +2787,48 @@ def test_pathconf_negative_fd_uses_fd_semantics(self):
27932787 if os .pathconf not in os .supports_fd :
27942788 self .skipTest ('needs fpathconf()' )
27952789
2796- with self .check_for_ebadf () :
2790+ with self .assertRaises ( OSError ) as ctx :
27972791 os .pathconf (- 1 , 1 )
2792+ self .assertEqual (ctx .exception .errno , errno .EBADF )
27982793
27992794 @support .subTests ("fd" , [- 1 , - 5 ])
28002795 def test_negative_fd_ebadf (self , fd ):
2801- with self .check_for_ebadf ():
2802- os .stat (fd )
2796+ tests = [(os .stat , fd )]
28032797 if hasattr (os , "statx" ):
2804- with self .check_for_ebadf ():
2805- os .statx (fd , mask = 0 )
2798+ tests .append ((os .statx , fd , 0 ))
28062799 if os .chdir in os .supports_fd :
2807- with self .check_for_ebadf ():
2808- os .chdir (fd )
2800+ tests .append ((os .chdir , fd ))
28092801 if os .chmod in os .supports_fd :
2810- with self .check_for_ebadf ():
2811- os .chmod (fd , 0o777 )
2802+ tests .append ((os .chmod , fd , 0o777 ))
28122803 if hasattr (os , "chown" ) and os .chown in os .supports_fd :
2813- with self .check_for_ebadf ():
2814- os .chown (fd , 0 , 0 )
2804+ tests .append ((os .chown , fd , 0 , 0 ))
28152805 if os .listdir in os .supports_fd :
2816- with self .check_for_ebadf ():
2817- os .listdir (fd )
2806+ tests .append ((os .listdir , fd ))
28182807 if os .utime in os .supports_fd :
2819- with self .check_for_ebadf ():
2820- os .utime (fd , (0 , 0 ))
2821- if hasattr (os , "execve" ) and os .execve in os .supports_fd :
2822- # glibc fails with EINVAL, musl fails with EBADF
2823- with self .check_for_ebadf (errnos = (errno .EBADF , errno .EINVAL )):
2824- os .execve (fd , [sys .executable , "-c" , "pass" ], os .environ )
2808+ tests .append ((os .utime , fd , (0 , 0 )))
28252809 if hasattr (os , "truncate" ) and os .truncate in os .supports_fd :
2826- with self .check_for_ebadf ():
2827- os .truncate (fd , 0 )
2810+ tests .append ((os .truncate , fd , 0 ))
28282811 if hasattr (os , 'statvfs' ) and os .statvfs in os .supports_fd :
2829- with self .check_for_ebadf ():
2830- os .statvfs (fd )
2812+ tests .append ((os .statvfs , fd ))
28312813 if hasattr (os , "setxattr" ):
2832- with self .check_for_ebadf ():
2833- os .getxattr (fd , b"user.test" )
2834- with self .check_for_ebadf ():
2835- os .setxattr (fd , b"user.test" , b"1" )
2836- with self .check_for_ebadf ():
2837- os .removexattr (fd , b"user.test" )
2838- with self .check_for_ebadf ():
2839- os .listxattr (fd )
2814+ tests .append ((os .getxattr , fd , b"user.test" ))
2815+ tests .append ((os .setxattr , fd , b"user.test" , b"1" ))
2816+ tests .append ((os .removexattr , fd , b"user.test" ))
2817+ tests .append ((os .listxattr , fd ))
28402818 if os .scandir in os .supports_fd :
2841- with self .check_for_ebadf ():
2842- os .scandir (fd )
2819+ tests .append ((os .scandir , fd ))
2820+
2821+ for func , * args in tests :
2822+ with self .subTest (func = func , args = args ):
2823+ with self .assertRaises (OSError ) as ctx :
2824+ func (* args )
2825+ self .assertEqual (ctx .exception .errno , errno .EBADF )
2826+
2827+ if hasattr (os , "execve" ) and os .execve in os .supports_fd :
2828+ # glibc fails with EINVAL, musl fails with EBADF
2829+ with self .assertRaises (OSError ) as ctx :
2830+ os .execve (fd , [sys .executable , "-c" , "pass" ], os .environ )
2831+ self .assertIn (ctx .exception .errno , (errno .EBADF , errno .EINVAL ))
28432832
28442833 if support .MS_WINDOWS :
28452834 import nt
0 commit comments