|
36 | 36 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
37 | 37 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
38 | 38 | # SOFTWARE.
|
39 |
| -import os |
40 |
| -import re |
41 |
| -import select |
42 |
| -import subprocess |
43 |
| -import sys |
44 |
| -import tempfile |
45 |
| -import termios |
46 |
| -from textwrap import dedent |
47 | 39 |
|
| 40 | +import sys |
48 | 41 |
|
49 |
| -def validate_repl(stdin, python_args=(), ignore_preamble=True): |
50 |
| - env = os.environ.copy() |
51 |
| - env['TERM'] = 'ansi' |
52 |
| - env['PYTHONIOENCODING'] = 'utf-8' |
53 |
| - pty_parent, pty_child = os.openpty() |
54 |
| - try: |
55 |
| - termios.tcsetwinsize(pty_parent, (60, 80)) |
56 |
| - proc = subprocess.Popen( |
57 |
| - [sys.executable, '-I', *python_args], |
58 |
| - env=env, |
59 |
| - stdin=pty_child, |
60 |
| - stdout=pty_child, |
61 |
| - stderr=pty_child, |
62 |
| - ) |
63 |
| - out = '' |
64 |
| - input_and_output = [] |
65 |
| - expected_preamble = '' |
66 |
| - in_matches = list(re.finditer(r'^(>>>|\.\.\.) (.*)', stdin, flags=re.MULTILINE)) |
67 |
| - for i, match in enumerate(in_matches): |
68 |
| - if i == 0: |
69 |
| - expected_preamble = stdin[:match.start() - 1] if match.start() else '' |
70 |
| - input_and_output.append(( |
71 |
| - match.group(1), |
72 |
| - match.group(2), |
73 |
| - stdin[match.end():in_matches[i + 1].start() - 1 if i + 1 < len(in_matches) else -1], |
74 |
| - )) |
75 |
| - index = -1 |
76 |
| - whole_out = '' |
77 |
| - while True: |
78 |
| - rlist, _, _ = select.select([pty_parent], [], [], 30) |
79 |
| - assert pty_parent in rlist, f"Timed out waiting for REPL output. Output: {whole_out}{out}" |
80 |
| - out += os.read(pty_parent, 1024).decode('utf-8') |
81 |
| - out = out.replace('\r\n', '\n') |
82 |
| - out = re.sub(r'\x1b\[(?:\?2004[hl]|\d+[A-G])', '', out) |
83 |
| - if out == '>>> ' or out.endswith(('\n>>> ', '\n... ')): |
84 |
| - prompt = out[:3] |
85 |
| - actual = out[:-5] |
86 |
| - if index >= 0: |
87 |
| - expected_prompt, current_in, expected_out = input_and_output[index] |
88 |
| - assert prompt == expected_prompt |
89 |
| - expected = f'{expected_prompt} {current_in}{expected_out}' |
90 |
| - else: |
91 |
| - expected = expected_preamble |
92 |
| - if index >= 0 or not ignore_preamble: |
93 |
| - assert actual == expected, f'Actual:\n{actual!r}\nExpected:\n{expected!r}' |
94 |
| - index += 1 |
95 |
| - whole_out += out[:-4] |
96 |
| - out = out[-4:] |
97 |
| - if index >= len(input_and_output): |
98 |
| - os.write(pty_parent, b'\x04') # CTRL-D |
99 |
| - proc.wait(timeout=30) |
100 |
| - out = os.read(pty_parent, 1024).decode('utf-8') |
101 |
| - out = re.sub(r'\x1b\[\?2004[hl]', '', out) |
102 |
| - assert not out.strip(), f"Garbage after EOF:\n{out!r}" |
103 |
| - return |
104 |
| - else: |
105 |
| - _, next_in, _ = input_and_output[index] |
106 |
| - os.write(pty_parent, next_in.encode('utf-8') + b'\r') |
107 |
| - finally: |
108 |
| - os.close(pty_child) |
109 |
| - os.close(pty_parent) |
| 42 | +if sys.platform != 'win32' and (sys.implementation.name != 'graalpy' or not __graalpython__.is_managed_launcher()): |
| 43 | + import os |
| 44 | + import re |
| 45 | + import select |
| 46 | + import subprocess |
| 47 | + import tempfile |
| 48 | + import termios |
| 49 | + from textwrap import dedent |
110 | 50 |
|
111 | 51 |
|
112 |
| -def test_basic_repl(): |
113 |
| - validate_repl(dedent("""\ |
114 |
| - >>> 1023 + 1 |
115 |
| - 1024 |
116 |
| - >>> None |
117 |
| - >>> "hello" |
118 |
| - 'hello' |
119 |
| - >>> _ |
120 |
| - 'hello' |
121 |
| - """)) |
| 52 | + def validate_repl(stdin, python_args=(), ignore_preamble=True): |
| 53 | + env = os.environ.copy() |
| 54 | + env['TERM'] = 'ansi' |
| 55 | + env['PYTHONIOENCODING'] = 'utf-8' |
| 56 | + pty_parent, pty_child = os.openpty() |
| 57 | + try: |
| 58 | + termios.tcsetwinsize(pty_parent, (60, 80)) |
| 59 | + proc = subprocess.Popen( |
| 60 | + [sys.executable, '-I', *python_args], |
| 61 | + env=env, |
| 62 | + stdin=pty_child, |
| 63 | + stdout=pty_child, |
| 64 | + stderr=pty_child, |
| 65 | + ) |
| 66 | + out = '' |
| 67 | + input_and_output = [] |
| 68 | + expected_preamble = '' |
| 69 | + in_matches = list(re.finditer(r'^(>>>|\.\.\.) (.*)', stdin, flags=re.MULTILINE)) |
| 70 | + for i, match in enumerate(in_matches): |
| 71 | + if i == 0: |
| 72 | + expected_preamble = stdin[:match.start() - 1] if match.start() else '' |
| 73 | + input_and_output.append(( |
| 74 | + match.group(1), |
| 75 | + match.group(2), |
| 76 | + stdin[match.end():in_matches[i + 1].start() - 1 if i + 1 < len(in_matches) else -1], |
| 77 | + )) |
| 78 | + index = -1 |
| 79 | + whole_out = '' |
| 80 | + while True: |
| 81 | + rlist, _, _ = select.select([pty_parent], [], [], 30) |
| 82 | + assert pty_parent in rlist, f"Timed out waiting for REPL output. Output: {whole_out}{out}" |
| 83 | + out += os.read(pty_parent, 1024).decode('utf-8') |
| 84 | + out = out.replace('\r\n', '\n') |
| 85 | + out = re.sub(r'\x1b\[(?:\?2004[hl]|\d+[A-G])', '', out) |
| 86 | + if out == '>>> ' or out.endswith(('\n>>> ', '\n... ')): |
| 87 | + prompt = out[:3] |
| 88 | + actual = out[:-5] |
| 89 | + if index >= 0: |
| 90 | + expected_prompt, current_in, expected_out = input_and_output[index] |
| 91 | + assert prompt == expected_prompt |
| 92 | + expected = f'{expected_prompt} {current_in}{expected_out}' |
| 93 | + else: |
| 94 | + expected = expected_preamble |
| 95 | + if index >= 0 or not ignore_preamble: |
| 96 | + assert actual == expected, f'Actual:\n{actual!r}\nExpected:\n{expected!r}' |
| 97 | + index += 1 |
| 98 | + whole_out += out[:-4] |
| 99 | + out = out[-4:] |
| 100 | + if index >= len(input_and_output): |
| 101 | + os.write(pty_parent, b'\x04') # CTRL-D |
| 102 | + proc.wait(timeout=30) |
| 103 | + out = os.read(pty_parent, 1024).decode('utf-8') |
| 104 | + out = re.sub(r'\x1b\[\?2004[hl]', '', out) |
| 105 | + assert not out.strip(), f"Garbage after EOF:\n{out!r}" |
| 106 | + return |
| 107 | + else: |
| 108 | + _, next_in, _ = input_and_output[index] |
| 109 | + os.write(pty_parent, next_in.encode('utf-8') + b'\r') |
| 110 | + finally: |
| 111 | + os.close(pty_child) |
| 112 | + os.close(pty_parent) |
122 | 113 |
|
123 | 114 |
|
124 |
| -def test_continuation(): |
125 |
| - validate_repl(dedent(r'''\ |
126 |
| - >>> def foo(): |
127 |
| - ... a = 1 |
128 |
| - ... return a |
129 |
| - ... |
130 |
| - >>> class Foo: |
131 |
| - ... def meth(self): |
132 |
| - ... return 1 |
133 |
| - ... |
134 |
| - >>> from functools import wraps |
135 |
| - >>> @wraps |
136 |
| - ... def foo(fn): |
137 |
| - ... return fn |
138 |
| - ... |
139 |
| - >>> from contextlib import contextmanager |
140 |
| - >>> @contextmanager |
141 |
| - ... class Foo: |
142 |
| - ... pass |
143 |
| - ... |
144 |
| - >>> """ |
145 |
| - ... asdf |
146 |
| - ... """ |
147 |
| - '\nasdf\n' |
148 |
| - ''')) |
| 115 | + def test_basic_repl(): |
| 116 | + validate_repl(dedent("""\ |
| 117 | + >>> 1023 + 1 |
| 118 | + 1024 |
| 119 | + >>> None |
| 120 | + >>> "hello" |
| 121 | + 'hello' |
| 122 | + >>> _ |
| 123 | + 'hello' |
| 124 | + """)) |
149 | 125 |
|
150 | 126 |
|
151 |
| -def test_exceptions(): |
152 |
| - validate_repl(dedent("""\ |
153 |
| - >>> 1 / 0 |
154 |
| - Traceback (most recent call last): |
155 |
| - File "<stdin>", line 1, in <module> |
156 |
| - ZeroDivisionError: division by zero |
157 |
| - >>> import sys |
158 |
| - >>> sys.last_value |
159 |
| - ZeroDivisionError('division by zero') |
160 |
| - >>> class BrokenRepr: |
161 |
| - ... def __repr__(self): |
162 |
| - ... asdf |
163 |
| - ... |
164 |
| - >>> BrokenRepr() |
165 |
| - Traceback (most recent call last): |
166 |
| - File "<stdin>", line 1, in <module> |
167 |
| - File "<stdin>", line 3, in __repr__ |
168 |
| - NameError: name 'asdf' is not defined |
169 |
| - """)) |
| 127 | + def test_continuation(): |
| 128 | + validate_repl(dedent(r'''\ |
| 129 | + >>> def foo(): |
| 130 | + ... a = 1 |
| 131 | + ... return a |
| 132 | + ... |
| 133 | + >>> class Foo: |
| 134 | + ... def meth(self): |
| 135 | + ... return 1 |
| 136 | + ... |
| 137 | + >>> from functools import wraps |
| 138 | + >>> @wraps |
| 139 | + ... def foo(fn): |
| 140 | + ... return fn |
| 141 | + ... |
| 142 | + >>> from contextlib import contextmanager |
| 143 | + >>> @contextmanager |
| 144 | + ... class Foo: |
| 145 | + ... pass |
| 146 | + ... |
| 147 | + >>> """ |
| 148 | + ... asdf |
| 149 | + ... """ |
| 150 | + '\nasdf\n' |
| 151 | + ''')) |
170 | 152 |
|
171 | 153 |
|
172 |
| -def test_inspect_flag(): |
173 |
| - with tempfile.NamedTemporaryFile('w') as f: |
174 |
| - f.write('a = 1\n') |
175 |
| - f.flush() |
| 154 | + def test_exceptions(): |
176 | 155 | validate_repl(dedent("""\
|
177 |
| - >>> a |
178 |
| - 1 |
179 |
| - """), python_args=['-i', f.name], ignore_preamble=False) |
| 156 | + >>> 1 / 0 |
| 157 | + Traceback (most recent call last): |
| 158 | + File "<stdin>", line 1, in <module> |
| 159 | + ZeroDivisionError: division by zero |
| 160 | + >>> import sys |
| 161 | + >>> sys.last_value |
| 162 | + ZeroDivisionError('division by zero') |
| 163 | + >>> class BrokenRepr: |
| 164 | + ... def __repr__(self): |
| 165 | + ... asdf |
| 166 | + ... |
| 167 | + >>> BrokenRepr() |
| 168 | + Traceback (most recent call last): |
| 169 | + File "<stdin>", line 1, in <module> |
| 170 | + File "<stdin>", line 3, in __repr__ |
| 171 | + NameError: name 'asdf' is not defined |
| 172 | + """)) |
180 | 173 |
|
181 | 174 |
|
182 |
| -def test_inspect_flag_exit(): |
183 |
| - with tempfile.NamedTemporaryFile('w') as f: |
184 |
| - f.write('a = 1\nimport sys\nsys.exit(1)\n') |
185 |
| - f.flush() |
186 |
| - validate_repl(dedent(f"""\ |
187 |
| - Traceback (most recent call last): |
188 |
| - File "{os.path.realpath(f.name)}", line 3, in <module> |
189 |
| - sys.exit(1) |
190 |
| - SystemExit: 1 |
191 |
| - >>> a |
192 |
| - 1 |
193 |
| - """), python_args=['-i', f.name], ignore_preamble=False) |
| 175 | + def test_inspect_flag(): |
| 176 | + with tempfile.NamedTemporaryFile('w') as f: |
| 177 | + f.write('a = 1\n') |
| 178 | + f.flush() |
| 179 | + validate_repl(dedent("""\ |
| 180 | + >>> a |
| 181 | + 1 |
| 182 | + """), python_args=['-i', f.name], ignore_preamble=False) |
| 183 | + |
| 184 | + |
| 185 | + def test_inspect_flag_exit(): |
| 186 | + with tempfile.NamedTemporaryFile('w') as f: |
| 187 | + f.write('a = 1\nimport sys\nsys.exit(1)\n') |
| 188 | + f.flush() |
| 189 | + validate_repl(dedent(f"""\ |
| 190 | + Traceback (most recent call last): |
| 191 | + File "{os.path.realpath(f.name)}", line 3, in <module> |
| 192 | + sys.exit(1) |
| 193 | + SystemExit: 1 |
| 194 | + >>> a |
| 195 | + 1 |
| 196 | + """), python_args=['-i', f.name], ignore_preamble=False) |
0 commit comments