Skip to content

gh-98894: Quote test_dtrace tracer subcommands#152901

Merged
vstinner merged 2 commits into
python:mainfrom
stratakis:shlex_dtrace
Jul 9, 2026
Merged

gh-98894: Quote test_dtrace tracer subcommands#152901
vstinner merged 2 commits into
python:mainfrom
stratakis:shlex_dtrace

Conversation

@stratakis

@stratakis stratakis commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Use shlex.join() when building tracer -c command strings for avoiding issues with spaces.

@bedevere-app bedevere-app Bot added tests Tests in the Lib/test dir awaiting review labels Jul 2, 2026
Use shlex.join() when building tracer -c command strings
for avoiding issues with spaces.
@stratakis

Copy link
Copy Markdown
Contributor Author

cc @vstinner .

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also modify f"Command {' '.join(command)!r} failed " to use shlex.join() in run_readelf() and trace()?

@vstinner vstinner added skip news needs backport to 3.15 pre-release feature fixes, bugs and security fixes labels Jul 3, 2026
@vstinner

vstinner commented Jul 9, 2026

Copy link
Copy Markdown
Member

I tried to create a path with a space to test if the command/paths are properly quoted.

I renamed the dtracedata/ directory to dtrace data/: mv Lib/test/dtracedata/ "Lib/test/dtrace data/".

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:

sudo ./python -m test -v test_dtrace -m test.test_dtrace.BPFTraceOptimizedTests.test_gc

I extracted the executed command and simplified it:

$ sudo bpftrace -e 'usdt:/home/vstinner/python/main/python:python:function__entry {}' -c "/home/vstinner/python/main/python -O -O '/home/vstinner/python/main/Lib/test/dtrace data/gc.py'"
Attached 1 probe
/home/vstinner/python/main/python: can't open file "/home/vstinner/python/main/'/home/vstinner/python/main/Lib/test/dtrace": [Errno 2] No such file or directory

So python fails to find its filename parameter.

@vstinner

vstinner commented Jul 9, 2026

Copy link
Copy Markdown
Member

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:

$ sudo bpftrace -e 'usdt:/home/vstinner/python/main/python:python:function__entry { printf("probe: success\\n"); exit(); }' '-c' "'/home/vstinner/python/main/python .exe' -c pass"
ERROR: Failed to fork child: path ''/home/vstinner/python/main/python' does not exist or is not executable

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread Lib/test/test_dtrace.py
@vstinner vstinner merged commit a159c68 into python:main Jul 9, 2026
52 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @stratakis for the PR, and @vstinner for merging it 🌮🎉.. I'm working now to backport this PR to: 3.15.
🐍🍒⛏🤖

@bedevere-app

bedevere-app Bot commented Jul 9, 2026

Copy link
Copy Markdown

GH-153459 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Jul 9, 2026
@vstinner

vstinner commented Jul 9, 2026

Copy link
Copy Markdown
Member

Merged. Thanks for the fix!

vstinner pushed a commit that referenced this pull request Jul 9, 2026
…53459)

gh-98894: Quote test_dtrace tracer subcommands (GH-152901)

Use shlex.join() when building tracer -c command strings
for avoiding issues with spaces.
(cherry picked from commit a159c68)

Co-authored-by: stratakis <cstratak@redhat.com>
@stratakis stratakis deleted the shlex_dtrace branch July 9, 2026 22:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip news tests Tests in the Lib/test dir

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants