mirror of
https://github.com/ARM-software/devlib.git
synced 2025-01-31 02:00:45 +00:00
utils/misc: Cleanup check_output()
* Remove check_output_lock as the issue has been fixed in Python 3.4 * Use Popen process as a context manager. Technically, Popen.communicate() already achieves those but the context manager will ensure this is done even if an exception happens at any point.
This commit is contained in:
parent
be734140b3
commit
678822f9e4
@ -151,22 +151,16 @@ def preexec_function():
|
||||
|
||||
|
||||
check_output_logger = logging.getLogger('check_output')
|
||||
# Popen is not thread safe. If two threads attempt to call it at the same time,
|
||||
# one may lock up. See https://bugs.python.org/issue12739.
|
||||
check_output_lock = threading.RLock()
|
||||
|
||||
|
||||
def get_subprocess(command, **kwargs):
|
||||
if 'stdout' in kwargs:
|
||||
raise ValueError('stdout argument not allowed, it will be overridden.')
|
||||
with check_output_lock:
|
||||
process = subprocess.Popen(command,
|
||||
return subprocess.Popen(command,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
stdin=subprocess.PIPE,
|
||||
preexec_fn=preexec_function,
|
||||
**kwargs)
|
||||
return process
|
||||
|
||||
|
||||
def check_subprocess_output(process, timeout=None, ignore=None, inputtext=None):
|
||||
@ -181,6 +175,7 @@ def check_subprocess_output(process, timeout=None, ignore=None, inputtext=None):
|
||||
message = 'Invalid value for ignore parameter: "{}"; must be an int or a list'
|
||||
raise ValueError(message.format(ignore))
|
||||
|
||||
with process:
|
||||
try:
|
||||
output, error = process.communicate(inputtext, timeout=timeout)
|
||||
except subprocess.TimeoutExpired as e:
|
||||
@ -195,7 +190,7 @@ def check_subprocess_output(process, timeout=None, ignore=None, inputtext=None):
|
||||
if timeout_expired:
|
||||
raise TimeoutError(process.args, output='\n'.join([output, error]))
|
||||
|
||||
retcode = process.poll()
|
||||
retcode = process.returncode
|
||||
if retcode and ignore != 'all' and retcode not in ignore:
|
||||
raise subprocess.CalledProcessError(retcode, process.args, output, error)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user