Skip to content

Use shell=False for Popen on Windows #1324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 1 addition & 21 deletions pytensor/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,34 +137,14 @@ def subprocess_Popen(command: str | list[str], **params):
except AttributeError:
startupinfo.dwFlags |= subprocess._subprocess.STARTF_USESHOWWINDOW # type: ignore[attr-defined]

# Anaconda for Windows does not always provide .exe files
# in the PATH, they also have .bat files that call the corresponding
# executable. For instance, "g++.bat" is in the PATH, not "g++.exe"
# Unless "shell=True", "g++.bat" is not executed when trying to
# execute "g++" without extensions.
# (Executing "g++.bat" explicitly would also work.)
params["shell"] = True
# "If shell is True, it is recommended to pass args as a string rather than as a sequence." (cite taken from https://docs.python.org/2/library/subprocess.html#frequently-used-arguments)
# In case when command arguments have spaces, passing a command as a list will result in incorrect arguments break down, and consequently
# in "The filename, directory name, or volume label syntax is incorrect" error message.
# Passing the command as a single string solves this problem.
if isinstance(command, list):
command = " ".join(command)

# Using the dummy file descriptors below is a workaround for a
# crash experienced in an unusual Python 2.4.4 Windows environment
# with the default None values.
stdin = None
if "stdin" not in params:
stdin = Path(os.devnull).open()
params["stdin"] = stdin.fileno()

try:
proc = subprocess.Popen(command, startupinfo=startupinfo, **params)
finally:
if stdin is not None:
stdin.close()
return proc
return subprocess.Popen(command, startupinfo=startupinfo, **params)


def call_subprocess_Popen(command, **params):
Expand Down
4 changes: 1 addition & 3 deletions tests/compile/function/test_function.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import pickle
import re
import shutil
import tempfile
from pathlib import Path
Expand Down Expand Up @@ -50,8 +49,7 @@ def test_function_name():
x = vector("x")
func = function([x], x + 1.0)

regex = re.compile(f".*{__file__}c?")
assert regex.match(func.name) is not None
assert __file__ in func.name


def test_trust_input():
Expand Down