1#!/usr/bin/env python
2
3'''This starts the python interpreter; captures the startup message; then gives
4the user interactive control over the session. Why? For fun...
5
6PEXPECT LICENSE
7
8    This license is approved by the OSI and FSF as GPL-compatible.
9        http://opensource.org/licenses/isc-license.txt
10
11    Copyright (c) 2012, Noah Spurrier <noah@noah.org>
12    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
13    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
14    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
15    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
16    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
18    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
21    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23'''
24
25from __future__ import absolute_import
26from __future__ import print_function
27from __future__ import unicode_literals
28
29import pexpect
30
31# Don't do this unless you like being John Malkovich
32# c = pexpect.spawnu('/usr/bin/env python ./python.py')
33
34# Note that, for Python 3 compatibility reasons, we are using spawnu and
35# importing unicode_literals (above). spawnu accepts Unicode input and
36# unicode_literals makes all string literals in this script Unicode by default.
37c = pexpect.spawnu('/usr/bin/env python')
38
39c.expect('>>>')
40print('And now for something completely different...')
41print(''.join(reversed((c.before))))
42print('Yes, it\'s python, but it\'s backwards.')
43print()
44print('Escape character is \'^]\'.')
45print(c.after, end=' ')
46c.interact()
47c.kill(1)
48print('is alive:', c.isalive())
49
50