Skip to content

Always use a sequence in args to subprocess.Popen() #52

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: master
Choose a base branch
from
Open
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
8 changes: 6 additions & 2 deletions delegator.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,12 @@ def run(self, block=True, binary=False, cwd=None, env=None):
if cwd:
popen_kwargs["cwd"] = cwd
if env:
popen_kwargs["env"].update(env)
s = subprocess.Popen(self._popen_args, **popen_kwargs)
popen_kwargs['env'].update(env)
args = self._popen_args
if not isinstance(args, str):

Choose a reason for hiding this comment

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

Need to account for unicode string type in PY2.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If python2 is still relevant, are you fine with introducing six as a new dependency and use six.string_types?

# It is recommended to pass a sequence based on docs.

Choose a reason for hiding this comment

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

This comment, although correct, is confusing in this context since we're converting from sequence to string on the next line.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will remove it.

args = subprocess.list2cmdline(args)

Choose a reason for hiding this comment

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

subprocess.list2cmdline is used mostly (only?) on Windows, so using it here might have unexpected results on *nix systems.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

An alternative could be to use shlex here, but required routines are only Python3.3+?

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe it's better to set shell kwargs depending on what the given args are.
Using shlex and/or subprocess.list2cmdline doesn't work in all cases, I guess.

More over shell=True should only be used if the cmd line can't be represented with a sequence.

s = subprocess.Popen(args, **popen_kwargs)
# Otherwise, use pexpect.
else:
pexpect_kwargs = self._default_pexpect_kwargs.copy()
Expand Down