Nick Coghlan <[email protected]> added the comment:
After working on the documentation updates to the subprocess module to
emphasise the convenience functions, I've now changed my mind and think it
makes sense to keep these additions in that module.
Now that the shell helpers will default to using shlex.quote() and require the
"!u" conversion specifier to bypass that behaviour, and also will have distinct
names from the ordinary non-shell based versions, my original concerns that
lead to me wanting to keep them out of subprocess no longer apply. That means
the general preference that subprocess be our "one stop shop" for explicit
subprocess invocation can once again take precedence.
I'm actually planning to change the API a bit though, dropping "shell_format"
and "shell_format_map" in favour of the following helper class:
class ShellCommand:
def __init__(self, command, *, **callkwds):
self.command = command
self.callkwds = callkwds
def format(self, *args, **kwds):
return _ShellFormatter().vformat(_fmt, args, kwds)
def format_map(self, mapping):
return _ShellFormatter().vformat(_fmt, (), mapping)
def call(self, *args, **kwds):
if args or kwds:
cmd = _ShellFormatter().vformat(cmd, args, kwds)
return subprocess.call(cmd, shell=True, **self.callkwds)
def check_call(self, *args, **kwds):
if args or kwds:
cmd = _ShellFormatter().vformat(cmd, args, kwds)
return subprocess.check_call(cmd, shell=True, **self.callkwds)
def shell_output(cmd, *args, **kwds):
if args or kwds:
cmd = _ShellFormatter().vformat(cmd, args, kwds)
data = subprocess.check_output(cmd, shell=True, universal_newlines=True)
if data[-1:] == "\n":
data = data[:-1]
return data
The module level helpers would then just become re module style wrappers:
def shell_call(command, *args, **kwds):
return ShellCommand(command).shell_call(*args, **kwds)
def check_shell_call(command, *args, **kwds):
return ShellCommand(command).check_shell_call(*args, **kwds)
def shell_output(command, *args, **kwds):
return ShellCommand(command).shell_output(*args, **kwds)
----------
title: Add shell command helpers to shutil module -> Add shell command helpers
to subprocess module
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue13238>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com