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