本文整理汇总了Python中subprocess.Popen.stderr方法的典型用法代码示例。如果您正苦于以下问题:Python Popen.stderr方法的具体用法?Python Popen.stderr怎么用?Python Popen.stderr使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类subprocess.Popen
的用法示例。
在下文中一共展示了Popen.stderr方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: nopen_keep_parent_stdin
# 需要导入模块: from subprocess import Popen [as 别名]
# 或者: from subprocess.Popen import stderr [as 别名]
def nopen_keep_parent_stdin(f, mode="r"):
if f.startswith("|"):
# using shell explicitly makes things like process substitution work:
# http://stackoverflow.com/questions/7407667/python-subprocess-subshells-and-redirection
# use sys.stderr so we dont have to worry about checking it...
p = Popen(f[1:], stdout=PIPE, stdin=sys.stdin,
stderr=sys.stderr if mode == "r" else PIPE,
shell=True, bufsize=-1, # use system default for buffering
preexec_fn=toolshed.files.prefunc,
close_fds=False, executable=os.environ.get('SHELL'))
if sys.version_info[0] > 2:
import io
p.stdout = io.TextIOWrapper(p.stdout)
p.stdin = io.TextIOWrapper(sys.stdin)
if mode != "r":
p.stderr = io.TextIOWrapper(p.stderr)
if mode and mode[0] == "r":
return toolshed.files.process_iter(p, f[1:])
return p
else:
return toolshed.files.nopen(f,mode)
示例2: nopen
# 需要导入模块: from subprocess import Popen [as 别名]
# 或者: from subprocess.Popen import stderr [as 别名]
def nopen(f, mode="r"):
r"""
open a file that's gzipped or return stdin for '-'
if f is a number, the result of nopen(sys.argv[f]) is returned.
>>> nopen('-') == sys.stdin, nopen('-', 'w') == sys.stdout
(True, True)
>>> nopen(sys.argv[0])
<...file...>
# expands user and vars ($HOME)
>>> nopen("~/.bashrc").name == nopen("$HOME/.bashrc").name
True
# an already open file.
>>> nopen(open(sys.argv[0]))
<...file...>
>>> nopen(0)
<...file...>
Or provide nicer access to Popen.stdout
>>> files = list(nopen("|ls"))
>>> assert 'setup.py\n' in files or b'setup.py\n' in files, files
"""
if isinstance(f, int_types):
return nopen(sys.argv[f], mode)
if not isinstance(f, basestring):
return f
if f.startswith("|"):
# using shell explicitly makes things like process substitution work:
# http://stackoverflow.com/questions/7407667/python-subprocess-subshells-and-redirection
# use sys.stderr so we dont have to worry about checking it...
p = Popen(f[1:], stdout=PIPE, stdin=PIPE,
stderr=sys.stderr if mode == "r" else PIPE,
shell=True, bufsize=-1, # use system default for buffering
close_fds=False, executable=os.environ.get('SHELL'))
if sys.version_info[0] > 2:
import io
p.stdout = io.TextIOWrapper(p.stdout)
p.stdin = io.TextIOWrapper(p.stdin)
if mode != "r":
p.stderr = io.TextIOWrapper(p.stderr)
if mode and mode[0] == "r":
return process_iter(p, f[1:])
return p
if f.startswith(("http://", "https://", "ftp://")):
fh = urlopen(f)
if f.endswith(".gz"):
return ungzipper(fh)
if sys.version_info[0] < 3:
return fh
import io
return io.TextIOWrapper(fh)
f = op.expanduser(op.expandvars(f))
if f.endswith((".gz", ".Z", ".z")):
fh = gzip.open(f, mode)
if sys.version_info[0] < 3:
return fh
import io
return io.TextIOWrapper(fh)
elif f.endswith((".bz", ".bz2", ".bzip2")):
fh = bz2.BZ2File(f, mode)
if sys.version_info[0] < 3:
return fh
import io
return io.TextIOWrapper(fh)
return {"r": sys.stdin, "w": sys.stdout}[mode[0]] if f == "-" \
else open(f, mode)
示例3: run_subproc
# 需要导入模块: from subprocess import Popen [as 别名]
# 或者: from subprocess.Popen import stderr [as 别名]
def run_subproc(cmds, captured=True):
"""Runs a subprocess, in its many forms. This takes a list of 'commands,'
which may be a list of command line arguments or a string, representing
a special connecting character. For example::
$ ls | grep wakka
is represented by the following cmds::
[['ls'], '|', ['grep', 'wakka']]
Lastly, the captured argument affects only the last real command.
"""
global ENV
last_stdout = PIPE if captured else None
background = False
if cmds[-1] == '&':
background = True
cmds = cmds[:-1]
write_target = None
if len(cmds) >= 3 and cmds[-2] in WRITER_MODES:
write_target = cmds[-1][0]
write_mode = WRITER_MODES[cmds[-2]]
cmds = cmds[:-2]
if write_target is not None:
try:
last_stdout = open(write_target, write_mode)
except FileNotFoundError:
e = 'xonsh: {0}: no such file or directory'
raise XonshError(e.format(write_target))
else:
last_stdout = PIPE
last_cmd = cmds[-1]
prev = None
procs = []
prev_proc = None
for cmd in cmds:
if isinstance(cmd, string_types):
prev = cmd
continue
stdout = last_stdout if cmd is last_cmd else PIPE
uninew = cmd is last_cmd
alias = builtins.aliases.get(cmd[0], None)
if _is_runnable_name(cmd[0]):
try:
aliased_cmd = get_script_subproc_command(cmd[0], cmd[1:])
except PermissionError:
e = 'xonsh: subprocess mode: permission denied: {0}'
raise XonshError(e.format(cmd[0]))
elif alias is None:
aliased_cmd = cmd
elif callable(alias):
aliased_cmd = alias
else:
aliased_cmd = alias + cmd[1:]
# compute stdin for subprocess
if prev_proc is None:
stdin = None
else:
stdin = prev_proc.stdout
if callable(aliased_cmd):
prev_is_proxy = True
numargs = len(inspect.signature(aliased_cmd).parameters)
if numargs == 2:
cls = SimpleProcProxy
elif numargs == 4:
cls = ProcProxy
else:
e = 'Expected callable with 2 or 4 arguments, not {}'
raise XonshError(e.format(numargs))
proc = cls(aliased_cmd, cmd[1:],
stdin, stdout, None,
universal_newlines=uninew)
else:
prev_is_proxy = False
subproc_kwargs = {}
if os.name == 'posix':
subproc_kwargs['preexec_fn'] = _subproc_pre
try:
proc = Popen(aliased_cmd,
universal_newlines=uninew,
env=ENV.detype(),
stdin=stdin,
stdout=PIPE,
stderr=PIPE, **subproc_kwargs)
proc_out, proc_err = proc.communicate()
sys.stdout.write(proc_out)
sys.stderr.write(proc_err)
proc.stdout = sys.stdout
proc.stderr = sys.stderr
except PermissionError:
e = 'xonsh: subprocess mode: permission denied: {0}'
raise XonshError(e.format(aliased_cmd[0]))
except FileNotFoundError:
cmd = aliased_cmd[0]
e = 'xonsh: subprocess mode: command not found: {0}'.format(cmd)
e += '\n' + suggest_commands(cmd, ENV, builtins.aliases)
raise XonshError(e)
procs.append(proc)
prev = None
#.........这里部分代码省略.........