diff options
Diffstat (limited to 'tests/backtest.py')
-rw-r--r-- | tests/backtest.py | 50 |
1 files changed, 21 insertions, 29 deletions
diff --git a/tests/backtest.py b/tests/backtest.py index 89a25536..439493d1 100644 --- a/tests/backtest.py +++ b/tests/backtest.py @@ -4,41 +4,31 @@ # (Redefining built-in blah) # The whole point of this file is to redefine built-ins, so shut up about it. -import os +import subprocess -# Py2 and Py3 don't agree on how to run commands in a subprocess. -try: - import subprocess -except ImportError: - def run_command(cmd, status=0): - """Run a command in a subprocess. - - Returns the exit status code and the combined stdout and stderr. - """ - _, stdouterr = os.popen4(cmd) - return status, stdouterr.read() +# This isn't really a backward compatibility thing, should be moved into a +# helpers file or something. +def run_command(cmd): + """Run a command in a subprocess. -else: - def run_command(cmd, status=0): - """Run a command in a subprocess. + Returns the exit status code and the combined stdout and stderr. - Returns the exit status code and the combined stdout and stderr. + """ + proc = subprocess.Popen(cmd, shell=True, + stdin=subprocess.PIPE, stdout=subprocess.PIPE, + stderr=subprocess.STDOUT + ) + output, _ = proc.communicate() + status = proc.returncode # pylint: disable=E1101 - """ - proc = subprocess.Popen(cmd, shell=True, - stdin=subprocess.PIPE, stdout=subprocess.PIPE, - stderr=subprocess.STDOUT - ) - output, _ = proc.communicate() - status = proc.returncode # pylint: disable=E1101 + # Get the output, and canonicalize it to strings with newlines. + if not isinstance(output, str): + output = output.decode('utf-8') + output = output.replace('\r', '') - # Get the output, and canonicalize it to strings with newlines. - if not isinstance(output, str): - output = output.decode('utf-8') - output = output.replace('\r', '') + return status, output - return status, output # No more execfile in Py3 try: @@ -46,4 +36,6 @@ try: except NameError: def execfile(filename, globs): """A Python 3 implementation of execfile.""" - exec(compile(open(filename).read(), filename, 'exec'), globs) + with open(filename) as fobj: + code = fobj.read() + exec(compile(code, filename, 'exec'), globs) |