Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/smexe_ro.../usr/lib64/python3....
File: subprocess.py
# subprocess - Subprocesses with accessible I/O streams
[0] Fix | Delete
#
[1] Fix | Delete
# For more information about this module, see PEP 324.
[2] Fix | Delete
#
[3] Fix | Delete
# Copyright (c) 2003-2005 by Peter Astrand <astrand@lysator.liu.se>
[4] Fix | Delete
#
[5] Fix | Delete
# Licensed to PSF under a Contributor Agreement.
[6] Fix | Delete
# See http://www.python.org/2.4/license for licensing details.
[7] Fix | Delete
[8] Fix | Delete
r"""Subprocesses with accessible I/O streams
[9] Fix | Delete
[10] Fix | Delete
This module allows you to spawn processes, connect to their
[11] Fix | Delete
input/output/error pipes, and obtain their return codes.
[12] Fix | Delete
[13] Fix | Delete
For a complete description of this module see the Python documentation.
[14] Fix | Delete
[15] Fix | Delete
Main API
[16] Fix | Delete
========
[17] Fix | Delete
run(...): Runs a command, waits for it to complete, then returns a
[18] Fix | Delete
CompletedProcess instance.
[19] Fix | Delete
Popen(...): A class for flexibly executing a command in a new process
[20] Fix | Delete
[21] Fix | Delete
Constants
[22] Fix | Delete
---------
[23] Fix | Delete
DEVNULL: Special value that indicates that os.devnull should be used
[24] Fix | Delete
PIPE: Special value that indicates a pipe should be created
[25] Fix | Delete
STDOUT: Special value that indicates that stderr should go to stdout
[26] Fix | Delete
[27] Fix | Delete
[28] Fix | Delete
Older API
[29] Fix | Delete
=========
[30] Fix | Delete
call(...): Runs a command, waits for it to complete, then returns
[31] Fix | Delete
the return code.
[32] Fix | Delete
check_call(...): Same as call() but raises CalledProcessError()
[33] Fix | Delete
if return code is not 0
[34] Fix | Delete
check_output(...): Same as check_call() but returns the contents of
[35] Fix | Delete
stdout instead of a return code
[36] Fix | Delete
getoutput(...): Runs a command in the shell, waits for it to complete,
[37] Fix | Delete
then returns the output
[38] Fix | Delete
getstatusoutput(...): Runs a command in the shell, waits for it to complete,
[39] Fix | Delete
then returns a (exitcode, output) tuple
[40] Fix | Delete
"""
[41] Fix | Delete
[42] Fix | Delete
import builtins
[43] Fix | Delete
import errno
[44] Fix | Delete
import io
[45] Fix | Delete
import os
[46] Fix | Delete
import time
[47] Fix | Delete
import signal
[48] Fix | Delete
import sys
[49] Fix | Delete
import threading
[50] Fix | Delete
import warnings
[51] Fix | Delete
import contextlib
[52] Fix | Delete
from time import monotonic as _time
[53] Fix | Delete
[54] Fix | Delete
[55] Fix | Delete
__all__ = ["Popen", "PIPE", "STDOUT", "call", "check_call", "getstatusoutput",
[56] Fix | Delete
"getoutput", "check_output", "run", "CalledProcessError", "DEVNULL",
[57] Fix | Delete
"SubprocessError", "TimeoutExpired", "CompletedProcess"]
[58] Fix | Delete
# NOTE: We intentionally exclude list2cmdline as it is
[59] Fix | Delete
# considered an internal implementation detail. issue10838.
[60] Fix | Delete
[61] Fix | Delete
try:
[62] Fix | Delete
import msvcrt
[63] Fix | Delete
import _winapi
[64] Fix | Delete
_mswindows = True
[65] Fix | Delete
except ModuleNotFoundError:
[66] Fix | Delete
_mswindows = False
[67] Fix | Delete
import _posixsubprocess
[68] Fix | Delete
import select
[69] Fix | Delete
import selectors
[70] Fix | Delete
else:
[71] Fix | Delete
from _winapi import (CREATE_NEW_CONSOLE, CREATE_NEW_PROCESS_GROUP,
[72] Fix | Delete
STD_INPUT_HANDLE, STD_OUTPUT_HANDLE,
[73] Fix | Delete
STD_ERROR_HANDLE, SW_HIDE,
[74] Fix | Delete
STARTF_USESTDHANDLES, STARTF_USESHOWWINDOW,
[75] Fix | Delete
ABOVE_NORMAL_PRIORITY_CLASS, BELOW_NORMAL_PRIORITY_CLASS,
[76] Fix | Delete
HIGH_PRIORITY_CLASS, IDLE_PRIORITY_CLASS,
[77] Fix | Delete
NORMAL_PRIORITY_CLASS, REALTIME_PRIORITY_CLASS,
[78] Fix | Delete
CREATE_NO_WINDOW, DETACHED_PROCESS,
[79] Fix | Delete
CREATE_DEFAULT_ERROR_MODE, CREATE_BREAKAWAY_FROM_JOB)
[80] Fix | Delete
[81] Fix | Delete
__all__.extend(["CREATE_NEW_CONSOLE", "CREATE_NEW_PROCESS_GROUP",
[82] Fix | Delete
"STD_INPUT_HANDLE", "STD_OUTPUT_HANDLE",
[83] Fix | Delete
"STD_ERROR_HANDLE", "SW_HIDE",
[84] Fix | Delete
"STARTF_USESTDHANDLES", "STARTF_USESHOWWINDOW",
[85] Fix | Delete
"STARTUPINFO",
[86] Fix | Delete
"ABOVE_NORMAL_PRIORITY_CLASS", "BELOW_NORMAL_PRIORITY_CLASS",
[87] Fix | Delete
"HIGH_PRIORITY_CLASS", "IDLE_PRIORITY_CLASS",
[88] Fix | Delete
"NORMAL_PRIORITY_CLASS", "REALTIME_PRIORITY_CLASS",
[89] Fix | Delete
"CREATE_NO_WINDOW", "DETACHED_PROCESS",
[90] Fix | Delete
"CREATE_DEFAULT_ERROR_MODE", "CREATE_BREAKAWAY_FROM_JOB"])
[91] Fix | Delete
[92] Fix | Delete
[93] Fix | Delete
# Exception classes used by this module.
[94] Fix | Delete
class SubprocessError(Exception): pass
[95] Fix | Delete
[96] Fix | Delete
[97] Fix | Delete
class CalledProcessError(SubprocessError):
[98] Fix | Delete
"""Raised when run() is called with check=True and the process
[99] Fix | Delete
returns a non-zero exit status.
[100] Fix | Delete
[101] Fix | Delete
Attributes:
[102] Fix | Delete
cmd, returncode, stdout, stderr, output
[103] Fix | Delete
"""
[104] Fix | Delete
def __init__(self, returncode, cmd, output=None, stderr=None):
[105] Fix | Delete
self.returncode = returncode
[106] Fix | Delete
self.cmd = cmd
[107] Fix | Delete
self.output = output
[108] Fix | Delete
self.stderr = stderr
[109] Fix | Delete
[110] Fix | Delete
def __str__(self):
[111] Fix | Delete
if self.returncode and self.returncode < 0:
[112] Fix | Delete
try:
[113] Fix | Delete
return "Command '%s' died with %r." % (
[114] Fix | Delete
self.cmd, signal.Signals(-self.returncode))
[115] Fix | Delete
except ValueError:
[116] Fix | Delete
return "Command '%s' died with unknown signal %d." % (
[117] Fix | Delete
self.cmd, -self.returncode)
[118] Fix | Delete
else:
[119] Fix | Delete
return "Command '%s' returned non-zero exit status %d." % (
[120] Fix | Delete
self.cmd, self.returncode)
[121] Fix | Delete
[122] Fix | Delete
@property
[123] Fix | Delete
def stdout(self):
[124] Fix | Delete
"""Alias for output attribute, to match stderr"""
[125] Fix | Delete
return self.output
[126] Fix | Delete
[127] Fix | Delete
@stdout.setter
[128] Fix | Delete
def stdout(self, value):
[129] Fix | Delete
# There's no obvious reason to set this, but allow it anyway so
[130] Fix | Delete
# .stdout is a transparent alias for .output
[131] Fix | Delete
self.output = value
[132] Fix | Delete
[133] Fix | Delete
[134] Fix | Delete
class TimeoutExpired(SubprocessError):
[135] Fix | Delete
"""This exception is raised when the timeout expires while waiting for a
[136] Fix | Delete
child process.
[137] Fix | Delete
[138] Fix | Delete
Attributes:
[139] Fix | Delete
cmd, output, stdout, stderr, timeout
[140] Fix | Delete
"""
[141] Fix | Delete
def __init__(self, cmd, timeout, output=None, stderr=None):
[142] Fix | Delete
self.cmd = cmd
[143] Fix | Delete
self.timeout = timeout
[144] Fix | Delete
self.output = output
[145] Fix | Delete
self.stderr = stderr
[146] Fix | Delete
[147] Fix | Delete
def __str__(self):
[148] Fix | Delete
return ("Command '%s' timed out after %s seconds" %
[149] Fix | Delete
(self.cmd, self.timeout))
[150] Fix | Delete
[151] Fix | Delete
@property
[152] Fix | Delete
def stdout(self):
[153] Fix | Delete
return self.output
[154] Fix | Delete
[155] Fix | Delete
@stdout.setter
[156] Fix | Delete
def stdout(self, value):
[157] Fix | Delete
# There's no obvious reason to set this, but allow it anyway so
[158] Fix | Delete
# .stdout is a transparent alias for .output
[159] Fix | Delete
self.output = value
[160] Fix | Delete
[161] Fix | Delete
[162] Fix | Delete
if _mswindows:
[163] Fix | Delete
class STARTUPINFO:
[164] Fix | Delete
def __init__(self, *, dwFlags=0, hStdInput=None, hStdOutput=None,
[165] Fix | Delete
hStdError=None, wShowWindow=0, lpAttributeList=None):
[166] Fix | Delete
self.dwFlags = dwFlags
[167] Fix | Delete
self.hStdInput = hStdInput
[168] Fix | Delete
self.hStdOutput = hStdOutput
[169] Fix | Delete
self.hStdError = hStdError
[170] Fix | Delete
self.wShowWindow = wShowWindow
[171] Fix | Delete
self.lpAttributeList = lpAttributeList or {"handle_list": []}
[172] Fix | Delete
[173] Fix | Delete
def copy(self):
[174] Fix | Delete
attr_list = self.lpAttributeList.copy()
[175] Fix | Delete
if 'handle_list' in attr_list:
[176] Fix | Delete
attr_list['handle_list'] = list(attr_list['handle_list'])
[177] Fix | Delete
[178] Fix | Delete
return STARTUPINFO(dwFlags=self.dwFlags,
[179] Fix | Delete
hStdInput=self.hStdInput,
[180] Fix | Delete
hStdOutput=self.hStdOutput,
[181] Fix | Delete
hStdError=self.hStdError,
[182] Fix | Delete
wShowWindow=self.wShowWindow,
[183] Fix | Delete
lpAttributeList=attr_list)
[184] Fix | Delete
[185] Fix | Delete
[186] Fix | Delete
class Handle(int):
[187] Fix | Delete
closed = False
[188] Fix | Delete
[189] Fix | Delete
def Close(self, CloseHandle=_winapi.CloseHandle):
[190] Fix | Delete
if not self.closed:
[191] Fix | Delete
self.closed = True
[192] Fix | Delete
CloseHandle(self)
[193] Fix | Delete
[194] Fix | Delete
def Detach(self):
[195] Fix | Delete
if not self.closed:
[196] Fix | Delete
self.closed = True
[197] Fix | Delete
return int(self)
[198] Fix | Delete
raise ValueError("already closed")
[199] Fix | Delete
[200] Fix | Delete
def __repr__(self):
[201] Fix | Delete
return "%s(%d)" % (self.__class__.__name__, int(self))
[202] Fix | Delete
[203] Fix | Delete
__del__ = Close
[204] Fix | Delete
else:
[205] Fix | Delete
# When select or poll has indicated that the file is writable,
[206] Fix | Delete
# we can write up to _PIPE_BUF bytes without risk of blocking.
[207] Fix | Delete
# POSIX defines PIPE_BUF as >= 512.
[208] Fix | Delete
_PIPE_BUF = getattr(select, 'PIPE_BUF', 512)
[209] Fix | Delete
[210] Fix | Delete
# poll/select have the advantage of not requiring any extra file
[211] Fix | Delete
# descriptor, contrarily to epoll/kqueue (also, they require a single
[212] Fix | Delete
# syscall).
[213] Fix | Delete
if hasattr(selectors, 'PollSelector'):
[214] Fix | Delete
_PopenSelector = selectors.PollSelector
[215] Fix | Delete
else:
[216] Fix | Delete
_PopenSelector = selectors.SelectSelector
[217] Fix | Delete
[218] Fix | Delete
[219] Fix | Delete
if _mswindows:
[220] Fix | Delete
# On Windows we just need to close `Popen._handle` when we no longer need
[221] Fix | Delete
# it, so that the kernel can free it. `Popen._handle` gets closed
[222] Fix | Delete
# implicitly when the `Popen` instance is finalized (see `Handle.__del__`,
[223] Fix | Delete
# which is calling `CloseHandle` as requested in [1]), so there is nothing
[224] Fix | Delete
# for `_cleanup` to do.
[225] Fix | Delete
#
[226] Fix | Delete
# [1] https://docs.microsoft.com/en-us/windows/desktop/ProcThread/
[227] Fix | Delete
# creating-processes
[228] Fix | Delete
_active = None
[229] Fix | Delete
[230] Fix | Delete
def _cleanup():
[231] Fix | Delete
pass
[232] Fix | Delete
else:
[233] Fix | Delete
# This lists holds Popen instances for which the underlying process had not
[234] Fix | Delete
# exited at the time its __del__ method got called: those processes are
[235] Fix | Delete
# wait()ed for synchronously from _cleanup() when a new Popen object is
[236] Fix | Delete
# created, to avoid zombie processes.
[237] Fix | Delete
_active = []
[238] Fix | Delete
[239] Fix | Delete
def _cleanup():
[240] Fix | Delete
if _active is None:
[241] Fix | Delete
return
[242] Fix | Delete
for inst in _active[:]:
[243] Fix | Delete
res = inst._internal_poll(_deadstate=sys.maxsize)
[244] Fix | Delete
if res is not None:
[245] Fix | Delete
try:
[246] Fix | Delete
_active.remove(inst)
[247] Fix | Delete
except ValueError:
[248] Fix | Delete
# This can happen if two threads create a new Popen instance.
[249] Fix | Delete
# It's harmless that it was already removed, so ignore.
[250] Fix | Delete
pass
[251] Fix | Delete
[252] Fix | Delete
PIPE = -1
[253] Fix | Delete
STDOUT = -2
[254] Fix | Delete
DEVNULL = -3
[255] Fix | Delete
[256] Fix | Delete
[257] Fix | Delete
# XXX This function is only used by multiprocessing and the test suite,
[258] Fix | Delete
# but it's here so that it can be imported when Python is compiled without
[259] Fix | Delete
# threads.
[260] Fix | Delete
[261] Fix | Delete
def _optim_args_from_interpreter_flags():
[262] Fix | Delete
"""Return a list of command-line arguments reproducing the current
[263] Fix | Delete
optimization settings in sys.flags."""
[264] Fix | Delete
args = []
[265] Fix | Delete
value = sys.flags.optimize
[266] Fix | Delete
if value > 0:
[267] Fix | Delete
args.append('-' + 'O' * value)
[268] Fix | Delete
return args
[269] Fix | Delete
[270] Fix | Delete
[271] Fix | Delete
def _args_from_interpreter_flags():
[272] Fix | Delete
"""Return a list of command-line arguments reproducing the current
[273] Fix | Delete
settings in sys.flags, sys.warnoptions and sys._xoptions."""
[274] Fix | Delete
flag_opt_map = {
[275] Fix | Delete
'debug': 'd',
[276] Fix | Delete
# 'inspect': 'i',
[277] Fix | Delete
# 'interactive': 'i',
[278] Fix | Delete
'dont_write_bytecode': 'B',
[279] Fix | Delete
'no_site': 'S',
[280] Fix | Delete
'verbose': 'v',
[281] Fix | Delete
'bytes_warning': 'b',
[282] Fix | Delete
'quiet': 'q',
[283] Fix | Delete
# -O is handled in _optim_args_from_interpreter_flags()
[284] Fix | Delete
}
[285] Fix | Delete
args = _optim_args_from_interpreter_flags()
[286] Fix | Delete
for flag, opt in flag_opt_map.items():
[287] Fix | Delete
v = getattr(sys.flags, flag)
[288] Fix | Delete
if v > 0:
[289] Fix | Delete
args.append('-' + opt * v)
[290] Fix | Delete
[291] Fix | Delete
if sys.flags.isolated:
[292] Fix | Delete
args.append('-I')
[293] Fix | Delete
else:
[294] Fix | Delete
if sys.flags.ignore_environment:
[295] Fix | Delete
args.append('-E')
[296] Fix | Delete
if sys.flags.no_user_site:
[297] Fix | Delete
args.append('-s')
[298] Fix | Delete
[299] Fix | Delete
# -W options
[300] Fix | Delete
warnopts = sys.warnoptions[:]
[301] Fix | Delete
bytes_warning = sys.flags.bytes_warning
[302] Fix | Delete
xoptions = getattr(sys, '_xoptions', {})
[303] Fix | Delete
dev_mode = ('dev' in xoptions)
[304] Fix | Delete
[305] Fix | Delete
if bytes_warning > 1:
[306] Fix | Delete
warnopts.remove("error::BytesWarning")
[307] Fix | Delete
elif bytes_warning:
[308] Fix | Delete
warnopts.remove("default::BytesWarning")
[309] Fix | Delete
if dev_mode:
[310] Fix | Delete
warnopts.remove('default')
[311] Fix | Delete
for opt in warnopts:
[312] Fix | Delete
args.append('-W' + opt)
[313] Fix | Delete
[314] Fix | Delete
# -X options
[315] Fix | Delete
if dev_mode:
[316] Fix | Delete
args.extend(('-X', 'dev'))
[317] Fix | Delete
for opt in ('faulthandler', 'tracemalloc', 'importtime',
[318] Fix | Delete
'showalloccount', 'showrefcount', 'utf8'):
[319] Fix | Delete
if opt in xoptions:
[320] Fix | Delete
value = xoptions[opt]
[321] Fix | Delete
if value is True:
[322] Fix | Delete
arg = opt
[323] Fix | Delete
else:
[324] Fix | Delete
arg = '%s=%s' % (opt, value)
[325] Fix | Delete
args.extend(('-X', arg))
[326] Fix | Delete
[327] Fix | Delete
return args
[328] Fix | Delete
[329] Fix | Delete
[330] Fix | Delete
def call(*popenargs, timeout=None, **kwargs):
[331] Fix | Delete
"""Run command with arguments. Wait for command to complete or
[332] Fix | Delete
timeout, then return the returncode attribute.
[333] Fix | Delete
[334] Fix | Delete
The arguments are the same as for the Popen constructor. Example:
[335] Fix | Delete
[336] Fix | Delete
retcode = call(["ls", "-l"])
[337] Fix | Delete
"""
[338] Fix | Delete
with Popen(*popenargs, **kwargs) as p:
[339] Fix | Delete
try:
[340] Fix | Delete
return p.wait(timeout=timeout)
[341] Fix | Delete
except: # Including KeyboardInterrupt, wait handled that.
[342] Fix | Delete
p.kill()
[343] Fix | Delete
# We don't call p.wait() again as p.__exit__ does that for us.
[344] Fix | Delete
raise
[345] Fix | Delete
[346] Fix | Delete
[347] Fix | Delete
def check_call(*popenargs, **kwargs):
[348] Fix | Delete
"""Run command with arguments. Wait for command to complete. If
[349] Fix | Delete
the exit code was zero then return, otherwise raise
[350] Fix | Delete
CalledProcessError. The CalledProcessError object will have the
[351] Fix | Delete
return code in the returncode attribute.
[352] Fix | Delete
[353] Fix | Delete
The arguments are the same as for the call function. Example:
[354] Fix | Delete
[355] Fix | Delete
check_call(["ls", "-l"])
[356] Fix | Delete
"""
[357] Fix | Delete
retcode = call(*popenargs, **kwargs)
[358] Fix | Delete
if retcode:
[359] Fix | Delete
cmd = kwargs.get("args")
[360] Fix | Delete
if cmd is None:
[361] Fix | Delete
cmd = popenargs[0]
[362] Fix | Delete
raise CalledProcessError(retcode, cmd)
[363] Fix | Delete
return 0
[364] Fix | Delete
[365] Fix | Delete
[366] Fix | Delete
def check_output(*popenargs, timeout=None, **kwargs):
[367] Fix | Delete
r"""Run command with arguments and return its output.
[368] Fix | Delete
[369] Fix | Delete
If the exit code was non-zero it raises a CalledProcessError. The
[370] Fix | Delete
CalledProcessError object will have the return code in the returncode
[371] Fix | Delete
attribute and output in the output attribute.
[372] Fix | Delete
[373] Fix | Delete
The arguments are the same as for the Popen constructor. Example:
[374] Fix | Delete
[375] Fix | Delete
>>> check_output(["ls", "-l", "/dev/null"])
[376] Fix | Delete
b'crw-rw-rw- 1 root root 1, 3 Oct 18 2007 /dev/null\n'
[377] Fix | Delete
[378] Fix | Delete
The stdout argument is not allowed as it is used internally.
[379] Fix | Delete
To capture standard error in the result, use stderr=STDOUT.
[380] Fix | Delete
[381] Fix | Delete
>>> check_output(["/bin/sh", "-c",
[382] Fix | Delete
... "ls -l non_existent_file ; exit 0"],
[383] Fix | Delete
... stderr=STDOUT)
[384] Fix | Delete
b'ls: non_existent_file: No such file or directory\n'
[385] Fix | Delete
[386] Fix | Delete
There is an additional optional argument, "input", allowing you to
[387] Fix | Delete
pass a string to the subprocess's stdin. If you use this argument
[388] Fix | Delete
you may not also use the Popen constructor's "stdin" argument, as
[389] Fix | Delete
it too will be used internally. Example:
[390] Fix | Delete
[391] Fix | Delete
>>> check_output(["sed", "-e", "s/foo/bar/"],
[392] Fix | Delete
... input=b"when in the course of fooman events\n")
[393] Fix | Delete
b'when in the course of barman events\n'
[394] Fix | Delete
[395] Fix | Delete
By default, all communication is in bytes, and therefore any "input"
[396] Fix | Delete
should be bytes, and the return value will be bytes. If in text mode,
[397] Fix | Delete
any "input" should be a string, and the return value will be a string
[398] Fix | Delete
decoded according to locale encoding, or by "encoding" if set. Text mode
[399] Fix | Delete
is triggered by setting any of text, encoding, errors or universal_newlines.
[400] Fix | Delete
"""
[401] Fix | Delete
if 'stdout' in kwargs:
[402] Fix | Delete
raise ValueError('stdout argument not allowed, it will be overridden.')
[403] Fix | Delete
[404] Fix | Delete
if 'input' in kwargs and kwargs['input'] is None:
[405] Fix | Delete
# Explicitly passing input=None was previously equivalent to passing an
[406] Fix | Delete
# empty string. That is maintained here for backwards compatibility.
[407] Fix | Delete
if kwargs.get('universal_newlines') or kwargs.get('text'):
[408] Fix | Delete
empty = ''
[409] Fix | Delete
else:
[410] Fix | Delete
empty = b''
[411] Fix | Delete
kwargs['input'] = empty
[412] Fix | Delete
[413] Fix | Delete
return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
[414] Fix | Delete
**kwargs).stdout
[415] Fix | Delete
[416] Fix | Delete
[417] Fix | Delete
class CompletedProcess(object):
[418] Fix | Delete
"""A process that has finished running.
[419] Fix | Delete
[420] Fix | Delete
This is returned by run().
[421] Fix | Delete
[422] Fix | Delete
Attributes:
[423] Fix | Delete
args: The list or str args passed to run().
[424] Fix | Delete
returncode: The exit code of the process, negative for signals.
[425] Fix | Delete
stdout: The standard output (None if not captured).
[426] Fix | Delete
stderr: The standard error (None if not captured).
[427] Fix | Delete
"""
[428] Fix | Delete
def __init__(self, args, returncode, stdout=None, stderr=None):
[429] Fix | Delete
self.args = args
[430] Fix | Delete
self.returncode = returncode
[431] Fix | Delete
self.stdout = stdout
[432] Fix | Delete
self.stderr = stderr
[433] Fix | Delete
[434] Fix | Delete
def __repr__(self):
[435] Fix | Delete
args = ['args={!r}'.format(self.args),
[436] Fix | Delete
'returncode={!r}'.format(self.returncode)]
[437] Fix | Delete
if self.stdout is not None:
[438] Fix | Delete
args.append('stdout={!r}'.format(self.stdout))
[439] Fix | Delete
if self.stderr is not None:
[440] Fix | Delete
args.append('stderr={!r}'.format(self.stderr))
[441] Fix | Delete
return "{}({})".format(type(self).__name__, ', '.join(args))
[442] Fix | Delete
[443] Fix | Delete
def check_returncode(self):
[444] Fix | Delete
"""Raise CalledProcessError if the exit code is non-zero."""
[445] Fix | Delete
if self.returncode:
[446] Fix | Delete
raise CalledProcessError(self.returncode, self.args, self.stdout,
[447] Fix | Delete
self.stderr)
[448] Fix | Delete
[449] Fix | Delete
[450] Fix | Delete
def run(*popenargs,
[451] Fix | Delete
input=None, capture_output=False, timeout=None, check=False, **kwargs):
[452] Fix | Delete
"""Run command with arguments and return a CompletedProcess instance.
[453] Fix | Delete
[454] Fix | Delete
The returned instance will have attributes args, returncode, stdout and
[455] Fix | Delete
stderr. By default, stdout and stderr are not captured, and those attributes
[456] Fix | Delete
will be None. Pass stdout=PIPE and/or stderr=PIPE in order to capture them.
[457] Fix | Delete
[458] Fix | Delete
If check is True and the exit code was non-zero, it raises a
[459] Fix | Delete
CalledProcessError. The CalledProcessError object will have the return code
[460] Fix | Delete
in the returncode attribute, and output & stderr attributes if those streams
[461] Fix | Delete
were captured.
[462] Fix | Delete
[463] Fix | Delete
If timeout is given, and the process takes too long, a TimeoutExpired
[464] Fix | Delete
exception will be raised.
[465] Fix | Delete
[466] Fix | Delete
There is an optional argument "input", allowing you to
[467] Fix | Delete
pass bytes or a string to the subprocess's stdin. If you use this argument
[468] Fix | Delete
you may not also use the Popen constructor's "stdin" argument, as
[469] Fix | Delete
it will be used internally.
[470] Fix | Delete
[471] Fix | Delete
By default, all communication is in bytes, and therefore any "input" should
[472] Fix | Delete
be bytes, and the stdout and stderr will be bytes. If in text mode, any
[473] Fix | Delete
"input" should be a string, and stdout and stderr will be strings decoded
[474] Fix | Delete
according to locale encoding, or by "encoding" if set. Text mode is
[475] Fix | Delete
triggered by setting any of text, encoding, errors or universal_newlines.
[476] Fix | Delete
[477] Fix | Delete
The other arguments are the same as for the Popen constructor.
[478] Fix | Delete
"""
[479] Fix | Delete
if input is not None:
[480] Fix | Delete
if kwargs.get('stdin') is not None:
[481] Fix | Delete
raise ValueError('stdin and input arguments may not both be used.')
[482] Fix | Delete
kwargs['stdin'] = PIPE
[483] Fix | Delete
[484] Fix | Delete
if capture_output:
[485] Fix | Delete
if kwargs.get('stdout') is not None or kwargs.get('stderr') is not None:
[486] Fix | Delete
raise ValueError('stdout and stderr arguments may not be used '
[487] Fix | Delete
'with capture_output.')
[488] Fix | Delete
kwargs['stdout'] = PIPE
[489] Fix | Delete
kwargs['stderr'] = PIPE
[490] Fix | Delete
[491] Fix | Delete
with Popen(*popenargs, **kwargs) as process:
[492] Fix | Delete
try:
[493] Fix | Delete
stdout, stderr = process.communicate(input, timeout=timeout)
[494] Fix | Delete
except TimeoutExpired as exc:
[495] Fix | Delete
process.kill()
[496] Fix | Delete
if _mswindows:
[497] Fix | Delete
# Windows accumulates the output in a single blocking
[498] Fix | Delete
# read() call run on child threads, with the timeout
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function