88import test .support
99from test import support
1010from test .support .script_helper import assert_python_ok
11+ from test .support import import_helper
1112from test .support import os_helper
1213from test .support import socket_helper
1314from test .support import captured_stderr
@@ -308,8 +309,7 @@ def test_getuserbase(self):
308309
309310 with EnvironmentVarGuard () as environ :
310311 environ ['PYTHONUSERBASE' ] = 'xoxo'
311- self .assertTrue (site .getuserbase ().startswith ('xoxo' ),
312- site .getuserbase ())
312+ self .assertTrue (site .getuserbase ().startswith ('xoxo' ))
313313
314314 @unittest .skipUnless (HAS_USER_SITE , 'need user site' )
315315 def test_getusersitepackages (self ):
@@ -319,7 +319,7 @@ def test_getusersitepackages(self):
319319
320320 # the call sets USER_BASE *and* USER_SITE
321321 self .assertEqual (site .USER_SITE , user_site )
322- self .assertTrue (user_site .startswith (site .USER_BASE ), user_site )
322+ self .assertTrue (user_site .startswith (site .USER_BASE ))
323323 self .assertEqual (site .USER_BASE , site .getuserbase ())
324324
325325 def test_getsitepackages (self ):
@@ -362,11 +362,10 @@ def test_no_home_directory(self):
362362 environ .unset ('PYTHONUSERBASE' , 'APPDATA' )
363363
364364 user_base = site .getuserbase ()
365- self .assertTrue (user_base .startswith ('~' + os .sep ),
366- user_base )
365+ self .assertTrue (user_base .startswith ('~' + os .sep ))
367366
368367 user_site = site .getusersitepackages ()
369- self .assertTrue (user_site .startswith (user_base ), user_site )
368+ self .assertTrue (user_site .startswith (user_base ))
370369
371370 with mock .patch ('os.path.isdir' , return_value = False ) as mock_isdir , \
372371 mock .patch .object (site , 'addsitedir' ) as mock_addsitedir , \
@@ -515,7 +514,7 @@ def test_sitecustomize_executed(self):
515514 # If sitecustomize is available, it should have been imported.
516515 if "sitecustomize" not in sys .modules :
517516 try :
518- import sitecustomize
517+ import sitecustomize # noqa: F401
519518 except ImportError :
520519 pass
521520 else :
@@ -578,6 +577,17 @@ def test_license_exists_at_url(self):
578577 code = e .code
579578 self .assertEqual (code , 200 , msg = "Can't find " + url )
580579
580+ @support .cpython_only
581+ def test_lazy_imports (self ):
582+ import_helper .ensure_lazy_imports ("site" , [
583+ "io" ,
584+ "locale" ,
585+ "traceback" ,
586+ "atexit" ,
587+ "warnings" ,
588+ "textwrap" ,
589+ ])
590+
581591
582592class StartupImportTests (unittest .TestCase ):
583593
@@ -843,12 +853,15 @@ def get_excepted_output(self, *args):
843853 return 10 , None
844854
845855 def invoke_command_line (self , * args ):
846- args = ["-m" , "site" , * args ]
856+ cmd_args = []
857+ if sys .flags .no_user_site :
858+ cmd_args .append ("-s" )
859+ cmd_args .extend (["-m" , "site" , * args ])
847860
848861 with EnvironmentVarGuard () as env :
849862 env ["PYTHONUTF8" ] = "1"
850863 env ["PYTHONIOENCODING" ] = "utf-8"
851- proc = spawn_python (* args , text = True , env = env ,
864+ proc = spawn_python (* cmd_args , text = True , env = env ,
852865 encoding = 'utf-8' , errors = 'replace' )
853866
854867 output = kill_python (proc )
0 commit comments