Skip to content

Commit 9d3470d

Browse files
authored
Update test_subprocess.py
1 parent 2a66dd3 commit 9d3470d

File tree

1 file changed

+41
-27
lines changed

1 file changed

+41
-27
lines changed

Lib/test/test_asyncio/test_subprocess.py

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,26 @@
2525
if support.check_sanitizer(address=True):
2626
raise unittest.SkipTest("Exposes ASAN flakiness in GitHub CI")
2727

28+
29+
def get_quoted_sys_executable():
30+
if hasattr(get_quoted_sys_executable, '_cached_get_quoted_sys_executable'):
31+
return get_quoted_sys_executable._cached_get_quoted_sys_executable
32+
33+
sysExecutable = sys.executable
34+
if not ((sysExecutable.startswith('"') and sysExecutable.endswith('"')) or
35+
(sysExecutable.startswith("'") and sysExecutable.endswith("'"))):
36+
if ' ' in (sysExecutable if sys.platform == 'win32' else sysExecutable.replace('\\ ', '')):
37+
sysExecutable = f'"{sysExecutable}"'
38+
39+
get_quoted_sys_executable._cached_get_quoted_sys_executable = sysExecutable
40+
return sysExecutable
41+
2842
# Program blocking
29-
PROGRAM_BLOCKED = [sys.executable, '-c', 'import time; time.sleep(3600)']
43+
PROGRAM_BLOCKED = [get_quoted_sys_executable(), '-c', 'import time; time.sleep(3600)']
3044

3145
# Program copying input to output
3246
PROGRAM_CAT = [
33-
sys.executable, '-c',
47+
get_quoted_sys_executable(), '-c',
3448
';'.join(('import sys',
3549
'data = sys.stdin.buffer.read()',
3650
'sys.stdout.buffer.write(data)'))]
@@ -209,7 +223,7 @@ def test_kill(self):
209223

210224
def test_kill_issue43884(self):
211225
if sys.platform == 'win32':
212-
blocking_shell_command = f'"{sys.executable}" -c "import time; time.sleep(2)"'
226+
blocking_shell_command = f'"{get_quoted_sys_executable()}" -c "import time; time.sleep(2)"'
213227
else:
214228
blocking_shell_command = 'sleep 1; sleep 1'
215229
creationflags = 0
@@ -255,7 +269,7 @@ def test_send_signal(self):
255269
old_handler = signal.signal(signal.SIGHUP, signal.SIG_DFL)
256270
try:
257271
code = 'import time; print("sleeping", flush=True); time.sleep(3600)'
258-
args = [sys.executable, '-c', code]
272+
args = [get_quoted_sys_executable(), '-c', code]
259273
proc = self.loop.run_until_complete(
260274
asyncio.create_subprocess_exec(
261275
*args,
@@ -304,7 +318,7 @@ def test_stdin_broken_pipe(self):
304318
# the program ends before the stdin can be fed
305319
proc = self.loop.run_until_complete(
306320
asyncio.create_subprocess_exec(
307-
sys.executable, '-c', code,
321+
get_quoted_sys_executable(), '-c', code,
308322
stdin=subprocess.PIPE,
309323
**kwargs
310324
)
@@ -330,7 +344,7 @@ def test_communicate_ignore_broken_pipe(self):
330344
# the program ends before the stdin can be fed
331345
proc = self.loop.run_until_complete(
332346
asyncio.create_subprocess_exec(
333-
sys.executable, '-c', 'pass',
347+
get_quoted_sys_executable(), '-c', 'pass',
334348
stdin=subprocess.PIPE,
335349
)
336350
)
@@ -362,7 +376,7 @@ async def connect_read_pipe_mock(*args, **kw):
362376
self.loop.connect_read_pipe = connect_read_pipe_mock
363377

364378
proc = await asyncio.create_subprocess_exec(
365-
sys.executable, '-c', code,
379+
get_quoted_sys_executable(), '-c', code,
366380
stdin=asyncio.subprocess.PIPE,
367381
stdout=asyncio.subprocess.PIPE,
368382
limit=limit,
@@ -390,7 +404,7 @@ def test_stdin_not_inheritable(self):
390404
async def len_message(message):
391405
code = 'import sys; data = sys.stdin.read(); print(len(data))'
392406
proc = await asyncio.create_subprocess_exec(
393-
sys.executable, '-c', code,
407+
get_quoted_sys_executable(), '-c', code,
394408
stdin=asyncio.subprocess.PIPE,
395409
stdout=asyncio.subprocess.PIPE,
396410
stderr=asyncio.subprocess.PIPE,
@@ -409,7 +423,7 @@ def test_empty_input(self):
409423
async def empty_input():
410424
code = 'import sys; data = sys.stdin.read(); print(len(data))'
411425
proc = await asyncio.create_subprocess_exec(
412-
sys.executable, '-c', code,
426+
get_quoted_sys_executable(), '-c', code,
413427
stdin=asyncio.subprocess.PIPE,
414428
stdout=asyncio.subprocess.PIPE,
415429
stderr=asyncio.subprocess.PIPE,
@@ -428,7 +442,7 @@ def test_devnull_input(self):
428442
async def empty_input():
429443
code = 'import sys; data = sys.stdin.read(); print(len(data))'
430444
proc = await asyncio.create_subprocess_exec(
431-
sys.executable, '-c', code,
445+
get_quoted_sys_executable(), '-c', code,
432446
stdin=asyncio.subprocess.DEVNULL,
433447
stdout=asyncio.subprocess.PIPE,
434448
stderr=asyncio.subprocess.PIPE,
@@ -447,7 +461,7 @@ def test_devnull_output(self):
447461
async def empty_output():
448462
code = 'import sys; data = sys.stdin.read(); print(len(data))'
449463
proc = await asyncio.create_subprocess_exec(
450-
sys.executable, '-c', code,
464+
get_quoted_sys_executable(), '-c', code,
451465
stdin=asyncio.subprocess.PIPE,
452466
stdout=asyncio.subprocess.DEVNULL,
453467
stderr=asyncio.subprocess.PIPE,
@@ -466,7 +480,7 @@ def test_devnull_error(self):
466480
async def empty_error():
467481
code = 'import sys; data = sys.stdin.read(); print(len(data))'
468482
proc = await asyncio.create_subprocess_exec(
469-
sys.executable, '-c', code,
483+
get_quoted_sys_executable(), '-c', code,
470484
stdin=asyncio.subprocess.PIPE,
471485
stdout=asyncio.subprocess.PIPE,
472486
stderr=asyncio.subprocess.DEVNULL,
@@ -487,7 +501,7 @@ def test_devstdin_input(self):
487501
async def devstdin_input(message):
488502
code = 'file = open("/dev/stdin"); data = file.read(); print(len(data))'
489503
proc = await asyncio.create_subprocess_exec(
490-
sys.executable, '-c', code,
504+
get_quoted_sys_executable(), '-c', code,
491505
stdin=asyncio.subprocess.PIPE,
492506
stdout=asyncio.subprocess.PIPE,
493507
stderr=asyncio.subprocess.PIPE,
@@ -643,7 +657,7 @@ async def _test_popen_error(self, stdin):
643657
with warnings.catch_warnings(record=True) as warns:
644658
with self.assertRaises(exc):
645659
await asyncio.create_subprocess_exec(
646-
sys.executable,
660+
get_quoted_sys_executable(),
647661
'-c',
648662
'pass',
649663
stdin=stdin
@@ -671,7 +685,7 @@ async def execute():
671685
'sys.exit(1)'])
672686

673687
process = await asyncio.create_subprocess_exec(
674-
sys.executable, '-c', code,
688+
get_quoted_sys_executable(), '-c', code,
675689
stdout=asyncio.subprocess.PIPE,
676690
)
677691

@@ -687,15 +701,15 @@ async def execute():
687701
def test_create_subprocess_exec_text_mode_fails(self):
688702
async def execute():
689703
with self.assertRaises(ValueError):
690-
await subprocess.create_subprocess_exec(sys.executable,
704+
await subprocess.create_subprocess_exec(get_quoted_sys_executable(),
691705
text=True)
692706

693707
with self.assertRaises(ValueError):
694-
await subprocess.create_subprocess_exec(sys.executable,
708+
await subprocess.create_subprocess_exec(get_quoted_sys_executable(),
695709
encoding="utf-8")
696710

697711
with self.assertRaises(ValueError):
698-
await subprocess.create_subprocess_exec(sys.executable,
712+
await subprocess.create_subprocess_exec(get_quoted_sys_executable(),
699713
errors="strict")
700714

701715
self.loop.run_until_complete(execute())
@@ -704,26 +718,26 @@ def test_create_subprocess_shell_text_mode_fails(self):
704718

705719
async def execute():
706720
with self.assertRaises(ValueError):
707-
await subprocess.create_subprocess_shell(sys.executable,
721+
await subprocess.create_subprocess_shell(get_quoted_sys_executable(),
708722
text=True)
709723

710724
with self.assertRaises(ValueError):
711-
await subprocess.create_subprocess_shell(sys.executable,
725+
await subprocess.create_subprocess_shell(get_quoted_sys_executable(),
712726
encoding="utf-8")
713727

714728
with self.assertRaises(ValueError):
715-
await subprocess.create_subprocess_shell(sys.executable,
729+
await subprocess.create_subprocess_shell(get_quoted_sys_executable(),
716730
errors="strict")
717731

718732
self.loop.run_until_complete(execute())
719733

720734
def test_create_subprocess_exec_with_path(self):
721735
async def execute():
722736
p = await subprocess.create_subprocess_exec(
723-
os_helper.FakePath(sys.executable), '-c', 'pass')
737+
os_helper.FakePath(get_quoted_sys_executable()), '-c', 'pass')
724738
await p.wait()
725739
p = await subprocess.create_subprocess_exec(
726-
sys.executable, '-c', 'pass', os_helper.FakePath('.'))
740+
get_quoted_sys_executable(), '-c', 'pass', os_helper.FakePath('.'))
727741
await p.wait()
728742

729743
self.assertIsNone(self.loop.run_until_complete(execute()))
@@ -739,7 +753,7 @@ async def check_stdout_output(self, coro, output):
739753

740754
def test_create_subprocess_env_shell(self) -> None:
741755
async def main() -> None:
742-
executable = sys.executable
756+
executable = get_quoted_sys_executable()
743757
if sys.platform == "win32":
744758
executable = f'"{executable}"'
745759
cmd = f'''{executable} -c "import os, sys; sys.stdout.write(os.getenv('FOO'))"'''
@@ -754,7 +768,7 @@ async def main() -> None:
754768

755769
def test_create_subprocess_env_exec(self) -> None:
756770
async def main() -> None:
757-
cmd = [sys.executable, "-c",
771+
cmd = [get_quoted_sys_executable(), "-c",
758772
"import os, sys; sys.stdout.write(os.getenv('FOO'))"]
759773
env = os.environ.copy()
760774
env["FOO"] = "baz"
@@ -825,7 +839,7 @@ async def main() -> None:
825839
exit_future = asyncio.Future()
826840
code = 'import sys; sys.stdout.write("stdout"); sys.stderr.write("stderr")'
827841
transport, _ = await loop.subprocess_exec(lambda: MyProtocol(exit_future),
828-
sys.executable, '-c', code, stdin=None)
842+
get_quoted_sys_executable(), '-c', code, stdin=None)
829843
await exit_future
830844
transport.close()
831845

@@ -858,7 +872,7 @@ async def get_command_stdout(cmd, *args):
858872

859873
async def main():
860874
outputs = [f'foo{i}' for i in range(10)]
861-
res = await asyncio.gather(*[get_command_stdout(sys.executable, '-c',
875+
res = await asyncio.gather(*[get_command_stdout(get_quoted_sys_executable(), '-c',
862876
f'print({out!r})') for out in outputs])
863877
self.assertEqual(res, outputs)
864878

0 commit comments

Comments
 (0)