1- import contextlib
21import errno
32import importlib
43import io
1211import textwrap
1312import time
1413import unittest
14+ import warnings
15+
1516from test import support
1617from test .support import import_helper
1718from test .support import os_helper
2324
2425
2526class TestSupport (unittest .TestCase ):
27+ @classmethod
28+ def setUpClass (cls ):
29+ orig_filter_len = len (warnings .filters )
30+ cls ._warnings_helper_token = support .ignore_deprecations_from (
31+ "test.support.warnings_helper" , like = ".*used in test_support.*"
32+ )
33+ cls ._test_support_token = support .ignore_deprecations_from (
34+ "test.test_support" , like = ".*You should NOT be seeing this.*"
35+ )
36+ assert len (warnings .filters ) == orig_filter_len + 2
37+
38+ @classmethod
39+ def tearDownClass (cls ):
40+ orig_filter_len = len (warnings .filters )
41+ support .clear_ignored_deprecations (
42+ cls ._warnings_helper_token ,
43+ cls ._test_support_token ,
44+ )
45+ assert len (warnings .filters ) == orig_filter_len - 2
46+
47+ def test_ignored_deprecations_are_silent (self ):
48+ """Test support.ignore_deprecations_from() silences warnings"""
49+ with warnings .catch_warnings (record = True ) as warning_objs :
50+ warnings_helper ._warn_about_deprecation ()
51+ warnings .warn ("You should NOT be seeing this." , DeprecationWarning )
52+ messages = [str (w .message ) for w in warning_objs ]
53+ self .assertEqual (len (messages ), 0 , messages )
2654
2755 def test_import_module (self ):
2856 import_helper .import_module ("ftplib" )
29- self .assertRaises (unittest .SkipTest , import_helper .import_module , "foo" )
57+ self .assertRaises (unittest .SkipTest ,
58+ import_helper .import_module , "foo" )
3059
3160 def test_import_fresh_module (self ):
3261 import_helper .import_fresh_module ("ftplib" )
@@ -47,7 +76,7 @@ def test_unload(self):
4776 self .assertNotIn ("sched" , sys .modules )
4877
4978 def test_unlink (self ):
50- with open (TESTFN , "w" ) as f :
79+ with open (TESTFN , "w" , encoding = "utf-8" ) as f :
5180 pass
5281 os_helper .unlink (TESTFN )
5382 self .assertFalse (os .path .exists (TESTFN ))
@@ -79,7 +108,7 @@ def test_rmtree(self):
79108
80109 def test_forget (self ):
81110 mod_filename = TESTFN + '.py'
82- with open (mod_filename , 'w' ) as f :
111+ with open (mod_filename , 'w' , encoding = "utf-8" ) as f :
83112 print ('foo = 1' , file = f )
84113 sys .path .insert (0 , os .curdir )
85114 importlib .invalidate_caches ()
@@ -177,16 +206,14 @@ def test_temp_dir__forked_child(self):
177206 script_helper .assert_python_ok ("-c" , textwrap .dedent ("""
178207 import os
179208 from test import support
209+ from test.support import os_helper
180210 with os_helper.temp_cwd() as temp_path:
181211 pid = os.fork()
182212 if pid != 0:
183- # parent process (child has pid == 0)
213+ # parent process
184214
185215 # wait for the child to terminate
186- (pid, status) = os.waitpid(pid, 0)
187- if status != 0:
188- raise AssertionError(f"Child process failed with exit "
189- f"status indication 0x{status:x}.")
216+ support.wait_process(pid, exitcode=0)
190217
191218 # Make sure that temp_path is still present. When the child
192219 # process leaves the 'temp_cwd'-context, the __exit__()-
@@ -295,8 +322,8 @@ def test_check_syntax_error(self):
295322
296323 def test_CleanImport (self ):
297324 import importlib
298- with import_helper .CleanImport ("asyncore " ):
299- importlib .import_module ("asyncore " )
325+ with import_helper .CleanImport ("pprint " ):
326+ importlib .import_module ("pprint " )
300327
301328 def test_DirsOnSysPath (self ):
302329 with import_helper .DirsOnSysPath ('foo' , 'bar' ):
@@ -414,8 +441,8 @@ def test_check__all__(self):
414441
415442 self .assertRaises (AssertionError , support .check__all__ , self , unittest )
416443
417- @unittest .skipUnless (hasattr (os , 'waitpid' ) and hasattr (os , 'WNOHANG' ) and hasattr ( os , 'fork' ) ,
418- 'need os.waitpid() and os.WNOHANG and os.fork() ' )
444+ @unittest .skipUnless (hasattr (os , 'waitpid' ) and hasattr (os , 'WNOHANG' ),
445+ 'need os.waitpid() and os.WNOHANG' )
419446 def test_reap_children (self ):
420447 # Make sure that there is no other pending child process
421448 support .reap_children ()
@@ -427,7 +454,7 @@ def test_reap_children(self):
427454 os ._exit (0 )
428455
429456 t0 = time .monotonic ()
430- deadline = time .monotonic () + 60.0
457+ deadline = time .monotonic () + support . SHORT_TIMEOUT
431458
432459 was_altered = support .environment_altered
433460 try :
@@ -502,7 +529,6 @@ def test_args_from_interpreter_flags(self):
502529 ['-Wignore' , '-X' , 'dev' ],
503530 ['-X' , 'faulthandler' ],
504531 ['-X' , 'importtime' ],
505- ['-X' , 'showalloccount' ],
506532 ['-X' , 'showrefcount' ],
507533 ['-X' , 'tracemalloc' ],
508534 ['-X' , 'tracemalloc=3' ],
@@ -647,7 +673,7 @@ def test_fd_count(self):
647673 def check_print_warning (self , msg , expected ):
648674 stderr = io .StringIO ()
649675
650- old_stderr = sys .__stderr__
676+ old_stderr = sys .__stderr__
651677 try :
652678 sys .__stderr__ = stderr
653679 support .print_warning (msg )
@@ -671,7 +697,6 @@ def test_print_warning(self):
671697 # findfile
672698 # check_warnings
673699 # EnvironmentVarGuard
674- # TransientResource
675700 # transient_internet
676701 # run_with_locale
677702 # set_memlimit
@@ -682,15 +707,10 @@ def test_print_warning(self):
682707 # run_doctest
683708 # threading_cleanup
684709 # reap_threads
685- # strip_python_stderr
686710 # can_symlink
687711 # skip_unless_symlink
688712 # SuppressCrashReport
689713
690714
691- def test_main ():
692- tests = [TestSupport ]
693- support .run_unittest (* tests )
694-
695715if __name__ == '__main__' :
696- test_main ()
716+ unittest . main ()
0 commit comments