summaryrefslogtreecommitdiff
path: root/tests/backtest.py
blob: 439493d1327cb8322f90f5177838df4dc1426a76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"""Add things to old Pythons so I can pretend they are newer, for tests."""

# pylint: disable=W0622
# (Redefining built-in blah)
# The whole point of this file is to redefine built-ins, so shut up about it.

import subprocess


# 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.

    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

    # 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


# No more execfile in Py3
try:
    execfile = execfile
except NameError:
    def execfile(filename, globs):
        """A Python 3 implementation of execfile."""
        with open(filename) as fobj:
            code = fobj.read()
        exec(compile(code, filename, 'exec'), globs)