Lines 1-25
Link Here
|
1 |
#!/usr/bin/python |
1 |
# |
2 |
# ^ for Python syntax highlighting |
2 |
# Expand-functions in this modude mostly for demonstration |
|
|
3 |
# than for daily use... |
4 |
# |
5 |
|
6 |
try: |
7 |
import builtins as interpreter |
8 |
except ImportError: |
9 |
import __builtin__ as interpreter |
10 |
|
11 |
try: |
12 |
from io import StringIO |
13 |
except: |
14 |
from cStringIO import StringIO |
3 |
|
15 |
|
4 |
import __builtin__ |
|
|
5 |
import sys |
16 |
import sys |
6 |
from cStringIO import StringIO |
|
|
7 |
|
17 |
|
8 |
def eval(s): |
18 |
def eval(s): |
9 |
# get automatic conversion to unicode or (that failing) |
19 |
# get automatic conversion to unicode or (that failing) |
10 |
# a string representation |
20 |
# a string representation |
11 |
return __builtin__.eval(s) |
21 |
return interpreter.eval(s) |
12 |
|
22 |
|
13 |
def python(s): |
23 |
def python(s): |
14 |
# execute a block. sys.stdout is written to the document |
24 |
# execute a block. sys.stdout is written to the document |
15 |
old = sys.stdout |
25 |
old = sys.stdout |
16 |
io = StringIO() |
26 |
io = StringIO() |
17 |
sys.stdout = io |
27 |
sys.stdout = io |
18 |
try: |
28 |
try: |
19 |
exec s |
29 |
exec(s) |
20 |
finally: |
30 |
finally: |
21 |
sys.stdout = old |
31 |
sys.stdout = old |
22 |
return io.getvalue().strip() # should we always strip? |
32 |
return io.getvalue().strip() # should we always strip? |
23 |
|
33 |
|
24 |
def test(s): |
34 |
def test(s): |
25 |
raise ValueError(s) |
35 |
raise ValueError(s) |
|
|
36 |
|
37 |
# kate: hl python; indent-width 4; |