File: test_zpy.py

package info (click to toggle)
pypy3 7.3.19%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 212,236 kB
  • sloc: python: 2,098,316; ansic: 540,565; sh: 21,462; asm: 14,419; cpp: 4,451; makefile: 4,209; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 12; awk: 4
file content (167 lines) | stat: -rw-r--r-- 5,501 bytes parent folder | download | duplicates (2)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167

from rpython.tool.udir import udir
import py
import sys
import pypy
import subprocess

pypypath = py.path.local(pypy.__file__).dirpath("bin", "pyinteractive.py")

def run(*args, **kwds):
    stdin = kwds.pop('stdin', '')
    assert not kwds
    argslist = map(str, args)
    popen = subprocess.Popen(argslist, stdin=subprocess.PIPE,
                                       stdout=subprocess.PIPE)
    stdout, stderr = popen.communicate(stdin)
    print('--- stdout ---')
    print(stdout)
    print()
    print('--- stderr ---')
    print(stderr)
    print()
    return stdout


def test_executable():
    """Ensures sys.executable points to the py.py script"""
    # TODO : watch out for spaces/special chars in pypypath
    output = run(sys.executable, pypypath, '-S',
                 "-c", "import sys;print(sys.executable)")
    assert output.splitlines()[-1] == pypypath

def test_special_names():
    """Test the __name__ and __file__ special global names"""
    cmd = "print(__name__); print('__file__' in globals())"
    output = run(sys.executable, pypypath, '-S', '-c', cmd)
    assert output.splitlines()[-2] == '__main__'
    assert output.splitlines()[-1] == 'False'

    tmpfilepath = str(udir.join("test_py_script_1.py"))
    tmpfile = file( tmpfilepath, "w" )
    tmpfile.write("print(__name__); print(__file__)\n")
    tmpfile.close()

    output = run(sys.executable, pypypath, '-S', tmpfilepath)
    assert output.splitlines()[-2] == '__main__'
    assert output.splitlines()[-1] == str(tmpfilepath)

def test_argv_command():
    """Some tests on argv"""
    # test 1 : no arguments
    output = run(sys.executable, pypypath, '-S',
                 "-c", "import sys;print(sys.argv)")
    assert output.splitlines()[-1] == str(['-c'])

    # test 2 : some arguments after
    output = run(sys.executable, pypypath, '-S',
                 "-c", "import sys;print(sys.argv)", "hello")
    assert output.splitlines()[-1] == str(['-c','hello'])
    
    # test 3 : additionnal pypy parameters
    output = run(sys.executable, pypypath, '-S',
                 "-O", "-c", "import sys;print(sys.argv)", "hello")
    assert output.splitlines()[-1] == str(['-c','hello'])

SCRIPT_1 = """
import sys
print(sys.argv)
"""
def test_scripts():
    tmpfilepath = str(udir.join("test_py_script.py"))
    tmpfile = file( tmpfilepath, "w" )
    tmpfile.write(SCRIPT_1)
    tmpfile.close()

    # test 1 : no arguments
    output = run(sys.executable, pypypath, '-S', tmpfilepath)
    assert output.splitlines()[-1] == str([tmpfilepath])
    
    # test 2 : some arguments after
    output = run(sys.executable, pypypath, '-S', tmpfilepath, "hello")
    assert output.splitlines()[-1] == str([tmpfilepath,'hello'])
    
    # test 3 : additionnal pypy parameters
    output = run(sys.executable, pypypath, '-S', "-O", tmpfilepath, "hello")
    assert output.splitlines()[-1] == str([tmpfilepath,'hello'])

def test_optimize_removes_assert():
    tmpfilepath = str(udir.join("test_assert.py"))
    tmpfile = file(tmpfilepath, "w")
    tmpfile.write("""
try:
    assert 0
except AssertionError:
    print("AssertionError")
else:
    print("nothing")
""")
    tmpfile.close()

    # no optimization: crashes
    output = run(sys.executable, pypypath, '-S', tmpfilepath)
    assert "AssertionError" in output

    # optimization: just works
    output = run(sys.executable, pypypath, '-SO', tmpfilepath)
    assert "nothing" in output


TB_NORMALIZATION_CHK= """
class K(object):
  def __repr__(self):
     return "<normalized>"
  def __str__(self):
     return "-not normalized-"

{}[K()]
"""

def test_tb_normalization():
    if sys.platform == "win32":
        py.test.skip("cannot detect process exit code for now")
    tmpfilepath = str(udir.join("test_py_script.py"))
    tmpfile = file( tmpfilepath, "w" )
    tmpfile.write(TB_NORMALIZATION_CHK)
    tmpfile.close()

    popen = subprocess.Popen([sys.executable, str(pypypath), '-S', tmpfilepath],
                             stderr=subprocess.PIPE)
    _, stderr = popen.communicate()
    assert 'KeyError: <normalized>\n' in stderr


def test_pytrace():
    output = run(sys.executable, pypypath, '-S',
                 stdin="__pytrace__ = 1\nx = 5\nx")
    output = output.replace('\r\n', '\n')
    assert ('\t<module>:           LOAD_CONST    0 (5)\n'
            '\t<module>:           STORE_NAME    0 (x)\n'
            '\t<module>:           LOAD_CONST    1 (None)\n'
            '\t<module>:           RETURN_VALUE    0 \n'
            '>>>> ') in output
    # '5\n' --- this line sent to stderr
    assert ('\t<module>:           LOAD_NAME    0 (x)\n'
            '\t<module>:           PRINT_EXPR    0 \n') in output

def test_pytrace_dis_bug():
    output = run(sys.executable, pypypath, '-S',
                 stdin="__pytrace__ = 1\nfor i in range(1): i += 1\n")
    output = output.replace('\r\n', '\n')
    assert """\
\t<module>:           LOAD_NAME    0 (range)
\t<module>:           LOAD_CONST    0 (1)
\t<module>:           CALL_FUNCTION    1 
\t<module>:           GET_ITER    0 
\t<module>:           FOR_ITER    6 (to 20)
\t<module>:           STORE_NAME    1 (i)
\t<module>:           LOAD_NAME    1 (i)
\t<module>:           LOAD_CONST    0 (1)
\t<module>:           INPLACE_ADD    0 
\t<module>:           STORE_NAME    1 (i)
\t<module>:           JUMP_ABSOLUTE    4 (to 8)
\t<module>:           FOR_ITER    6 (to 20)
\t<module>:           LOAD_CONST    1 (None)
\t<module>:           RETURN_VALUE    0 
""" in output