8000 Adjust the test. · python/cpython@b94afa7 · GitHub
[go: up one dir, main page]

Skip to content

Commit b94afa7

Browse files
Adjust the test.
1 parent e36e0d2 commit b94afa7

File tree

1 file changed

+53
-28
lines changed

1 file changed

+53
-28
lines changed

Lib/test/test_interpreters.py

Lines changed: 53 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,32 @@ class StartupTests(TestBase):
496496
# We want to ensure the initial state of subinterpreters
497497
# matches expectations.
498498

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+
499525
def create_temp_dir(self):
500526
import tempfile
501527
tmp = tempfile.mkdtemp(prefix='test_interpreters_')
@@ -512,36 +538,36 @@ def write_script(self, *path, text):
512538
outfile.write(dedent(text))
513539
return filename
514540

515-
def run_cmd(self, cmd, *, cwd=None):
541+
@support.requires_subprocess()
542+
def run_python(self, argv, *, cwd=None):
516543
# This method is inspired by
517544
# EmbeddingTestsMixin.run_embedded_interpreter() in test_embed.py.
518545
import shlex
519546
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
533561
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')
538565
self.assertEqual(proc.returncode, 0)
539566
self.assertEqual(proc.stderr, '')
540567
return proc.stdout
541568

542569
def test_sys_path_0(self):
543570
# The main interpreter's sys.path[0] should be used by subinterpreters.
544-
545571
script = '''
546572
import sys
547573
from test.support import interpreters
@@ -558,7 +584,6 @@ def test_sys_path_0(self):
558584
}}, indent=4), flush=True)
559585
""")
560586
'''
561-
562587
# <tmp>/
563588
# pkg/
564589
# __init__.py
@@ -572,15 +597,15 @@ def test_sys_path_0(self):
572597
self.write_script(cwd, 'script.py', text=script)
573598

574599
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"', ''),
580605
]
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)
584609
data = json.loads(out)
585610
sp0_main, sp0_sub = data['main'], data['sub']
586611
self.assertEqual(sp0_sub, sp0_main)

0 commit comments

Comments
 (0)
0