]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/py/interpreter.py
1 """Interpreter executes Python commands."""
3 __author__
= "Patrick K. O'Brien <pobrien@orbtech.com>"
5 __revision__
= "$Revision$"[11:-2]
9 from code
import InteractiveInterpreter
20 class Interpreter(InteractiveInterpreter
):
21 """Interpreter based on code.InteractiveInterpreter."""
23 revision
= __revision__
25 def __init__(self
, locals=None, rawin
=None,
26 stdin
=sys
.stdin
, stdout
=sys
.stdout
, stderr
=sys
.stderr
):
27 """Create an interactive interpreter object."""
28 InteractiveInterpreter
.__init
__(self
, locals=locals)
34 __builtin__
.raw_input = rawin
36 copyright
= 'Type "help", "copyright", "credits" or "license"'
37 copyright
+= ' for more information.'
38 self
.introText
= 'Python %s on %s%s%s' % \
39 (sys
.version
, sys
.platform
, os
.linesep
, copyright
)
42 except AttributeError:
46 except AttributeError:
49 # List of lists to support recursive push().
50 self
.commandBuffer
= []
51 self
.startupScript
= os
.environ
.get('PYTHONSTARTUP')
53 def push(self
, command
):
54 """Send command to the interpreter to be executed.
56 Because this may be called recursively, we append a new list
57 onto the commandBuffer list and then append commands into
58 that. If the passed in command is part of a multi-line
59 command we keep appending the pieces to the last list in
60 commandBuffer until we have a complete command. If not, we
61 delete that last list."""
62 command
= str(command
) # In case the command is unicode.
64 try: del self
.commandBuffer
[-1]
65 except IndexError: pass
66 if not self
.more
: self
.commandBuffer
.append([])
67 self
.commandBuffer
[-1].append(command
)
68 source
= '\n'.join(self
.commandBuffer
[-1])
69 more
= self
.more
= self
.runsource(source
)
70 dispatcher
.send(signal
='Interpreter.push', sender
=self
,
71 command
=command
, more
=more
, source
=source
)
74 def runsource(self
, source
):
75 """Compile and run source code in the interpreter."""
76 stdin
, stdout
, stderr
= sys
.stdin
, sys
.stdout
, sys
.stderr
77 sys
.stdin
, sys
.stdout
, sys
.stderr
= \
78 self
.stdin
, self
.stdout
, self
.stderr
79 more
= InteractiveInterpreter
.runsource(self
, source
)
80 # If sys.std* is still what we set it to, then restore it.
81 # But, if the executed source changed sys.std*, assume it was
82 # meant to be changed and leave it. Power to the people.
83 if sys
.stdin
== self
.stdin
:
85 if sys
.stdout
== self
.stdout
:
87 if sys
.stderr
== self
.stderr
:
91 def getAutoCompleteKeys(self
):
92 """Return list of auto-completion keycodes."""
95 def getAutoCompleteList(self
, command
='', *args
, **kwds
):
96 """Return list of auto-completion options for a command.
98 The list of options will be based on the locals namespace."""
99 stdin
, stdout
, stderr
= sys
.stdin
, sys
.stdout
, sys
.stderr
100 sys
.stdin
, sys
.stdout
, sys
.stderr
= \
101 self
.stdin
, self
.stdout
, self
.stderr
102 l
= introspect
.getAutoCompleteList(command
, self
.locals,
104 sys
.stdin
, sys
.stdout
, sys
.stderr
= stdin
, stdout
, stderr
107 def getCallTip(self
, command
='', *args
, **kwds
):
108 """Return call tip text for a command.
110 Call tip information will be based on the locals namespace."""
111 return introspect
.getCallTip(command
, self
.locals, *args
, **kwds
)
114 class InterpreterAlaCarte(Interpreter
):
115 """Demo Interpreter."""
117 def __init__(self
, locals, rawin
, stdin
, stdout
, stderr
,
118 ps1
='main prompt', ps2
='continuation prompt'):
119 """Create an interactive interpreter object."""
120 Interpreter
.__init
__(self
, locals=locals, rawin
=rawin
,
121 stdin
=stdin
, stdout
=stdout
, stderr
=stderr
)