@@ -496,6 +496,32 @@ class StartupTests(TestBase):
496
496
# We want to ensure the initial state of subinterpreters
497
497
# matches expectations.
498
498
499
+ _debugged = False
500
+ _debugged_in_subtest = False
501
+ def debug (self , msg , * , header = None ):
502
+ if header :
503
+ self .debug (f'--- { header } ---' )
504
+ if msg :
505
+ if msg .endswith (os .linesep ):
506
+ self .debug (msg [:- len (os .linesep )])
507
+ else :
508
+ self .debug (msg )
509
+ self .debug ('<no newline>' )
510
+ self .debug ('------' )
511
+ return
512
+
513
+ if not self ._debugged :
514
+ print ()
515
+ self ._debugged = True
516
+ if self ._subtest is not None and not self ._debugged_in_subtest :
517
+ # The first subtest adds a leading newline, so we
518
+ # compensated in debug() by not printing a trailing
519
+ # newline. We fix that here.
520
+ print (msg , end = '' )
521
+ self ._debugged_in_subtest = True
522
+ else :
523
+ print (msg )
524
+
499
525
def create_temp_dir (self ):
500
526
import tempfile
501
527
tmp = tempfile .mkdtemp (prefix = 'test_interpreters_' )
@@ -512,36 +538,36 @@ def write_script(self, *path, text):
512
538
outfile .write (dedent (text ))
513
539
return filename
514
540
515
- def run_cmd (self , cmd , * , cwd = None ):
541
+ @support .requires_subprocess ()
542
+ def run_python (self , argv , * , cwd = None ):
516
543
# This method is inspired by
517
544
# EmbeddingTestsMixin.run_embedded_interpreter() in test_embed.py.
518
545
import shlex
519
546
import subprocess
520
- assert cmd .startswith ('python3 ' ), repr (cmd )
521
- if cmd .startswith ('python3 ' ):
522
- cmd = cmd .replace ('python3' , sys .executable , 1 )
523
- argv = shlex .split (cmd )
524
- proc = subprocess .run (
525
- argv ,
526
- cwd = cwd ,
527
- capture_output = True ,
528
- text = True ,
529
- )
530
- if proc .stderr != '' :
531
- # This is a hack until _PyThreadState_MustExit() is fixed.
532
- proc .returncode = 1
547
+ if isinstance (argv , str ):
548
+ argv = shlex .split (argv )
549
+ argv = [sys .executable , * argv ]
550
+ try :
551
+ proc = subprocess .run (
552
+ argv ,
553
+ cwd = cwd ,
554
+ capture_output = True ,
555
+ text = True ,
556
+ )
557
+ except Exception :
558
+ self .debug (f'# cmd: { shlex .join (argv )} ' )
559
+ raise # re-raise
560
+ assert proc .stderr == '' or proc .returncode != 0 , proc .stderr
533
561
if proc .returncode != 0 and support .verbose :
534
- print (f'--- { cmd } failed ---' )
535
- print (f'stdout:\n { proc .stdout } ' )
536
- print (f'stderr:\n { proc .stderr } ' )
537
- print ('------' )
562
+ self .debug (f'# python3 { shlex .join (argv [1 :])} failed:' )
563
+ self .debug (proc .stdout , header = 'stdout' )
564
+ self .debug (proc .stderr , header = 'stderr' )
538
565
self .assertEqual (proc .returncode , 0 )
539
566
self .assertEqual (proc .stderr , '' )
540
567
return proc .stdout
541
568
542
569
def test_sys_path_0 (self ):
543
570
# The main interpreter's sys.path[0] should be used by subinterpreters.
544
-
545
571
script = '''
546
572
import sys
547
573
from test.support import interpreters
@@ -558,7 +584,6 @@ def test_sys_path_0(self):
558
584
}}, indent=4), flush=True)
559
585
""")
560
586
'''
561
-
562
587
# <tmp>/
563
588
# pkg/
564
589
# __init__.py
@@ -572,15 +597,15 @@ def test_sys_path_0(self):
572
597
self .write_script (cwd , 'script.py' , text = script )
573
598
574
599
cases = [
575
- ('python3 script.py' , cwd ),
576
- ('python3 -m script' , cwd ),
577
- ('python3 -m pkg' , cwd ),
578
- ('python3 -m pkg.script' , cwd ),
579
- ('python3 -c "import script"' , '' ),
600
+ ('script.py' , cwd ),
601
+ ('-m script' , cwd ),
602
+ ('-m pkg' , cwd ),
603
+ ('-m pkg.script' , cwd ),
604
+ ('-c "import script"' , '' ),
580
605
]
581
- for cmd , expected in cases :
582
- with self .subTest (cmd ):
583
- out = self .run_cmd ( cmd , cwd = cwd )
606
+ for argv , expected in cases :
607
+ with self .subTest (f'python3 { argv } ' ):
608
+ out = self .run_python ( argv , cwd = cwd )
584
609
data = json .loads (out )
585
610
sp0_main , sp0_sub = data ['main' ], data ['sub' ]
586
611
self .assertEqual (sp0_sub , sp0_main )
0 commit comments