gh-98894: Quote test_dtrace tracer subcommands#152901
Conversation
Use shlex.join() when building tracer -c command strings for avoiding issues with spaces.
|
cc @vstinner . |
vstinner
left a comment
There was a problem hiding this comment.
Can you also modify f"Command {' '.join(command)!r} failed " to use shlex.join() in run_readelf() and trace()?
|
I tried to create a path with a space to test if the command/paths are properly quoted. I renamed the I applied this patch: diff --git a/Lib/test/test_dtrace.py b/Lib/test/test_dtrace.py
index 30731b8f90a..b890f467807 100644
--- a/Lib/test/test_dtrace.py
+++ b/Lib/test/test_dtrace.py
@@ -22,7 +22,7 @@
def abspath(filename):
- return os.path.abspath(findfile(filename, subdir="dtracedata"))
+ return os.path.abspath(findfile(filename, subdir="dtrace data"))
def normalize_trace_output(output):For example, this bpftrace program test fails:
I extracted the executed command and simplified it: So |
|
Same error if I try to create a Python executable path with a space in assert_usable(): $ git diff
diff --git a/Lib/test/test_dtrace.py b/Lib/test/test_dtrace.py
index 30731b8f90a..011507bd5c3 100644
--- a/Lib/test/test_dtrace.py
+++ b/Lib/test/test_dtrace.py
@@ -314,9 +314,15 @@ def assert_usable(self):
# Check if bpftrace is available and can attach to USDT probes
program = f'usdt:{sys.executable}:python:function__entry {{ printf("probe: success\\n"); exit(); }}'
try:
+ exe = sys.executable + ' .exe'
+ import shutil
+ shutil.copy2(sys.executable, exe)
+ cmd = shlex.join([exe, "-c", "pass"])
+ print("CMD", cmd)
+ cmd = ["bpftrace", "-e", program, "-c", cmd]
+ print("CMD", cmd)
proc = create_process_group(
- ["bpftrace", "-e", program, "-c",
- shlex.join([sys.executable, "-c", "pass"])],
+ cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,If I run manually bpftrace, it also fails: |
vstinner
left a comment
There was a problem hiding this comment.
LGTM. The AssertionError changes are useful. I'm less sure that the bpftrace are useful, but well, they look correct at least. It's just that bpftrace doesn't handle properly commands (-c argument) with quotes.
|
Thanks @stratakis for the PR, and @vstinner for merging it 🌮🎉.. I'm working now to backport this PR to: 3.15. |
|
GH-153459 is a backport of this pull request to the 3.15 branch. |
|
Merged. Thanks for the fix! |
Use shlex.join() when building tracer -c command strings for avoiding issues with spaces.