| 1 |
|
| 2 |
from rpython.tool.udir import udir |
| 3 |
import py |
| 4 |
import sys |
| 5 |
import pypy |
| 6 |
import subprocess |
| 7 |
import pytest |
| 8 |
|
| 9 |
if sys.platform == 'win32': |
| 10 |
pytestmark = pytest.mark.skip(reason="too slow on windows") |
| 11 |
|
| 12 |
pypypath = py.path.local(pypy.__file__).dirpath("bin", "pyinteractive.py") |
| 13 |
|
| 14 |
def run(*args, **kwds): |
| 15 |
stdin = kwds.pop('stdin', '') |
| 16 |
assert not kwds |
| 17 |
argslist = map(str, args) |
| 18 |
popen = subprocess.Popen(argslist, stdin=subprocess.PIPE, |
| 19 |
stdout=subprocess.PIPE) |
| 20 |
stdout, stderr = popen.communicate(stdin) |
| 21 |
print('--- stdout ---') |
| 22 |
print(stdout) |
| 23 |
print() |
| 24 |
print('--- stderr ---') |
| 25 |
print(stderr) |
| 26 |
print() |
| 27 |
return stdout |
| 28 |
|
| 29 |
|
| 30 |
def test_executable(): |
| 31 |
"""Ensures sys.executable points to the py.py script""" |
| 32 |
|
| 33 |
output = run(sys.executable, pypypath, '-S', |
| 34 |
"-c", "import sys;print(sys.executable)") |
| 35 |
assert output.splitlines()[-1] == pypypath |
| 36 |
|
| 37 |
def test_special_names(): |
| 38 |
"""Test the __name__ and __file__ special global names""" |
| 39 |
cmd = "print(__name__); print('__file__' in globals())" |
| 40 |
output = run(sys.executable, pypypath, '-S', '-c', cmd) |
| 41 |
assert output.splitlines()[-2] == '__main__' |
| 42 |
assert output.splitlines()[-1] == 'False' |
| 43 |
|
| 44 |
tmpfilepath = str(udir.join("test_py_script_1.py")) |
| 45 |
tmpfile = file( tmpfilepath, "w" ) |
| 46 |
tmpfile.write("print(__name__); print(__file__)\n") |
| 47 |
tmpfile.close() |
| 48 |
|
| 49 |
output = run(sys.executable, pypypath, '-S', tmpfilepath) |
| 50 |
assert output.splitlines()[-2] == '__main__' |
| 51 |
assert output.splitlines()[-1] == str(tmpfilepath) |
| 52 |
|
| 53 |
def test_argv_command(): |
| 54 |
"""Some tests on argv""" |
| 55 |
|
| 56 |
output = run(sys.executable, pypypath, '-S', |
| 57 |
"-c", "import sys;print(sys.argv)") |
| 58 |
assert output.splitlines()[-1] == str(['-c']) |
| 59 |
|
| 60 |
|
| 61 |
output = run(sys.executable, pypypath, '-S', |
| 62 |
"-c", "import sys;print(sys.argv)", "hello") |
| 63 |
assert output.splitlines()[-1] == str(['-c','hello']) |
| 64 |
|
| 65 |
|
| 66 |
output = run(sys.executable, pypypath, '-S', |
| 67 |
"-O", "-c", "import sys;print(sys.argv)", "hello") |
| 68 |
assert output.splitlines()[-1] == str(['-c','hello']) |
| 69 |
|
| 70 |
SCRIPT_1 = """ |
| 71 |
import sys |
| 72 |
print(sys.argv) |
| 73 |
""" |
| 74 |
def test_scripts(): |
| 75 |
tmpfilepath = str(udir.join("test_py_script.py")) |
| 76 |
tmpfile = file( tmpfilepath, "w" ) |
| 77 |
tmpfile.write(SCRIPT_1) |
| 78 |
tmpfile.close() |
| 79 |
|
| 80 |
|
| 81 |
output = run(sys.executable, pypypath, '-S', tmpfilepath) |
| 82 |
assert output.splitlines()[-1] == str([tmpfilepath]) |
| 83 |
|
| 84 |
|
| 85 |
output = run(sys.executable, pypypath, '-S', tmpfilepath, "hello") |
| 86 |
assert output.splitlines()[-1] == str([tmpfilepath,'hello']) |
| 87 |
|
| 88 |
|
| 89 |
output = run(sys.executable, pypypath, '-S', "-O", tmpfilepath, "hello") |
| 90 |
assert output.splitlines()[-1] == str([tmpfilepath,'hello']) |
| 91 |
|
| 92 |
def test_optimize_removes_assert(): |
| 93 |
tmpfilepath = str(udir.join("test_assert.py")) |
| 94 |
tmpfile = file(tmpfilepath, "w") |
| 95 |
tmpfile.write(""" |
| 96 |
try: |
| 97 |
assert 0 |
| 98 |
except AssertionError: |
| 99 |
print("AssertionError") |
| 100 |
else: |
| 101 |
print("nothing") |
| 102 |
""") |
| 103 |
tmpfile.close() |
| 104 |
|
| 105 |
|
| 106 |
output = run(sys.executable, pypypath, '-S', tmpfilepath) |
| 107 |
assert "AssertionError" in output |
| 108 |
|
| 109 |
|
| 110 |
output = run(sys.executable, pypypath, '-SO', tmpfilepath) |
| 111 |
assert "nothing" in output |
| 112 |
|
| 113 |
|
| 114 |
TB_NORMALIZATION_CHK= """ |
| 115 |
class K(object): |
| 116 |
def __repr__(self): |
| 117 |
return "<normalized>" |
| 118 |
def __str__(self): |
| 119 |
return "-not normalized-" |
| 120 |
|
| 121 |
{}[K()] |
| 122 |
""" |
| 123 |
|
| 124 |
def test_tb_normalization(): |
| 125 |
if sys.platform == "win32": |
| 126 |
py.test.skip("cannot detect process exit code for now") |
| 127 |
tmpfilepath = str(udir.join("test_py_script.py")) |
| 128 |
tmpfile = file( tmpfilepath, "w" ) |
| 129 |
tmpfile.write(TB_NORMALIZATION_CHK) |
| 130 |
tmpfile.close() |
| 131 |
|
| 132 |
popen = subprocess.Popen([sys.executable, str(pypypath), '-S', tmpfilepath], |
| 133 |
stderr=subprocess.PIPE) |
| 134 |
_, stderr = popen.communicate() |
| 135 |
assert 'KeyError: <normalized>\n' in stderr |
| 136 |
|
| 137 |
|
| 138 |
def test_pytrace(): |
| 139 |
output = run(sys.executable, pypypath, '-S', |
| 140 |
stdin="__pytrace__ = 1\nx = 5\nx") |
| 141 |
output = output.replace('\r\n', '\n') |
| 142 |
assert ('\t<module>: LOAD_CONST 0 (5)\n' |
| 143 |
'\t<module>: STORE_NAME 0 (x)\n' |
| 144 |
'\t<module>: LOAD_CONST 1 (None)\n' |
| 145 |
'\t<module>: RETURN_VALUE 0 \n' |
| 146 |
'>>>> ') in output |
| 147 |
|
| 148 |
assert ('\t<module>: LOAD_NAME 0 (x)\n' |
| 149 |
'\t<module>: PRINT_EXPR 0 \n') in output |
| 150 |
|
| 151 |
def test_pytrace_dis_bug(): |
| 152 |
output = run(sys.executable, pypypath, '-S', |
| 153 |
stdin="__pytrace__ = 1\nfor i in range(1): i += 1\n") |
| 154 |
output = output.replace('\r\n', '\n') |
| 155 |
assert """\ |
| 156 |
\t<module>: LOAD_NAME 0 (range) |
| 157 |
\t<module>: LOAD_CONST 0 (1) |
| 158 |
\t<module>: CALL_FUNCTION 1 |
| 159 |
\t<module>: GET_ITER 0 |
| 160 |
\t<module>: FOR_ITER 6 (to 20) |
| 161 |
\t<module>: STORE_NAME 1 (i) |
| 162 |
\t<module>: LOAD_NAME 1 (i) |
| 163 |
\t<module>: LOAD_CONST 0 (1) |
| 164 |
\t<module>: INPLACE_ADD 0 |
| 165 |
\t<module>: STORE_NAME 1 (i) |
| 166 |
\t<module>: JUMP_ABSOLUTE 4 (to 8) |
| 167 |
\t<module>: FOR_ITER 6 (to 20) |
| 168 |
\t<module>: LOAD_CONST 1 (None) |
| 169 |
\t<module>: RETURN_VALUE 0 |
| 170 |
""" in output |
| 171 |
|