blob: f484fb860cf0868540c3e887c1c4852124b653ea [file] [log] [blame]
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +00001#! /usr/bin/env python
2
3import os
4import sys
5import string
Guido van Rossum80d132d1998-10-16 16:12:11 +00006import getopt
Guido van Rossum5af7a721998-10-12 23:59:27 +00007import re
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +00008
9import linecache
10from code import InteractiveInterpreter
11
12from Tkinter import *
13import tkMessageBox
14
Guido van Rossum504b0bf1999-01-02 21:28:54 +000015from EditorWindow import EditorWindow, fixwordbreaks
16from FileList import FileList
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +000017from ColorDelegator import ColorDelegator
Guido van Rossum504b0bf1999-01-02 21:28:54 +000018from OutputWindow import OutputWindow
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +000019
Guido van Rossumcc0ade81998-10-19 02:26:16 +000020# We need to patch linecache.checkcache, because we don't want it
21# to throw away our <pyshell#...> entries.
22# Rather than repeating its code here, we save those entries,
23# then call the original function, and then restore the saved entries.
24def linecache_checkcache(orig_checkcache=linecache.checkcache):
25 cache = linecache.cache
26 save = {}
27 for filename in cache.keys():
28 if filename[:1] + filename[-1:] == '<>':
29 save[filename] = cache[filename]
30 orig_checkcache()
31 cache.update(save)
32linecache.checkcache = linecache_checkcache
33
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +000034
Guido van Rossum504b0bf1999-01-02 21:28:54 +000035# Note: <<newline-and-indent>> event is defined in AutoIndent.py
Guido van Rossumcc0ade81998-10-19 02:26:16 +000036
Guido van Rossum504b0bf1999-01-02 21:28:54 +000037#$ event <<plain-newline-and-indent>>
38#$ win <Control-j>
39#$ unix <Control-j>
40
41#$ event <<beginning-of-line>>
42#$ win <Control-a>
43#$ win <Home>
44#$ unix <Control-a>
45#$ unix <Home>
46
47#$ event <<history-next>>
48#$ win <Alt-n>
49#$ unix <Alt-n>
50
51#$ event <<history-previous>>
52#$ win <Alt-p>
53#$ unix <Alt-p>
54
55#$ event <<interrupt-execution>>
56#$ win <Control-c>
57#$ unix <Control-c>
58
59#$ event <<end-of-file>>
60#$ win <Control-d>
61#$ unix <Control-d>
62
63#$ event <<open-stack-viewer>>
64
65#$ event <<toggle-debugger>>
66
67
68class PyShellEditorWindow(EditorWindow):
69
70 # Regular text edit window when a shell is present
71 # XXX ought to merge with regular editor window
72
73 def __init__(self, *args):
74 apply(EditorWindow.__init__, (self,) + args)
75 self.text.bind("<<set-breakpoint-here>>", self.set_breakpoint_here)
76 self.text.bind("<<open-python-shell>>", self.flist.open_shell)
77
78 rmenu_specs = [
79 ("Set breakpoint here", "<<set-breakpoint-here>>"),
80 ]
81
82 def set_breakpoint_here(self, event=None):
Guido van Rossumcc0ade81998-10-19 02:26:16 +000083 if not self.flist.pyshell or not self.flist.pyshell.interp.debugger:
84 self.text.bell()
85 return
86 self.flist.pyshell.interp.debugger.set_breakpoint_here(self)
Guido van Rossum5af7a721998-10-12 23:59:27 +000087
88
89class PyShellFileList(FileList):
Guido van Rossum504b0bf1999-01-02 21:28:54 +000090
91 # File list when a shell is present
92
Guido van Rossum5af7a721998-10-12 23:59:27 +000093 EditorWindow = PyShellEditorWindow
Guido van Rossum504b0bf1999-01-02 21:28:54 +000094
Guido van Rossum5af7a721998-10-12 23:59:27 +000095 pyshell = None
96
Guido van Rossum504b0bf1999-01-02 21:28:54 +000097 def open_shell(self, event=None):
Guido van Rossum5af7a721998-10-12 23:59:27 +000098 if self.pyshell:
99 self.pyshell.wakeup()
100 else:
101 self.pyshell = PyShell(self)
102 self.pyshell.begin()
103 return self.pyshell
104
105
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +0000106class ModifiedColorDelegator(ColorDelegator):
Guido van Rossum504b0bf1999-01-02 21:28:54 +0000107
108 # Colorizer for the shell window itself
109
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +0000110 def recolorize_main(self):
111 self.tag_remove("TODO", "1.0", "iomark")
112 self.tag_add("SYNC", "1.0", "iomark")
113 ColorDelegator.recolorize_main(self)
114
Guido van Rossum504b0bf1999-01-02 21:28:54 +0000115 tagdefs = ColorDelegator.tagdefs.copy()
116
117 tagdefs.update({
118 ##"stdin": {"background": "yellow"},
119 "stdout": {"foreground": "blue"},
120 "stderr": {"foreground": "#007700"},
121 "console": {"foreground": "#770000"},
122 "ERROR": {"background": "#FF7777"},
123 None: {"foreground": "purple"}, # default
124 })
125
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +0000126
127class ModifiedInterpreter(InteractiveInterpreter):
Guido van Rossum504b0bf1999-01-02 21:28:54 +0000128
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +0000129 def __init__(self, tkconsole):
130 self.tkconsole = tkconsole
131 InteractiveInterpreter.__init__(self)
132
133 gid = 0
134
135 def runsource(self, source):
136 # Extend base class to stuff the source in the line cache
Guido van Rossumcc0ade81998-10-19 02:26:16 +0000137 filename = "<pyshell#%d>" % self.gid
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +0000138 self.gid = self.gid + 1
139 lines = string.split(source, "\n")
140 linecache.cache[filename] = len(source)+1, 0, lines, filename
141 self.more = 0
142 return InteractiveInterpreter.runsource(self, source, filename)
143
144 def showsyntaxerror(self, filename=None):
145 # Extend base class to color the offending position
146 # (instead of printing it and pointing at it with a caret)
147 text = self.tkconsole.text
148 stuff = self.unpackerror()
149 if not stuff:
150 self.tkconsole.resetoutput()
151 InteractiveInterpreter.showsyntaxerror(self, filename)
152 return
153 msg, lineno, offset, line = stuff
154 if lineno == 1:
155 pos = "iomark + %d chars" % (offset-1)
156 else:
157 pos = "iomark linestart + %d lines + %d chars" % (lineno-1,
158 offset-1)
159 text.tag_add("ERROR", pos)
160 text.see(pos)
161 char = text.get(pos)
162 if char in string.letters + string.digits + "_":
163 text.tag_add("ERROR", pos + " wordstart", pos)
164 self.tkconsole.resetoutput()
165 self.write("SyntaxError: %s\n" % str(msg))
166
167 def unpackerror(self):
168 type, value, tb = sys.exc_info()
169 ok = type == SyntaxError
170 if ok:
171 try:
172 msg, (dummy_filename, lineno, offset, line) = value
173 except:
174 ok = 0
175 if ok:
176 return msg, lineno, offset, line
177 else:
178 return None
179
180 def showtraceback(self):
181 # Extend base class method to reset output properly
182 text = self.tkconsole.text
183 self.tkconsole.resetoutput()
Guido van Rossum19563521998-10-13 16:32:05 +0000184 self.checklinecache()
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +0000185 InteractiveInterpreter.showtraceback(self)
Guido van Rossum504b0bf1999-01-02 21:28:54 +0000186
Guido van Rossum19563521998-10-13 16:32:05 +0000187 def checklinecache(self):
188 c = linecache.cache
189 for key in c.keys():
190 if key[:1] + key[-1:] != "<>":
191 del c[key]
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +0000192
Guido van Rossum35f75421998-10-13 23:51:13 +0000193 debugger = None
Guido van Rossum504b0bf1999-01-02 21:28:54 +0000194
Guido van Rossum35f75421998-10-13 23:51:13 +0000195 def setdebugger(self, debugger):
196 self.debugger = debugger
Guido van Rossum504b0bf1999-01-02 21:28:54 +0000197
Guido van Rossum35f75421998-10-13 23:51:13 +0000198 def getdebugger(self):
199 return self.debugger
200
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +0000201 def runcode(self, code):
202 # Override base class method
Guido van Rossum35f75421998-10-13 23:51:13 +0000203 debugger = self.debugger
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +0000204 try:
205 self.tkconsole.beginexecuting()
206 try:
Guido van Rossum35f75421998-10-13 23:51:13 +0000207 if debugger:
208 debugger.run(code, self.locals)
209 else:
210 exec code in self.locals
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +0000211 except SystemExit:
212 if tkMessageBox.askyesno(
213 "Exit?",
214 "Do you want to exit altogether?",
215 default="yes",
216 master=self.tkconsole.text):
217 raise
218 else:
219 self.showtraceback()
Guido van Rossum38df3c31999-01-08 15:31:07 +0000220 if self.tkconsole.jit_stack_view:
221 self.tkconsole.open_stack_viewer()
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +0000222 except:
223 self.showtraceback()
Guido van Rossum38df3c31999-01-08 15:31:07 +0000224 if self.tkconsole.jit_stack_view:
225 self.tkconsole.open_stack_viewer()
226
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +0000227 finally:
228 self.tkconsole.endexecuting()
Guido van Rossum504b0bf1999-01-02 21:28:54 +0000229
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +0000230 def write(self, s):
231 # Override base class write
232 self.tkconsole.console.write(s)
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +0000233
Guido van Rossum504b0bf1999-01-02 21:28:54 +0000234
235class PyShell(OutputWindow):
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +0000236
237 # Override classes
238 ColorDelegator = ModifiedColorDelegator
Guido van Rossum504b0bf1999-01-02 21:28:54 +0000239
Guido van Rossum5af7a721998-10-12 23:59:27 +0000240 # Override menu bar specs
241 menu_specs = PyShellEditorWindow.menu_specs[:]
Guido van Rossum504b0bf1999-01-02 21:28:54 +0000242 menu_specs.insert(len(menu_specs)-2, ("debug", "_Debug"))
243
Guido van Rossumcc0ade81998-10-19 02:26:16 +0000244 # New classes
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +0000245 from History import History
246
247 def __init__(self, flist=None):
248 self.interp = ModifiedInterpreter(self)
249 if flist is None:
250 root = Tk()
251 fixwordbreaks(root)
252 root.withdraw()
Guido van Rossum5af7a721998-10-12 23:59:27 +0000253 flist = PyShellFileList(root)
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +0000254
Guido van Rossum504b0bf1999-01-02 21:28:54 +0000255 OutputWindow.__init__(self, flist, None, None)
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +0000256
257 import __builtin__
258 __builtin__.quit = __builtin__.exit = "To exit, type Ctrl-D."
259
Guido van Rossum504b0bf1999-01-02 21:28:54 +0000260 self.auto = self.extensions["AutoIndent"] # Required extension
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +0000261 self.auto.config(prefertabs=1)
262
263 text = self.text
Guido van Rossum504b0bf1999-01-02 21:28:54 +0000264 text.configure(wrap="char")
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +0000265 text.bind("<<newline-and-indent>>", self.enter_callback)
266 text.bind("<<plain-newline-and-indent>>", self.linefeed_callback)
267 text.bind("<<interrupt-execution>>", self.cancel_callback)
268 text.bind("<<beginning-of-line>>", self.home_callback)
269 text.bind("<<end-of-file>>", self.eof_callback)
Guido van Rossum5af7a721998-10-12 23:59:27 +0000270 text.bind("<<open-stack-viewer>>", self.open_stack_viewer)
Guido van Rossum35f75421998-10-13 23:51:13 +0000271 text.bind("<<toggle-debugger>>", self.toggle_debugger)
Guido van Rossum504b0bf1999-01-02 21:28:54 +0000272 text.bind("<<open-python-shell>>", self.flist.open_shell)
Guido van Rossum38df3c31999-01-08 15:31:07 +0000273 text.bind("<<toggle-jit-stack-viewer>>", self.toggle_jit_stack_viewer)
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +0000274
275 sys.stdout = PseudoFile(self, "stdout")
Guido van Rossum3f08d401998-10-13 15:21:41 +0000276 sys.stderr = PseudoFile(self, "stderr")
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +0000277 sys.stdin = self
278 self.console = PseudoFile(self, "console")
279
280 self.history = self.History(self.text)
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +0000281
282 reading = 0
283 executing = 0
284 canceled = 0
285 endoffile = 0
Guido van Rossum504b0bf1999-01-02 21:28:54 +0000286
Guido van Rossum35f75421998-10-13 23:51:13 +0000287 def toggle_debugger(self, event=None):
288 if self.executing:
289 tkMessageBox.showerror("Don't debug now",
290 "You can only toggle the debugger when idle",
291 master=self.text)
292 return "break"
293 db = self.interp.getdebugger()
294 if db:
Guido van Rossum35e55da1998-10-14 03:43:05 +0000295 self.close_debugger()
296 else:
297 self.open_debugger()
298
Guido van Rossum38df3c31999-01-08 15:31:07 +0000299 jit_stack_view = 0
300
301 def toggle_jit_stack_viewer( self, event=None):
302 self.jit_stack_view = not self.jit_stack_view
303
Guido van Rossum35e55da1998-10-14 03:43:05 +0000304 def close_debugger(self):
305 db = self.interp.getdebugger()
306 if db:
307 self.interp.setdebugger(None)
Guido van Rossum35f75421998-10-13 23:51:13 +0000308 db.close()
309 self.resetoutput()
310 self.console.write("[DEBUG OFF]\n")
311 sys.ps1 = ">>> "
312 self.showprompt()
Guido van Rossum35e55da1998-10-14 03:43:05 +0000313
314 def open_debugger(self):
315 import Debugger
316 self.interp.setdebugger(Debugger.Debugger(self))
Guido van Rossumadfe7731998-10-16 21:09:35 +0000317 sys.ps1 = "[DEBUG ON]\n>>> "
Guido van Rossum35e55da1998-10-14 03:43:05 +0000318 self.showprompt()
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +0000319
320 def beginexecuting(self):
321 # Helper for ModifiedInterpreter
322 self.resetoutput()
323 self.executing = 1
324 self._cancel_check = self.cancel_check
325 ##sys.settrace(self._cancel_check)
326
327 def endexecuting(self):
328 # Helper for ModifiedInterpreter
329 sys.settrace(None)
330 self.executing = 0
331 self.canceled = 0
332
333 def close(self):
334 # Extend base class method
335 if self.executing:
336 # XXX Need to ask a question here
337 if not tkMessageBox.askokcancel(
338 "Cancel?",
339 "The program is still running; do you want to cancel it?",
340 default="ok",
341 master=self.text):
342 return "cancel"
343 self.canceled = 1
344 if self.reading:
345 self.top.quit()
346 return "cancel"
Guido van Rossum5af7a721998-10-12 23:59:27 +0000347 reply = PyShellEditorWindow.close(self)
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +0000348 if reply != "cancel":
Guido van Rossum5af7a721998-10-12 23:59:27 +0000349 self.flist.pyshell = None
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +0000350 # Restore std streams
351 sys.stdout = sys.__stdout__
352 sys.stderr = sys.__stderr__
353 sys.stdin = sys.__stdin__
354 # Break cycles
355 self.interp = None
356 self.console = None
357 return reply
358
359 def ispythonsource(self, filename):
360 # Override this so EditorWindow never removes the colorizer
361 return 1
362
Guido van Rossum504b0bf1999-01-02 21:28:54 +0000363 def short_title(self):
364 return "Python Shell"
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +0000365
Guido van Rossum5af7a721998-10-12 23:59:27 +0000366 def begin(self):
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +0000367 self.resetoutput()
368 self.write("Python %s on %s\n%s\n" %
369 (sys.version, sys.platform, sys.copyright))
370 try:
371 sys.ps1
372 except AttributeError:
373 sys.ps1 = ">>> "
374 self.showprompt()
375 import Tkinter
376 Tkinter._default_root = None
Guido van Rossum504b0bf1999-01-02 21:28:54 +0000377
Guido van Rossum5af7a721998-10-12 23:59:27 +0000378 def interact(self):
379 self.begin()
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +0000380 self.top.mainloop()
381
382 def readline(self):
383 save = self.reading
384 try:
385 self.reading = 1
386 self.top.mainloop()
387 finally:
388 self.reading = save
389 line = self.text.get("iomark", "end-1c")
390 self.resetoutput()
391 if self.canceled:
392 self.canceled = 0
393 raise KeyboardInterrupt
394 if self.endoffile:
395 self.endoffile = 0
396 return ""
397 return line
398
399 def cancel_callback(self, event):
400 try:
401 if self.text.compare("sel.first", "!=", "sel.last"):
402 return # Active selection -- always use default binding
403 except:
404 pass
405 if not (self.executing or self.reading):
406 self.resetoutput()
407 self.write("KeyboardInterrupt\n")
408 self.showprompt()
409 return "break"
410 self.endoffile = 0
411 self.canceled = 1
412 if self.reading:
413 self.top.quit()
414 return "break"
415
416 def eof_callback(self, event):
417 if self.executing and not self.reading:
418 return # Let the default binding (delete next char) take over
419 if not (self.text.compare("iomark", "==", "insert") and
420 self.text.compare("insert", "==", "end-1c")):
421 return # Let the default binding (delete next char) take over
422 if not self.executing:
423## if not tkMessageBox.askokcancel(
424## "Exit?",
425## "Are you sure you want to exit?",
426## default="ok", master=self.text):
427## return "break"
428 self.resetoutput()
429 self.close()
430 else:
431 self.canceled = 0
432 self.endoffile = 1
433 self.top.quit()
434 return "break"
435
436 def home_callback(self, event):
437 if event.state != 0 and event.keysym == "Home":
438 return # <Modifier-Home>; fall back to class binding
439 if self.text.compare("iomark", "<=", "insert") and \
440 self.text.compare("insert linestart", "<=", "iomark"):
441 self.text.mark_set("insert", "iomark")
442 self.text.tag_remove("sel", "1.0", "end")
443 self.text.see("insert")
444 return "break"
445
446 def linefeed_callback(self, event):
447 # Insert a linefeed without entering anything (still autoindented)
448 if self.reading:
449 self.text.insert("insert", "\n")
450 self.text.see("insert")
451 else:
Guido van Rossum504b0bf1999-01-02 21:28:54 +0000452 self.auto.auto_indent(event)
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +0000453 return "break"
454
455 def enter_callback(self, event):
456 if self.executing and not self.reading:
457 return # Let the default binding (insert '\n') take over
458 # If some text is selected, recall the selection
Guido van Rossum4650df91998-10-13 14:41:27 +0000459 # (but only if this before the I/O mark)
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +0000460 try:
461 sel = self.text.get("sel.first", "sel.last")
462 if sel:
Guido van Rossum504b0bf1999-01-02 21:28:54 +0000463 if self.text.compare("sel.last", "<=", "iomark"):
Guido van Rossum4650df91998-10-13 14:41:27 +0000464 self.recall(sel)
465 return "break"
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +0000466 except:
467 pass
468 # If we're strictly before the line containing iomark, recall
469 # the current line, less a leading prompt, less leading or
470 # trailing whitespace
471 if self.text.compare("insert", "<", "iomark linestart"):
Guido van Rossum4650df91998-10-13 14:41:27 +0000472 # Check if there's a relevant stdin range -- if so, use it
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +0000473 prev = self.text.tag_prevrange("stdin", "insert")
474 if prev and self.text.compare("insert", "<", prev[1]):
475 self.recall(self.text.get(prev[0], prev[1]))
476 return "break"
477 next = self.text.tag_nextrange("stdin", "insert")
478 if next and self.text.compare("insert lineend", ">=", next[0]):
479 self.recall(self.text.get(next[0], next[1]))
480 return "break"
481 # No stdin mark -- just get the current line
482 self.recall(self.text.get("insert linestart", "insert lineend"))
483 return "break"
Guido van Rossum4650df91998-10-13 14:41:27 +0000484 # If we're in the current input before its last line,
485 # insert a newline right at the insert point
486 if self.text.compare("insert", "<", "end-1c linestart"):
Guido van Rossum504b0bf1999-01-02 21:28:54 +0000487 self.auto.auto_indent(event)
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +0000488 return "break"
Guido van Rossum4650df91998-10-13 14:41:27 +0000489 # We're in the last line; append a newline and submit it
490 self.text.mark_set("insert", "end-1c")
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +0000491 if self.reading:
492 self.text.insert("insert", "\n")
493 self.text.see("insert")
494 else:
Guido van Rossum504b0bf1999-01-02 21:28:54 +0000495 self.auto.auto_indent(event)
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +0000496 self.text.tag_add("stdin", "iomark", "end-1c")
497 self.text.update_idletasks()
498 if self.reading:
499 self.top.quit() # Break out of recursive mainloop() in raw_input()
500 else:
501 self.runit()
502 return "break"
503
504 def recall(self, s):
505 if self.history:
506 self.history.recall(s)
507
508 def runit(self):
509 line = self.text.get("iomark", "end-1c")
510 # Strip off last newline and surrounding whitespace.
511 # (To allow you to hit return twice to end a statement.)
512 i = len(line)
513 while i > 0 and line[i-1] in " \t":
514 i = i-1
515 if i > 0 and line[i-1] == "\n":
516 i = i-1
517 while i > 0 and line[i-1] in " \t":
518 i = i-1
519 line = line[:i]
520 more = self.interp.runsource(line)
521 if not more:
522 self.showprompt()
523
524 def cancel_check(self, frame, what, args,
525 dooneevent=tkinter.dooneevent,
526 dontwait=tkinter.DONT_WAIT):
527 # Hack -- use the debugger hooks to be able to handle events
528 # and interrupt execution at any time.
529 # This slows execution down quite a bit, so you may want to
530 # disable this (by not calling settrace() in runcode() above)
531 # for full-bore (uninterruptable) speed.
532 # XXX This should become a user option.
533 if self.canceled:
534 return
535 dooneevent(dontwait)
536 if self.canceled:
537 self.canceled = 0
538 raise KeyboardInterrupt
539 return self._cancel_check
Guido van Rossum504b0bf1999-01-02 21:28:54 +0000540
Guido van Rossum5af7a721998-10-12 23:59:27 +0000541 def open_stack_viewer(self, event=None):
542 try:
543 sys.last_traceback
544 except:
545 tkMessageBox.showerror("No stack trace",
546 "There is no stack trace yet.\n"
547 "(sys.last_traceback is not defined)",
548 master=self.text)
549 return
Guido van Rossum80d132d1998-10-16 16:12:11 +0000550 from StackViewer import StackBrowser
551 sv = StackBrowser(self.root, self.flist)
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +0000552
553 def showprompt(self):
554 self.resetoutput()
555 try:
556 s = str(sys.ps1)
557 except:
558 s = ""
559 self.console.write(s)
560 self.text.mark_set("insert", "end-1c")
561
562 def resetoutput(self):
563 source = self.text.get("iomark", "end-1c")
564 if self.history:
565 self.history.history_store(source)
566 if self.text.get("end-2c") != "\n":
567 self.text.insert("end-1c", "\n")
568 self.text.mark_set("iomark", "end-1c")
569 sys.stdout.softspace = 0
570
Guido van Rossum504b0bf1999-01-02 21:28:54 +0000571 def write(self, s, tags=()):
572 self.text.mark_gravity("iomark", "right")
573 OutputWindow.write(self, s, tags, "iomark")
574 self.text.mark_gravity("iomark", "left")
575 if self.canceled:
576 self.canceled = 0
577 raise KeyboardInterrupt
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +0000578
579class PseudoFile:
580
Guido van Rossum504b0bf1999-01-02 21:28:54 +0000581 def __init__(self, shell, tags):
582 self.shell = shell
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +0000583 self.tags = tags
584
585 def write(self, s):
Guido van Rossum504b0bf1999-01-02 21:28:54 +0000586 self.shell.write(s, self.tags)
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +0000587
588 def writelines(self, l):
589 map(self.write, l)
590
591
592def main():
Guido van Rossum80d132d1998-10-16 16:12:11 +0000593 debug = 0
594 try:
595 opts, args = getopt.getopt(sys.argv[1:], "d")
596 except getopt.error, msg:
597 sys.stderr.write("Error: %s\n" % str(msg))
598 sys.exit(2)
599 for o, a in opts:
600 if o == "-d":
601 debug = 1
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +0000602 global flist, root
603 root = Tk()
604 fixwordbreaks(root)
605 root.withdraw()
Guido van Rossum5af7a721998-10-12 23:59:27 +0000606 flist = PyShellFileList(root)
Guido van Rossum80d132d1998-10-16 16:12:11 +0000607 if args:
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +0000608 for filename in sys.argv[1:]:
609 flist.open(filename)
610 t = PyShell(flist)
Guido van Rossum5af7a721998-10-12 23:59:27 +0000611 flist.pyshell = t
612 t.begin()
Guido van Rossum80d132d1998-10-16 16:12:11 +0000613 if debug:
614 t.open_debugger()
Guido van Rossum5af7a721998-10-12 23:59:27 +0000615 root.mainloop()
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +0000616
617if __name__ == "__main__":
618 main()