Skip to content

Commit

Permalink
Fix subprocess argument building issue
Browse files Browse the repository at this point in the history
  • Loading branch information
gaogaotiantian committed Dec 1, 2023
1 parent 3ecd46a commit c62d5f3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/viztracer/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,17 @@ def build_command(args: Sequence[str]) -> list[str] | None:
py_args.append(f"-{cm_py_args}")
mode = [f"-{cm}"]
# -m mod | -mmod
script = cm_arg or next(args_iter, None)
mode.append(cm_arg or next(args_iter, None))
break

# -pyopts
py_args.append(arg)

if script is None:
return None
return [sys.executable, *py_args, "-m", "viztracer", "--quiet", *viz_args, *mode, script, *args_iter]
if script:
return [sys.executable, *py_args, "-m", "viztracer", "--quiet", *viz_args, "--", script, *args_iter]
elif mode and mode[-1] is not None:
return [sys.executable, *py_args, "-m", "viztracer", "--quiet", *viz_args, *mode, "--", *args_iter]
return None

@functools.wraps(subprocess.Popen.__init__)
def subprocess_init(self: subprocess.Popen[Any], args: Union[str, Sequence[Any], Any], **kwargs: Any) -> None:
Expand Down
2 changes: 2 additions & 0 deletions tests/test_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ def foo():
print(subprocess.check_output(["python", "-", "foo"], input=b"import sys; print(sys.argv)"))
print(subprocess.check_output(["python"], input=b"import sys; print(sys.argv)"))
print(subprocess.check_output(["python", "-im", "check_output_echo", "asdf"], input=b"import sys; print(sys.argv)"))
print(subprocess.check_output(["python", "check_output_echo.py", "test.py", "--output_dir", "test", "--other", "abc"]))
print(subprocess.check_output(["python", "-m", "check_output_echo", "test.py", "--output_dir", "test", "--other", "abc"]))
os.remove("check_output_echo.py")
"""
Expand Down

0 comments on commit c62d5f3

Please sign in to comment.