Skip to content

Commit a159c68

Browse files
authored
pythongh-98894: Quote test_dtrace tracer subcommands (python#152901)
Use shlex.join() when building tracer -c command strings for avoiding issues with spaces.
1 parent 3114449 commit a159c68

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

Lib/test/test_dtrace.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import dis
22
import os.path
33
import re
4+
import shlex
45
import signal
56
import subprocess
67
import sys
@@ -92,7 +93,7 @@ def run_readelf(cmd):
9293

9394
if proc.returncode:
9495
raise AssertionError(
95-
f"Command {' '.join(cmd)!r} failed "
96+
f"Command {shlex.join(cmd)!r} failed "
9697
f"with exit code {proc.returncode}: "
9798
f"stdout={stdout!r} stderr={stderr!r}"
9899
)
@@ -139,7 +140,7 @@ def trace(self, script_file, subcommand=None, *, timeout=None,
139140
raise
140141
if check_returncode and proc.returncode:
141142
raise AssertionError(
142-
f"Command {' '.join(command)!r} failed "
143+
f"Command {shlex.join(command)!r} failed "
143144
f"with exit code {proc.returncode}: output={stdout!r}"
144145
)
145146
return stdout
@@ -148,7 +149,7 @@ def trace_python(self, script_file, python_file, optimize_python=None):
148149
python_flags = []
149150
if optimize_python:
150151
python_flags.extend(["-O"] * optimize_python)
151-
subcommand = " ".join([sys.executable] + python_flags + [python_file])
152+
subcommand = shlex.join([sys.executable] + python_flags + [python_file])
152153
return self.trace(script_file, subcommand, timeout=60,
153154
check_returncode=True)
154155

@@ -276,7 +277,7 @@ def run_case(self, name, optimize_python=None):
276277

277278
try:
278279
proc = create_process_group(
279-
["bpftrace", "-e", program, "-c", " ".join(subcommand)],
280+
["bpftrace", "-e", program, "-c", shlex.join(subcommand)],
280281
stdout=subprocess.PIPE,
281282
stderr=subprocess.PIPE,
282283
universal_newlines=True,
@@ -314,7 +315,8 @@ def assert_usable(self):
314315
program = f'usdt:{sys.executable}:python:function__entry {{ printf("probe: success\\n"); exit(); }}'
315316
try:
316317
proc = create_process_group(
317-
["bpftrace", "-e", program, "-c", f"{sys.executable} -c pass"],
318+
["bpftrace", "-e", program, "-c",
319+
shlex.join([sys.executable, "-c", "pass"])],
318320
stdout=subprocess.PIPE,
319321
stderr=subprocess.PIPE,
320322
universal_newlines=True,

0 commit comments

Comments
 (0)