Debian Codegraph @

extra_tests/test_pyrepl/test_functional.py

raw symbols SCIP
1 # Copyright 2000-2007 Michael Hudson-Doyle <[email protected]>
2 # Maciek Fijalkowski
3 # License: MIT
4 # some functional tests, to see if this is really working
5
6 import pytest
7 from contextlib import contextmanager
8 import sys, os
9 import pytest
10
11 try:
12 from pexpect import spawn
13 except ImportError:
14 pytest.skip(allow_module_level=True)
15
16 @contextmanager
17 def start_repl(extra_env=dict(), explicit_pyrepl=True, colors=False):
18 args = []
19 if explicit_pyrepl:
20 args = ['-S', '-c', "from pyrepl.main import interactive_console as __pyrepl_interactive_console; __pyrepl_interactive_console()"],
21 child = spawn(
22 sys.executable,
23 env=os.environ | {"PYTHON_COLORS": "1" if colors else "0"} | extra_env,
24 timeout=10, encoding="utf-8")
25 child.logfile = sys.stdout
26 yield child
27 child.close()
28
29
30 def test_basic():
31 with start_repl() as child:
32 child.sendline('a = 120202012012')
33 child.sendline('a')
34 child.expect('120202012012')
35
36 def test_error():
37 with start_repl() as child:
38 child.sendline('1/0')
39 child.expect('Traceback.*File.*1/0.*ZeroDivisionError: division by zero')
40
41 def test_ctrl_left_ctrl_right():
42 with start_repl() as child:
43 child.send('abc 123456789')
44 child.send('\033[1;5D') # ctrl-left
45 child.send('=')
46 child.send('\033[1;5C') # ctrl-right
47 child.sendline('88888')
48 child.sendline('abc')
49 child.expect('12345678988888')
50
51 def test_sys_excepthook_is_broken():
52 with start_repl() as child:
53 child.sendline("import sys")
54 child.sendline("sys.excepthook = 1")
55 child.sendline("1/0")
56 child.expect('Error in sys.excepthook.*object is not callable.*Traceback(.*)ZeroDivisionError: division by zero')
57 child.sendline('a = 10000000000')
58 child.sendline('a * 5')
59 child.expect('50000000000')
60
61 def test_sys_tracebacklimit_is_correct():
62 with start_repl() as child:
63 child.sendline("def x1(): 1/0")
64 child.sendline("def x2(): x1()")
65 child.sendline("def x3(): x2()")
66 child.sendline("x3()")
67 child.expect('Traceback.*File.*in x3.*File.*in x2.*File.*in x1.*1/0.*ZeroDivisionError: division by zero')
68 child.sendline("import sys")
69 child.sendline("sys.tracebacklimit=1")
70 child.sendline("x3()")
71 child.expect('Traceback(.*)ZeroDivisionError: division by zero')
72 assert "x3" not in child.match.group(1)
73
74 def test_hyperlinks_error():
75 with start_repl(colors=True) as child:
76 child.sendline("import traceback; list(traceback.walk_tb(1))")
77 import socket
78 import traceback
79 child.expect(f"\x1b]8;;file://{traceback.__file__}\x1b.{traceback.__file__}\x1b]8;;\x1b.")
80
81 def test_dumb_terminal():
82 with start_repl(extra_env=dict(TERM="dumb"), explicit_pyrepl=False) as child:
83 child.sendline('a = 10000000000')
84 child.sendline('a * 5')
85 child.expect('50000000000')
86 # assert "InvalidTerminal" not in child.match.string
87
88 def test_syntaxerror_correct_filename_and_positions():
89 with start_repl(colors=False) as child:
90 child.sendline('a bbbb c')
91 child.expect(' File "<python-input-0>", line 1')
92 child.expect(' a bbbb c')
93 child.expect('^^^^')
94 child.expect('SyntaxError')
95 with start_repl(colors=False) as child:
96 child.sendline(' 124')
97 child.expect(' File "<python-input-0>", line 1')
98 child.expect(' 124')
99 child.expect('^^^^')
100 child.expect('IndentationError')
101
102 def test_cmd_module_tab_completion_with_pyrepl_readline(tmpdir):
103 fn = tmpdir / "cmdbug.py"
104 fn.write("""
105 import cmd
106
107 class Console(cmd.Cmd):
108
109 def do_abc(self, arg):
110 print("ABC!!!!", arg)
111
112 def do_exit(self, arg):
113 raise SystemExit(0)
114
115 def completedefault(self, text: str, line: str, begidx: int, endidx: int):
116 return ["foo", "bar"]
117
118 if __name__ == "__main__":
119 Console().cmdloop()
120 """)
121 child = spawn(
122 sys.executable,
123 [str(fn)],
124 env=os.environ,
125 timeout=2, encoding="utf-8")
126 try:
127 child.sendline("a\t def")
128 child.expect("ABC!!!! def")
129 child.sendline("a\t f\t\t")
130 child.expect("bar foo")
131 finally:
132 child.close()
133
134 def test_sys_audit_called_in_pyrepl(tmpdir):
135 with start_repl(colors=False) as child:
136 child.sendline("import sys")
137 child.sendline("sys.addaudithook(lambda name, *args: print(name, *args) if 'input' in name else None)")
138 child.sendline("x = input('xyz')")
139 child.sendline("abc")
140 child.expect("input.*xyz")
141 child.expect("input/result.*abc")
142
143 def test_input_is_not_monkeypatched(tmpdir):
144 with start_repl(colors=False) as child:
145 child.sendline("print(input)")
146 child.expect("<built-in function input>")
147
148 def test_tab_completion_works():
149 with start_repl(colors=False, explicit_pyrepl=False) as child:
150 child.sendline("import io")
151 child.sendline("io.Bloc\t)")
152 child.expect("BlockingIOError()")