]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/wxPython/lib/PyCrust/interpreter.py
8274e35ed14b97165c4c1073ea9018940d5a0bea
1 """PyCrust Interpreter executes Python commands."""
3 __author__
= "Patrick K. O'Brien <pobrien@orbtech.com>"
5 __version__
= "$Revision$"[11:-2]
9 from code
import InteractiveInterpreter
13 class Interpreter(InteractiveInterpreter
):
14 """PyCrust Interpreter based on code.InteractiveInterpreter."""
16 revision
= __version__
18 def __init__(self
, locals=None, rawin
=None, \
19 stdin
=sys
.stdin
, stdout
=sys
.stdout
, stderr
=sys
.stderr
):
20 """Create an interactive interpreter object."""
21 InteractiveInterpreter
.__init
__(self
, locals=locals)
27 __builtin__
.raw_input = rawin
30 'Type "copyright", "credits" or "license" for more information.'
31 self
.introText
= 'Python %s on %s%s%s' % \
32 (sys
.version
, sys
.platform
, os
.linesep
, copyright
)
35 except AttributeError:
39 except AttributeError:
42 # List of lists to support recursive push().
43 self
.commandBuffer
= []
44 self
.startupScript
= os
.environ
.get('PYTHONSTARTUP')
46 def push(self
, command
):
47 """Send command to the interpreter to be executed.
49 Because this may be called recursively, we append a new list
50 onto the commandBuffer list and then append commands into that.
51 If the passed in command is part of a multi-line command we keep
52 appending the pieces to the last list in commandBuffer until we
53 have a complete command. If not, we delete that last list."""
55 try: del self
.commandBuffer
[-1]
56 except IndexError: pass
57 if not self
.more
: self
.commandBuffer
.append([])
58 self
.commandBuffer
[-1].append(command
)
59 source
= '\n'.join(self
.commandBuffer
[-1])
60 self
.more
= self
.runsource(source
)
63 def runsource(self
, source
):
64 """Compile and run source code in the interpreter."""
65 stdin
, stdout
, stderr
= sys
.stdin
, sys
.stdout
, sys
.stderr
66 sys
.stdin
= self
.stdin
67 sys
.stdout
= self
.stdout
68 sys
.stderr
= self
.stderr
69 more
= InteractiveInterpreter
.runsource(self
, source
)
70 # If sys.std* is still what we set it to, then restore it.
71 # But, if the executed source changed sys.std*, assume it
72 # was meant to be changed and leave it. Power to the people.
73 if sys
.stdin
== self
.stdin
:
75 if sys
.stdout
== self
.stdout
:
77 if sys
.stderr
== self
.stderr
:
81 def getAutoCompleteList(self
, command
='', *args
, **kwds
):
82 """Return list of auto-completion options for a command.
84 The list of options will be based on the locals namespace."""
85 return introspect
.getAutoCompleteList(command
, self
.locals, *args
, **kwds
)
87 def getCallTip(self
, command
='', *args
, **kwds
):
88 """Return call tip text for a command.
90 The call tip information will be based on the locals namespace."""
91 return introspect
.getCallTip(command
, self
.locals, *args
, **kwds
)
94 class InterpreterAlaCarte(Interpreter
):
95 """PyCrustAlaCarte Demo Interpreter."""
97 def __init__(self
, locals, rawin
, stdin
, stdout
, stderr
, \
98 ps1
='main prompt', ps2
='continuation prompt'):
99 """Create an interactive interpreter object."""
100 Interpreter
.__init
__(self
, locals=locals, rawin
=rawin
, \
101 stdin
=stdin
, stdout
=stdout
, stderr
=stderr
)