@@ -2510,19 +2510,49 @@ def test_isatty(self):
25102510
25112511 @unittest .skipUnless (hasattr (os , 'closerange' ), 'test needs os.closerange()' )
25122512 def test_closerange (self ):
2513- fd = os_helper .make_bad_fd ()
2514- # Make sure none of the descriptors we are about to close are
2515- # currently valid (issue 6542).
2516- for i in range (10 ):
2517- try : os .fstat (fd + i )
2518- except OSError :
2519- pass
2520- else :
2521- break
2522- if i < 2 :
2523- raise unittest .SkipTest (
2524- "Unable to acquire a range of invalid file descriptors" )
2525- self .assertEqual (os .closerange (fd , fd + i - 1 ), None )
2513+ if support .is_mac_catalyst :
2514+ # On Mac Catalyst, somehow we're not detecting a guarded FD
2515+ # using fstat. We make some random fds first, stop once not
2516+ # consecutive, close all of them one by one using the result
2517+ # of open(), then assert that closerange is failing.
2518+ #
2519+ # This ensures that none of those things we're closing is
2520+ # guarded, if we're careful to not use code that makes guarded
2521+ # file descriptors.
2522+
2523+ copies = []
2524+ # Open a file for testing and get its FD
2525+ file = open (os_helper .TESTFN , "wb" )
2526+ fd = file .fileno ()
2527+ copies .append (file )
2528+ for i in range (10 ):
2529+ file_dup = open (os_helper .TESTFN , "wb" )
2530+ if file_dup .fileno () != fd + i :
2531+ file_dup .close ()
2532+ break
2533+ else :
2534+ copies .append (file_dup )
2535+ # Close everything
2536+ for copy in copies :
2537+ copy .close ()
2538+ os .unlink (os_helper .TESTFN )
2539+
2540+ # Now we're left with invalid FDs. Let's go close them!
2541+ self .assertEqual (os .closerange (fd , fd + i - 1 ), None )
2542+ else :
2543+ fd = os_helper .make_bad_fd ()
2544+ # Make sure none of the descriptors we are about to close are
2545+ # currently valid (issue 6542).
2546+ for i in range (10 ):
2547+ try : os .fstat (fd + i )
2548+ except OSError :
2549+ pass
2550+ else :
2551+ break
2552+ if i < 2 :
2553+ raise unittest .SkipTest (
2554+ "Unable to acquire a range of invalid file descriptors" )
2555+ self .assertEqual (os .closerange (fd , fd + i - 1 ), None )
25262556
25272557 @unittest .skipUnless (hasattr (os , 'dup2' ), 'test needs os.dup2()' )
25282558 def test_dup2 (self ):
0 commit comments