12
12
import textwrap
13
13
import unittest
14
14
import warnings
15
- from test .support import no_tracing , verbose
15
+ from test .support import (
16
+ force_not_colorized_test_class ,
17
+ infinite_recursion ,
18
+ no_tracing ,
19
+ requires_resource ,
20
+ requires_subprocess ,
21
+ verbose ,
22
+ )
16
23
from test .support .import_helper import forget , make_legacy_pyc , unload
17
- from test .support .os_helper import create_empty_file , temp_dir
24
+ from test .support .os_helper import create_empty_file , temp_dir , FakePath
18
25
from test .support .script_helper import make_script , make_zip_script
19
26
20
27
@@ -656,13 +663,14 @@ def test_basic_script(self):
656
663
self ._check_script (script_name , "<run_path>" , script_name ,
657
664
script_name , expect_spec = False )
658
665
659
- def test_basic_script_with_path_object (self ):
666
+ def test_basic_script_with_pathlike_object (self ):
660
667
with temp_dir () as script_dir :
661
668
mod_name = 'script'
662
- script_name = pathlib .Path (self ._make_test_script (script_dir ,
663
- mod_name ))
664
- self ._check_script (script_name , "<run_path>" , script_name ,
665
- script_name , expect_spec = False )
669
+ script_name = self ._make_test_script (script_dir , mod_name )
670
+ self ._check_script (FakePath (script_name ), "<run_path>" ,
671
+ script_name ,
672
+ script_name ,
673
+ expect_spec = False )
666
674
667
675
def test_basic_script_no_suffix (self ):
668
676
with temp_dir () as script_dir :
@@ -734,17 +742,18 @@ def test_zipfile_error(self):
734
742
self ._check_import_error (zip_name , msg )
735
743
736
744
@no_tracing
745
+ @requires_resource ('cpu' )
737
746
def test_main_recursion_error (self ):
738
747
with temp_dir () as script_dir , temp_dir () as dummy_dir :
739
748
mod_name = '__main__'
740
749
source = ("import runpy\n "
741
750
"runpy.run_path(%r)\n " ) % dummy_dir
742
751
script_name = self ._make_test_script (script_dir , mod_name , source )
743
752
zip_name , fname = make_zip_script (script_dir , 'test_zip' , script_name )
744
- self .assertRaises (RecursionError , run_path , zip_name )
753
+ with infinite_recursion (25 ):
754
+ self .assertRaises (RecursionError , run_path , zip_name )
745
755
746
- # TODO: RUSTPYTHON, detect encoding comments in files
747
- @unittest .expectedFailure
756
+ @unittest .expectedFailure # TODO: RUSTPYTHON; detect encoding comments in files
748
757
def test_encoding (self ):
749
758
with temp_dir () as script_dir :
750
759
filename = os .path .join (script_dir , 'script.py' )
@@ -757,6 +766,7 @@ def test_encoding(self):
757
766
self .assertEqual (result ['s' ], "non-ASCII: h\xe9 " )
758
767
759
768
769
+ @force_not_colorized_test_class
760
770
class TestExit (unittest .TestCase ):
761
771
STATUS_CONTROL_C_EXIT = 0xC000013A
762
772
EXPECTED_CODE = (
@@ -783,16 +793,19 @@ def run(self, *args, **kwargs):
783
793
)
784
794
super ().run (* args , ** kwargs )
785
795
786
- def assertSigInt (self , * args , ** kwargs ):
787
- proc = subprocess .run (* args , ** kwargs , text = True , stderr = subprocess .PIPE )
788
- self .assertTrue (proc .stderr .endswith ("\n KeyboardInterrupt\n " ))
796
+ @requires_subprocess ()
797
+ def assertSigInt (self , cmd , * args , ** kwargs ):
798
+ # Use -E to ignore PYTHONSAFEPATH
799
+ cmd = [sys .executable , '-E' , * cmd ]
800
+ proc = subprocess .run (cmd , * args , ** kwargs , text = True , stderr = subprocess .PIPE )
801
+ self .assertTrue (proc .stderr .endswith ("\n KeyboardInterrupt\n " ), proc .stderr )
789
802
self .assertEqual (proc .returncode , self .EXPECTED_CODE )
790
803
791
- @unittest .expectedFailureIfWindows ("TODO: RUSTPYTHON" )
804
+ @unittest .expectedFailureIfWindows ("TODO: RUSTPYTHON; " )
792
805
def test_pymain_run_file (self ):
793
- self .assertSigInt ([sys . executable , self .ham ])
806
+ self .assertSigInt ([self .ham ])
794
807
795
- @unittest .expectedFailureIfWindows ("TODO: RUSTPYTHON" )
808
+ @unittest .expectedFailureIfWindows ("TODO: RUSTPYTHON; " )
796
809
def test_pymain_run_file_runpy_run_module (self ):
797
810
tmp = self .ham .parent
798
811
run_module = tmp / "run_module.py"
@@ -804,9 +817,9 @@ def test_pymain_run_file_runpy_run_module(self):
804
817
"""
805
818
)
806
819
)
807
- self .assertSigInt ([sys . executable , run_module ], cwd = tmp )
820
+ self .assertSigInt ([run_module ], cwd = tmp )
808
821
809
- @unittest .expectedFailureIfWindows ("TODO: RUSTPYTHON" )
822
+ @unittest .expectedFailureIfWindows ("TODO: RUSTPYTHON; " )
810
823
def test_pymain_run_file_runpy_run_module_as_main (self ):
811
824
tmp = self .ham .parent
812
825
run_module_as_main = tmp / "run_module_as_main.py"
@@ -818,28 +831,27 @@ def test_pymain_run_file_runpy_run_module_as_main(self):
818
831
"""
819
832
)
820
833
)
821
- self .assertSigInt ([sys . executable , run_module_as_main ], cwd = tmp )
834
+ self .assertSigInt ([run_module_as_main ], cwd = tmp )
822
835
823
- @unittest .expectedFailureIfWindows ("TODO: RUSTPYTHON" )
836
+ @unittest .expectedFailureIfWindows ("TODO: RUSTPYTHON; " )
824
837
def test_pymain_run_command_run_module (self ):
825
838
self .assertSigInt (
826
- [sys . executable , "-c" , "import runpy; runpy.run_module('ham')" ],
839
+ ["-c" , "import runpy; runpy.run_module('ham')" ],
827
840
cwd = self .ham .parent ,
828
841
)
829
842
830
- @unittest .expectedFailureIfWindows ("TODO: RUSTPYTHON" )
843
+ @unittest .expectedFailureIfWindows ("TODO: RUSTPYTHON; " )
831
844
def test_pymain_run_command (self ):
832
- self .assertSigInt ([sys . executable , "-c" , "import ham" ], cwd = self .ham .parent )
845
+ self .assertSigInt (["-c" , "import ham" ], cwd = self .ham .parent )
833
846
834
- # TODO: RUSTPYTHON
835
- @unittest .expectedFailure
847
+ @unittest .expectedFailure # TODO: RUSTPYTHON
836
848
def test_pymain_run_stdin (self ):
837
- self .assertSigInt ([sys . executable ], input = "import ham" , cwd = self .ham .parent )
849
+ self .assertSigInt ([], input = "import ham" , cwd = self .ham .parent )
838
850
839
- @unittest .expectedFailureIfWindows ("TODO: RUSTPYTHON" )
851
+ @unittest .expectedFailureIfWindows ("TODO: RUSTPYTHON; " )
840
852
def test_pymain_run_module (self ):
841
853
ham = self .ham
842
- self .assertSigInt ([sys . executable , "-m" , ham .stem ], cwd = ham .parent )
854
+ self .assertSigInt (["-m" , ham .stem ], cwd = ham .parent )
843
855
844
856
845
857
if __name__ == "__main__" :
0 commit comments