diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2009-09-22 07:22:56 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2009-09-22 07:22:56 -0400 |
commit | ff9099051e0daff7b08035713eeca5696ab3217a (patch) | |
tree | 13a391e9010e9e0b99ff403ec2fd0252d460ab5a /test/backtest.py | |
parent | e3c0b4b2c7d2a92344d30a25467a1e863bbb7d31 (diff) | |
download | python-coveragepy-git-ff9099051e0daff7b08035713eeca5696ab3217a.tar.gz |
The best way to get py3k support: same source runs on both, with some contortions.
Diffstat (limited to 'test/backtest.py')
-rw-r--r-- | test/backtest.py | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/test/backtest.py b/test/backtest.py new file mode 100644 index 00000000..21a14d6d --- /dev/null +++ b/test/backtest.py @@ -0,0 +1,54 @@ +"""Add things to old Pythons so I can pretend they are newer, for tests.""" + +# pylint: disable-msg=W0622 +# (Redefining built-in blah) +# The whole point of this file is to redefine built-ins, so shut up about it. + +import os, sys + +# Py2k and 3k don't agree on how to run commands in a subprocess. +try: + import subprocess +except ImportError: + def run_command(cmd): + """Run a command in a subprocess. + + Returns the exit code and the combined stdout and stderr. + + """ + _, stdouterr = os.popen4(cmd) + return 0, stdouterr.read() +else: + def run_command(cmd): + """Run a command in a subprocess. + + Returns the exit code and the combined stdout and stderr. + + """ + + if sys.hexversion > 0x03000000 and cmd.startswith("coverage "): + # We don't have a coverage command on 3.x, so fix it up to call the + # script. Eventually we won't need this. + cmd = "python " + sys.prefix + os.sep + "Scripts" + os.sep + cmd + + proc = subprocess.Popen(cmd, shell=True, + stdin=subprocess.PIPE, stdout=subprocess.PIPE, + stderr=subprocess.STDOUT + ) + retcode = proc.wait() + + # Get the output, and canonicalize it to strings with newlines. + output = proc.stdout.read() + if not isinstance(output, str): + output = output.decode('utf-8') + output = output.replace('\r', '') + + return retcode, output + +# No more execfile in Py3k +try: + execfile = execfile +except NameError: + def execfile(filename, globs): + """A Python 3 implementation of execfile.""" + exec(compile(open(filename).read(), filename, 'exec'), globs) |