2
Tests for the entry point of pypy-c, app_main.py.
7
from pypy.tool.udir import udir
11
print 'Name:', __name__
12
print 'File:', __file__
14
print 'Exec:', sys.executable
15
print 'Argv:', sys.argv
20
CRASHING_DEMO_SCRIPT = """
25
print 'Goodbye2' # should not be reached
28
banner = sys.version.splitlines()[0]
31
# force 'path' to be a relative path, for testing purposes
32
curdir = py.path.local()
33
p = py.path.local(path)
35
while not p.relto(curdir):
36
result.append(os.pardir)
37
if curdir == curdir.dirpath():
38
return str(path) # no relative path found, give up
39
curdir = curdir.dirpath()
40
result.append(p.relto(curdir))
41
return os.path.join(*result)
43
app_main = os.path.join(autopath.this_dir, os.pardir, 'app_main.py')
44
app_main = os.path.abspath(app_main)
46
demo_script_p = udir.join('demo_test_app_main.py')
47
demo_script_p.write(DEMO_SCRIPT)
48
demo_script = relpath(demo_script_p)
50
crashing_demo_script_p = udir.join('crashing_demo_test_app_main.py')
51
crashing_demo_script_p.write(CRASHING_DEMO_SCRIPT)
52
crashing_demo_script = relpath(crashing_demo_script_p)
55
class TestInteraction:
57
These tests require pexpect (UNIX-only).
58
http://pexpect.sourceforge.net/
61
def _spawn(self, *args, **kwds):
64
except ImportError, e:
66
kwds.setdefault('timeout', 10)
67
print 'SPAWN:', args, kwds
68
child = pexpect.spawn(*args, **kwds)
69
child.logfile = sys.stdout
72
def spawn(self, argv):
73
return self._spawn(sys.executable, [app_main] + argv)
75
def test_interactive(self):
76
child = self.spawn([])
77
child.expect('Python ') # banner
78
child.expect('>>> ') # prompt
79
child.sendline('[6*7]')
80
child.expect(re.escape('[42]'))
81
child.sendline('def f(x):')
82
child.expect(re.escape('... '))
83
child.sendline(' return x + 100')
84
child.expect(re.escape('... '))
87
child.sendline('f(98)')
90
child.sendline('__name__')
91
child.expect("'__main__'")
93
def test_run_script(self):
94
child = self.spawn([demo_script])
95
idx = child.expect(['hello', 'Python ', '>>> '])
96
assert idx == 0 # no banner or prompt
97
child.expect(re.escape("Name: __main__"))
98
child.expect(re.escape('File: ' + demo_script))
99
child.expect(re.escape('Exec: ' + app_main))
100
child.expect(re.escape('Argv: ' + repr([demo_script])))
101
child.expect('goodbye')
103
def test_run_script_with_args(self):
104
argv = [demo_script, 'hello', 'world']
105
child = self.spawn(argv)
106
child.expect(re.escape('Argv: ' + repr(argv)))
107
child.expect('goodbye')
109
def test_no_such_script(self):
111
msg = os.strerror(errno.ENOENT) # 'No such file or directory'
112
child = self.spawn(['xxx-no-such-file-xxx'])
113
child.expect(re.escape(msg))
115
def test_option_i(self):
116
argv = [demo_script, 'foo', 'bar']
117
child = self.spawn(['-i'] + argv)
118
idx = child.expect(['hello', re.escape(banner)])
119
assert idx == 0 # no banner
120
child.expect(re.escape('File: ' + demo_script))
121
child.expect(re.escape('Argv: ' + repr(argv)))
122
child.expect('goodbye')
123
idx = child.expect(['>>> ', re.escape(banner)])
124
assert idx == 0 # prompt, but still no banner
125
child.sendline('myvalue * 102')
127
child.sendline('__name__')
128
child.expect('__main__')
130
def test_option_i_crashing(self):
131
argv = [crashing_demo_script, 'foo', 'bar']
132
child = self.spawn(['-i'] + argv)
133
idx = child.expect(['Hello2', re.escape(banner)])
134
assert idx == 0 # no banner
135
child.expect('NameError')
136
child.sendline('myvalue2 * 1001')
137
child.expect('11011')
138
child.sendline('import sys; sys.argv')
139
child.expect(re.escape(repr(argv)))
140
child.sendline('sys.last_type.__name__')
141
child.expect(re.escape(repr('NameError')))
143
def test_options_i_c(self):
144
child = self.spawn(['-i', '-c', 'x=555'])
145
idx = child.expect(['>>> ', re.escape(banner)])
146
assert idx == 0 # prompt, but no banner
149
child.sendline('__name__')
150
child.expect('__main__')
151
child.sendline('import sys; sys.argv')
152
child.expect(re.escape("['-c']"))
154
def test_options_i_c_crashing(self):
155
child = self.spawn(['-i', '-c', 'x=666;foobar'])
156
child.expect('NameError')
157
idx = child.expect(['>>> ', re.escape(banner)])
158
assert idx == 0 # prompt, but no banner
161
child.sendline('__name__')
162
child.expect('__main__')
163
child.sendline('import sys; sys.argv')
164
child.expect(re.escape("['-c']"))
165
child.sendline('sys.last_type.__name__')
166
child.expect(re.escape(repr('NameError')))
168
def test_atexit(self):
169
child = self.spawn([])
171
child.sendline('def f(): print "foobye"')
173
child.sendline('import atexit; atexit.register(f)')
174
child.sendline('6*7')
176
# pexpect's sendeof() is confused by py.test capturing, though
177
# I think that it is a bug of sendeof()
184
child.expect('foobye')
186
def test_pythonstartup(self):
187
old = os.environ.get('PYTHONSTARTUP', '')
189
os.environ['PYTHONSTARTUP'] = crashing_demo_script
190
child = self.spawn([])
191
child.expect(re.escape(banner))
192
child.expect('Traceback')
193
child.expect('NameError')
195
child.sendline('[myvalue2]')
196
child.expect(re.escape('[11]'))
199
child = self.spawn(['-i', demo_script])
200
for line in ['hello', 'goodbye', '>>> ']:
201
idx = child.expect([line, 'Hello2'])
202
assert idx == 0 # no PYTHONSTARTUP run here
203
child.sendline('myvalue2')
204
child.expect('Traceback')
205
child.expect('NameError')
207
os.environ['PYTHONSTARTUP'] = old
209
def test_unbuffered(self):
210
line = 'import os,sys;sys.stdout.write(str(789));os.read(0,1)'
211
child = self.spawn(['-u', '-c', line])
212
child.expect('789') # expect to see it before the timeout hits
215
def test_options_i_m(self):
216
p = os.path.join(autopath.this_dir, 'mymodule.py')
217
p = os.path.abspath(p)
218
child = self.spawn(['-i',
219
'-m', 'pypy.translator.goal.test2.mymodule',
221
child.expect('mymodule running')
222
child.expect('Name: __main__')
223
child.expect(re.escape('File: ' + p))
224
child.expect(re.escape('Argv: ' + repr([p, 'extra'])))
226
#XXX the following doesn't work on CPython 2.5 either
227
#child.sendline('somevalue')
228
#child.expect(re.escape(repr("foobar")))
229
#child.expect('>>> ')
230
child.sendline('import sys')
231
child.sendline('"pypy.translator.goal.test2" in sys.modules')
233
child.sendline('"pypy.translator.goal.test2.mymodule" in sys.modules')
234
child.expect('False')
236
def test_options_u_i(self):
237
import subprocess, select, os
238
python = sys.executable
239
pipe = subprocess.Popen([python, app_main, "-u", "-i"],
240
stdout=subprocess.PIPE,
241
stdin=subprocess.PIPE,
242
stderr=subprocess.STDOUT,
243
bufsize=0, close_fds=True)
244
iwtd, owtd, ewtd = select.select([pipe.stdout], [], [], 5)
245
assert iwtd # else we timed out
246
data = os.read(pipe.stdout.fileno(), 1024)
247
assert data.startswith('Python')
249
def test_options_u_PYTHONINSPECT(self):
250
import subprocess, select, os
251
python = sys.executable
252
pipe = subprocess.Popen([python, app_main, "-u"],
253
stdout=subprocess.PIPE,
254
stdin=subprocess.PIPE,
255
stderr=subprocess.STDOUT,
256
bufsize=0, close_fds=True,
257
env={'PYTHONINSPECT': '1'})
258
iwtd, owtd, ewtd = select.select([pipe.stdout], [], [], 5)
259
assert iwtd # else we timed out
260
data = os.read(pipe.stdout.fileno(), 1024)
261
assert data.startswith('Python')
263
def test_paste_several_lines_doesnt_mess_prompt(self):
264
py.test.skip("this can only work if readline is enabled")
265
child = self.spawn([])
267
child.sendline('if 1:\n print 42\n')
268
child.expect('... print 42')
274
class TestNonInteractive:
276
def run(self, cmdline):
277
cmdline = '"%s" "%s" %s' % (sys.executable, app_main, cmdline)
278
print 'POPEN:', cmdline
279
child_in, child_out_err = os.popen4(cmdline)
281
data = child_out_err.read()
282
child_out_err.close()
283
assert banner not in data # no banner
284
assert '>>> ' not in data # no prompt
287
def test_script_on_stdin(self):
288
for extraargs, expected_argv in [
291
('- hello world', ['-', 'hello', 'world']),
293
data = self.run('%s < "%s"' % (extraargs, demo_script))
294
assert "hello" in data
295
assert "Name: __main__" in data
296
assert "File: <stdin>" in data
297
assert ("Exec: " + app_main) in data
298
assert ("Argv: " + repr(expected_argv)) in data
299
assert "goodbye" in data
301
def test_run_crashing_script(self):
302
data = self.run('"%s"' % (crashing_demo_script,))
303
assert 'Hello2' in data
304
assert 'NameError' in data
305
assert 'Goodbye2' not in data
307
def test_crashing_script_on_stdin(self):
308
data = self.run(' < "%s"' % (crashing_demo_script,))
309
assert 'Hello2' in data
310
assert 'NameError' in data
311
assert 'Goodbye2' not in data
313
def test_option_c(self):
314
data = self.run('-c "print 6**5"')
315
assert '7776' in data
317
def test_no_pythonstartup(self):
318
old = os.environ.get('PYTHONSTARTUP', '')
320
os.environ['PYTHONSTARTUP'] = crashing_demo_script
321
data = self.run('"%s"' % (demo_script,))
322
assert 'Hello2' not in data
323
data = self.run('-c pass')
324
assert 'Hello2' not in data
326
os.environ['PYTHONSTARTUP'] = old
328
def test_option_m(self):
329
p = os.path.join(autopath.this_dir, 'mymodule.py')
330
p = os.path.abspath(p)
331
data = self.run('-m pypy.translator.goal.test2.mymodule extra')
332
assert 'mymodule running' in data
333
assert 'Name: __main__' in data
334
assert ('File: ' + p) in data
335
assert ('Argv: ' + repr([p, 'extra'])) in data