Coding style.

Using the timeout option in subprocess rather than the timeout command.
This commit is contained in:
Andrew Hamilton 2016-02-01 22:12:14 +00:00
parent b05d4a5b7f
commit 66ac05c00b

View file

@ -80,7 +80,7 @@ def get_ls_color_codes():
LS_COLOR_CODES = get_ls_color_codes() LS_COLOR_CODES = get_ls_color_codes()
TIMEOUT = "60" TIMEOUT = 60
def fix_input(input_): def fix_input(input_):
@ -88,12 +88,16 @@ def fix_input(input_):
return input_str.replace("\t", " " * 4) return input_str.replace("\t", " " * 4)
def _do_command(command, **kwargs): def _do_command(command, timeout=None, **kwargs):
stdout, stderr = "", "" stdout, stderr = "", ""
with contextlib.suppress(subprocess.CalledProcessError): with contextlib.suppress(subprocess.CalledProcessError):
process = subprocess.Popen(command, stdout=subprocess.PIPE, process = subprocess.Popen(command, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, **kwargs) stderr=subprocess.PIPE, **kwargs)
stdout, stderr = process.communicate() try:
stdout, stderr = process.communicate(timeout=timeout)
except subprocess.TimeoutExpired:
process.kill()
raise
return fix_input(stdout), fix_input(stderr), process.returncode return fix_input(stdout), fix_input(stderr), process.returncode
@ -266,7 +270,7 @@ def python_unittests(path):
if str(path).endswith("_test.py"): if str(path).endswith("_test.py"):
python_version = _python_version(path) python_version = _python_version(path)
cmd = [path] if _has_shebang_line(path) else [python_version, path] cmd = [path] if _has_shebang_line(path) else [python_version, path]
stdout, stderr, returncode = _do_command(["timeout", TIMEOUT] + cmd) stdout, stderr, returncode = _do_command(cmd, timeout=TIMEOUT)
markup = pygments.lex(stderr, _python_console_lexer) markup = pygments.lex(stderr, _python_console_lexer)
status = Status.ok if returncode == 0 else Status.problem status = Status.ok if returncode == 0 else Status.problem
native_style = pygments.styles.get_style_by_name("native") native_style = pygments.styles.get_style_by_name("native")
@ -281,8 +285,7 @@ def pydoc(path):
pydoc_exe = "pydoc3" if _python_version(path) == "python3" else "pydoc" pydoc_exe = "pydoc3" if _python_version(path) == "python3" else "pydoc"
status, output = Status.normal, "" status, output = Status.normal, ""
try: try:
output = subprocess.check_output( output = subprocess.check_output([pydoc_exe, path], timeout=TIMEOUT)
["timeout", TIMEOUT, pydoc_exe, path])
output = fix_input(output) output = fix_input(output)
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
status = Status.not_applicable status = Status.not_applicable
@ -308,7 +311,7 @@ def python_coverage(path):
env = os.environ.copy() env = os.environ.copy()
env["COVERAGE_FILE"] = coverage_path env["COVERAGE_FILE"] = coverage_path
stdout, *rest = _do_command( stdout, *rest = _do_command(
["timeout", TIMEOUT, python_exe, "run", test_path], env=env) [python_exe, "run", test_path], env=env, timeout=TIMEOUT)
stdout, *rest = _do_command( stdout, *rest = _do_command(
[python_exe, "annotate", "--directory", temp_dir, [python_exe, "annotate", "--directory", temp_dir,
os.path.normpath(path)], env=env) os.path.normpath(path)], env=env)
@ -322,9 +325,8 @@ python_coverage.dependencies = {"python-coverage", "python3-coverage"}
def python_profile(path): def python_profile(path):
stdout, *rest = _do_command(["timeout", TIMEOUT, _python_version(path), stdout, *rest = _do_command([_python_version(path), "-m", "cProfile",
"-m", "cProfile", "--sort=cumulative", "--sort=cumulative", path], timeout=TIMEOUT)
path])
return Status.normal, fill3.Text(stdout) return Status.normal, fill3.Text(stdout)
python_profile.dependencies = {"python", "python3"} python_profile.dependencies = {"python", "python3"}
@ -647,6 +649,8 @@ def _get_python_console_lexer():
def run_tool_no_error(path, tool): def run_tool_no_error(path, tool):
try: try:
status, result = tool(path) status, result = tool(path)
except subprocess.TimeoutExpired:
status, result = Status.error, fill3.Text("Timed out")
except: except:
# Maybe use code.InteractiveInterpreter.showtraceback() ? # Maybe use code.InteractiveInterpreter.showtraceback() ?
tokens = pygments.lex(traceback.format_exc(), tokens = pygments.lex(traceback.format_exc(),