-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathconsole.py
More file actions
74 lines (62 loc) · 2 KB
/
console.py
File metadata and controls
74 lines (62 loc) · 2 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
# This is a BIG BIG BIG security hole.
#
# It allows to execute arbitrary python code from inside the MUD.
# Guard the efun 'python_console' very tightly or better do
# not use this in production MUDs.
#
# This module can be used together with the python.c LPC program,
# that will offer a python console in the MUD.
import ldmud, code, io, sys
consoles = {}
class LDMudConsole(code.InteractiveConsole):
def __init__(self, showbanner = True):
code.InteractiveConsole.__init__(self)
try:
sys.ps1
except AttributeError:
sys.ps1 = ">>> "
try:
sys.ps2
except AttributeError:
sys.ps2 = "... "
self.output = io.StringIO()
self.write = self.output.write
if showbanner:
self.write("Python %s on %s\nType \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n" %
(sys.version, sys.platform,))
def input(self, line):
oldout = sys.stdout
olderr = sys.stderr
sys.stdout = self.output
sys.stderr = self.output
try:
more = self.push(line)
if more:
prompt = sys.ps2
else:
prompt = sys.ps1
except SystemExit:
prompt = None
except:
self.showtraceback()
prompt = sys.ps1
finally:
sys.stdout = oldout
sys.stderr = olderr
text = self.output.getvalue()
self.output.seek(0)
self.output.truncate(0)
return (text, prompt,)
def console(line = ""):
ob = ldmud.efuns.this_object()
obname = ldmud.efuns.object_name(ob)
if obname in consoles:
console = consoles[obname]
else:
console = LDMudConsole(len(line) == 0)
consoles[obname] = console
(output, prompt) = console.input(line)
if prompt is None:
del consoles[obname]
return ldmud.Array((output, prompt,))
ldmud.register_efun("python_console", console)