66
66
67
67
from test .support .script_helper import assert_python_ok
68
68
69
+
69
70
root_in_posix = False
70
71
if hasattr (os , 'geteuid' ):
71
72
root_in_posix = (os .geteuid () == 0 )
82
83
# Issue #14110: Some tests fail on FreeBSD if the user is in the wheel group.
83
84
HAVE_WHEEL_GROUP = sys .platform .startswith ('freebsd' ) and os .getgid () == 0
84
85
86
+
87
+ @contextlib .contextmanager
88
+ def ignore_deprecation_warnings (msg_regex , quiet = False ):
89
+ with support .check_warnings ((msg_regex , DeprecationWarning ), quiet = quiet ):
90
+ yield
91
+
92
+
93
+ @contextlib .contextmanager
94
+ def bytes_filename_warn (expected ):
95
+ msg = 'The Windows bytes API has been deprecated'
96
+ if os .name == 'nt' :
97
+ with ignore_deprecation_warnings (msg , quiet = not expected ):
98
+ yield
99
+ else :
100
+ yield
101
+
102
+
85
103
# Tests creating TESTFN
86
104
class FileTests (unittest .TestCase ):
87
105
def setUp (self ):
@@ -305,8 +323,7 @@ def test_stat_attributes_bytes(self):
305
323
fname = self .fname .encode (sys .getfilesystemencoding ())
306
324
except UnicodeEncodeError :
307
325
self .skipTest ("cannot encode %a for the filesystem" % self .fname )
308
- with warnings .catch_warnings ():
309
- warnings .simplefilter ("ignore" , DeprecationWarning )
326
+ with bytes_filename_warn (True ):
310
327
self .check_stat_attributes (fname )
311
328
312
329
def test_stat_result_pickle (self ):
@@ -443,15 +460,11 @@ def setUp(self):
443
460
fp .write (b"ABC" )
444
461
445
462
def restore_float_times (state ):
446
- with warnings .catch_warnings ():
447
- warnings .simplefilter ("ignore" , DeprecationWarning )
448
-
463
+ with ignore_deprecation_warnings ('stat_float_times' ):
449
464
os .stat_float_times (state )
450
465
451
466
# ensure that st_atime and st_mtime are float
452
- with warnings .catch_warnings ():
453
- warnings .simplefilter ("ignore" , DeprecationWarning )
454
-
467
+ with ignore_deprecation_warnings ('stat_float_times' ):
455
468
old_float_times = os .stat_float_times (- 1 )
456
469
self .addCleanup (restore_float_times , old_float_times )
457
470
@@ -1024,8 +1037,7 @@ def setUp(self):
1024
1037
super ().setUp ()
1025
1038
self .stack = contextlib .ExitStack ()
1026
1039
if os .name == 'nt' :
1027
- self .stack .enter_context (warnings .catch_warnings ())
1028
- warnings .simplefilter ("ignore" , DeprecationWarning )
1040
+ self .stack .enter_context (bytes_filename_warn (False ))
1029
1041
1030
1042
def tearDown (self ):
1031
1043
self .stack .close ()
@@ -1580,8 +1592,7 @@ def _test_link(self, file1, file2):
1580
1592
with open (file1 , "w" ) as f1 :
1581
1593
f1 .write ("test" )
1582
1594
1583
- with warnings .catch_warnings ():
1584
- warnings .simplefilter ("ignore" , DeprecationWarning )
1595
+ with bytes_filename_warn (False ):
1585
1596
os .link (file1 , file2 )
1586
1597
with open (file1 , "r" ) as f1 , open (file2 , "r" ) as f2 :
1587
1598
self .assertTrue (os .path .sameopenfile (f1 .fileno (), f2 .fileno ()))
@@ -1873,10 +1884,12 @@ def test_listdir_no_extended_path(self):
1873
1884
self .assertEqual (
1874
1885
sorted (os .listdir (support .TESTFN )),
1875
1886
self .created_paths )
1887
+
1876
1888
# bytes
1877
- self .assertEqual (
1878
- sorted (os .listdir (os .fsencode (support .TESTFN ))),
1879
- [os .fsencode (path ) for path in self .created_paths ])
1889
+ with bytes_filename_warn (False ):
1890
+ self .assertEqual (
1891
+ sorted (os .listdir (os .fsencode (support .TESTFN ))),
1892
+ [os .fsencode (path ) for path in self .created_paths ])
1880
1893
1881
1894
def test_listdir_extended_path (self ):
1882
1895
"""Test when the path starts with '\\ \\ ?\\ '."""
@@ -1886,11 +1899,13 @@ def test_listdir_extended_path(self):
1886
1899
self .assertEqual (
1887
1900
sorted (os .listdir (path )),
1888
1901
self .created_paths )
1902
+
1889
1903
# bytes
1890
- path = b'\\ \\ ?\\ ' + os .fsencode (os .path .abspath (support .TESTFN ))
1891
- self .assertEqual (
1892
- sorted (os .listdir (path )),
1893
- [os .fsencode (path ) for path in self .created_paths ])
1904
+ with bytes_filename_warn (False ):
1905
+ path = b'\\ \\ ?\\ ' + os .fsencode (os .path .abspath (support .TESTFN ))
1906
+ self .assertEqual (
1907
+ sorted (os .listdir (path )),
1908
+ [os .fsencode (path ) for path in self .created_paths ])
1894
1909
1895
1910
1896
1911
@unittest .skipUnless (sys .platform == "win32" , "Win32 specific tests" )
@@ -1965,9 +1980,9 @@ def check_stat(self, link, target):
1965
1980
self .assertNotEqual (os .lstat (link ), os .stat (link ))
1966
1981
1967
1982
bytes_link = os .fsencode (link )
1968
- with warnings .catch_warnings ():
1969
- warnings .simplefilter ("ignore" , DeprecationWarning )
1983
+ with bytes_filename_warn (True ):
1970
1984
self .assertEqual (os .stat (bytes_link ), os .stat (target ))
1985
+ with bytes_filename_warn (True ):
1971
1986
self .assertNotEqual (os .lstat (bytes_link ), os .stat (bytes_link ))
1972
1987
1973
1988
def test_12084 (self ):
@@ -2529,36 +2544,37 @@ class Win32DeprecatedBytesAPI(unittest.TestCase):
2529
2544
def test_deprecated (self ):
2530
2545
import nt
2531
2546
filename = os .fsencode (support .TESTFN )
2532
- with warnings .catch_warnings ():
2533
- warnings .simplefilter ("error" , DeprecationWarning )
2534
- for func , * args in (
2535
- (nt ._getfullpathname , filename ),
2536
- (nt ._isdir , filename ),
2537
- (os .access , filename , os .R_OK ),
2538
- (os .chdir , filename ),
2539
- (os .chmod , filename , 0o777 ),
2540
- (os .getcwdb ,),
2541
- (os .link , filename , filename ),
2542
- (os .listdir , filename ),
2543
- (os .lstat , filename ),
2544
- (os .mkdir , filename ),
2545
- (os .open , filename , os .O_RDONLY ),
2546
- (os .rename , filename , filename ),
2547
- (os .rmdir , filename ),
2548
- (os .startfile , filename ),
2549
- (os .stat , filename ),
2550
- (os .unlink , filename ),
2551
- (os .utime , filename ),
2552
- ):
2553
- self .assertRaises (DeprecationWarning , func , * args )
2547
+ for func , * args in (
2548
+ (nt ._getfullpathname , filename ),
2549
+ (nt ._isdir , filename ),
2550
+ (os .access , filename , os .R_OK ),
2551
+ (os .chdir , filename ),
2552
+ (os .chmod , filename , 0o777 ),
2553
+ (os .getcwdb ,),
2554
+ (os .link , filename , filename ),
2555
+ (os .listdir , filename ),
2556
+ (os .lstat , filename ),
2557
+ (os .mkdir , filename ),
2558
+ (os .open , filename , os .O_RDONLY ),
2559
+ (os .rename , filename , filename ),
2560
+ (os .rmdir , filename ),
2561
+ (os .startfile , filename ),
2562
+ (os .stat , filename ),
2563
+ (os .unlink , filename ),
2564
+ (os .utime , filename ),
2565
+ ):
2566
+ with bytes_filename_warn (True ):
2567
+ try :
2568
+ func (* args )
2569
+ except OSError :
2570
+ # ignore OSError, we only care about DeprecationWarning
2571
+ pass
2554
2572
2555
2573
@support .skip_unless_symlink
2556
2574
def test_symlink (self ):
2557
2575
filename = os .fsencode (support .TESTFN )
2558
- with warnings .catch_warnings ():
2559
- warnings .simplefilter ("error" , DeprecationWarning )
2560
- self .assertRaises (DeprecationWarning ,
2561
- os .symlink , filename , filename )
2576
+ with bytes_filename_warn (True ):
2577
+ os .symlink (filename , filename )
2562
2578
2563
2579
2564
2580
@unittest .skipUnless (hasattr (os , 'get_terminal_size' ), "requires os.get_terminal_size" )
@@ -2696,7 +2712,8 @@ def test_oserror_filename(self):
2696
2712
for filenames , func , * func_args in funcs :
2697
2713
for name in filenames :
2698
2714
try :
2699
- func (name , * func_args )
2715
+ with bytes_filename_warn (False ):
2716
+ func (name , * func_args )
2700
2717
except OSError as err :
2701
2718
self .assertIs (err .filename , name )
2702
2719
else :
@@ -3011,7 +3028,8 @@ def test_broken_symlink(self):
3011
3028
def test_bytes (self ):
3012
3029
if os .name == "nt" :
3013
3030
# On Windows, os.scandir(bytes) must raise an exception
3014
- self .assertRaises (TypeError , os .scandir , b'.' )
3031
+ with bytes_filename_warn (True ):
3032
+ self .assertRaises (TypeError , os .scandir , b'.' )
3015
3033
return
3016
3034
3017
3035
self .create_file ("file.txt" )
0 commit comments