Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork252
Expand file tree
/
Copy pathtest_crashers.py
More file actions
153 lines (124 loc) · 4.38 KB
/
test_crashers.py
File metadata and controls
153 lines (124 loc) · 4.38 KB
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
importfcntl
importos
importpty
importstruct
importsys
importtermios
importtextwrap
importunittest
frombpython.testimportTEST_CONFIG
frombpython.configimportgetpreferredencoding
try:
fromtwisted.internetimportreactor
fromtwisted.internet.deferimportDeferred
fromtwisted.internet.protocolimportProcessProtocol
fromtwisted.trial.unittestimportTestCaseasTrialTestCase
exceptImportError:
classTrialTestCase:# type: ignore [no-redef]
pass
reactor=None# type: ignore
try:
importurwid
have_urwid=True
exceptImportError:
have_urwid=False
defset_win_size(fd,rows,columns):
s=struct.pack("HHHH",rows,columns,0,0)
fcntl.ioctl(fd,termios.TIOCSWINSZ,s)
classCrashersTest:
backend="cli"
defrun_bpython(self,input):
"""
Run bpython (with `backend` as backend) in a subprocess and
enter the given input. Uses a test config that disables the
paste detection.
Returns bpython's output.
"""
result=Deferred()
encoding=getpreferredencoding()
classProtocol(ProcessProtocol):
STATES= (SEND_INPUT,COLLECT)=range(2)
def__init__(self):
self.data=""
self.delayed_call=None
self.states=iter(self.STATES)
self.state=next(self.states)
defoutReceived(self,data):
self.data+=data.decode(encoding)
ifself.delayed_callisnotNone:
self.delayed_call.cancel()
self.delayed_call=reactor.callLater(0.5,self.next)
defnext(self):
self.delayed_call=None
ifself.state==self.SEND_INPUT:
index=self.data.find(">>> ")
ifindex>=0:
self.data=self.data[index+4 :]
self.transport.write(input.encode(encoding))
self.state=next(self.states)
elifself.data=="\x1b[6n":
# this is a cursor position query
# respond that cursor is on row 2, column 1
self.transport.write("\x1b[2;1R".encode(encoding))
else:
self.transport.closeStdin()
ifself.transport.pidisnotNone:
self.delayed_call=None
self.transport.signalProcess("TERM")
defprocessExited(self,reason):
ifself.delayed_callisnotNone:
self.delayed_call.cancel()
result.callback(self.data)
(master,slave)=pty.openpty()
set_win_size(slave,25,80)
reactor.spawnProcess(
Protocol(),
sys.executable,
(
sys.executable,
"-m",
f"bpython.{self.backend}",
"--config",
str(TEST_CONFIG),
"-q",# prevents version greeting
),
env={
"TERM":"vt100",
"LANG":os.environ.get("LANG","C.UTF-8"),
},
usePTY=(master,slave,os.ttyname(slave)),
)
returnresult
deftest_issue108(self):
input=textwrap.dedent(
"""\
def spam():
u"y\\xe4y"
\b
spam("""
)
deferred=self.run_bpython(input)
returndeferred.addCallback(self.check_no_traceback)
deftest_issue133(self):
input=textwrap.dedent(
"""\
def spam(a, (b, c)):
pass
\b
spam(1"""
)
returnself.run_bpython(input).addCallback(self.check_no_traceback)
defcheck_no_traceback(self,data):
self.assertNotIn("Traceback",data)
@unittest.skipIf(reactorisNone,"twisted is not available")
classCurtsiesCrashersTest(TrialTestCase,CrashersTest):
backend="curtsies"
@unittest.skipIf(reactorisNone,"twisted is not available")
classCursesCrashersTest(TrialTestCase,CrashersTest):
backend="cli"
@unittest.skipUnless(have_urwid,"urwid is required")
@unittest.skipIf(reactorisNone,"twisted is not available")
classUrwidCrashersTest(TrialTestCase,CrashersTest):
backend="urwid"
if__name__=="__main__":
unittest.main()