David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 3 | import os |
Kurt B. Kaiser | 6e44cc2 | 2002-11-30 06:18:00 +0000 | [diff] [blame] | 4 | import os.path |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 5 | import sys |
| 6 | import string |
| 7 | import getopt |
| 8 | import re |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 9 | import socket |
| 10 | import time |
Kurt B. Kaiser | 003091c | 2003-02-17 18:57:16 +0000 | [diff] [blame] | 11 | import threading |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 12 | import traceback |
Kurt B. Kaiser | 6283398 | 2002-09-18 17:07:05 +0000 | [diff] [blame] | 13 | import types |
Ronald Oussoren | 19302d9 | 2006-06-11 14:33:36 +0000 | [diff] [blame^] | 14 | import macosxSupport |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 15 | |
| 16 | import linecache |
| 17 | from code import InteractiveInterpreter |
| 18 | |
Kurt B. Kaiser | 2303b1c | 2003-11-24 05:26:16 +0000 | [diff] [blame] | 19 | try: |
| 20 | from Tkinter import * |
| 21 | except ImportError: |
| 22 | print>>sys.__stderr__, "** IDLE can't import Tkinter. " \ |
| 23 | "Your Python may not be configured for Tk. **" |
| 24 | sys.exit(1) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 25 | import tkMessageBox |
| 26 | |
| 27 | from EditorWindow import EditorWindow, fixwordbreaks |
| 28 | from FileList import FileList |
| 29 | from ColorDelegator import ColorDelegator |
| 30 | from UndoDelegator import UndoDelegator |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 31 | from OutputWindow import OutputWindow |
Steven M. Gava | 9930061 | 2001-11-04 07:03:08 +0000 | [diff] [blame] | 32 | from configHandler import idleConf |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 33 | import idlever |
| 34 | |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 35 | import rpc |
Kurt B. Kaiser | 7f38ec0 | 2003-05-15 03:19:42 +0000 | [diff] [blame] | 36 | import Debugger |
Kurt B. Kaiser | ffd3a42 | 2002-06-26 02:32:09 +0000 | [diff] [blame] | 37 | import RemoteDebugger |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 38 | |
Kurt B. Kaiser | b976419 | 2002-09-23 04:10:37 +0000 | [diff] [blame] | 39 | IDENTCHARS = string.ascii_letters + string.digits + "_" |
Kurt B. Kaiser | 24d7e0c | 2003-06-05 23:51:29 +0000 | [diff] [blame] | 40 | LOCALHOST = '127.0.0.1' |
Kurt B. Kaiser | b976419 | 2002-09-23 04:10:37 +0000 | [diff] [blame] | 41 | |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 42 | try: |
| 43 | from signal import SIGTERM |
| 44 | except ImportError: |
| 45 | SIGTERM = 15 |
| 46 | |
Kurt B. Kaiser | 49a5fe1 | 2004-07-04 01:25:56 +0000 | [diff] [blame] | 47 | # Override warnings module to write to warning_stream. Initialize to send IDLE |
| 48 | # internal warnings to the console. ScriptBinding.check_syntax() will |
| 49 | # temporarily redirect the stream to the shell window to display warnings when |
| 50 | # checking user's code. |
| 51 | global warning_stream |
| 52 | warning_stream = sys.__stderr__ |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 53 | try: |
| 54 | import warnings |
| 55 | except ImportError: |
| 56 | pass |
| 57 | else: |
| 58 | def idle_showwarning(message, category, filename, lineno): |
Kurt B. Kaiser | 49a5fe1 | 2004-07-04 01:25:56 +0000 | [diff] [blame] | 59 | file = warning_stream |
| 60 | try: |
| 61 | file.write(warnings.formatwarning(message, category, filename, lineno)) |
| 62 | except IOError: |
| 63 | pass ## file (probably __stderr__) is invalid, warning dropped. |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 64 | warnings.showwarning = idle_showwarning |
Kurt B. Kaiser | 49a5fe1 | 2004-07-04 01:25:56 +0000 | [diff] [blame] | 65 | def idle_formatwarning(message, category, filename, lineno): |
| 66 | """Format warnings the IDLE way""" |
| 67 | s = "\nWarning (from warnings module):\n" |
| 68 | s += ' File \"%s\", line %s\n' % (filename, lineno) |
| 69 | line = linecache.getline(filename, lineno).strip() |
| 70 | if line: |
| 71 | s += " %s\n" % line |
| 72 | s += "%s: %s\n>>> " % (category.__name__, message) |
| 73 | return s |
| 74 | warnings.formatwarning = idle_formatwarning |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 75 | |
Kurt B. Kaiser | f7a8899 | 2004-11-13 21:05:58 +0000 | [diff] [blame] | 76 | def extended_linecache_checkcache(filename=None, |
| 77 | orig_checkcache=linecache.checkcache): |
Kurt B. Kaiser | 45186c4 | 2002-10-23 04:48:08 +0000 | [diff] [blame] | 78 | """Extend linecache.checkcache to preserve the <pyshell#...> entries |
| 79 | |
Kurt B. Kaiser | f7a8899 | 2004-11-13 21:05:58 +0000 | [diff] [blame] | 80 | Rather than repeating the linecache code, patch it to save the |
| 81 | <pyshell#...> entries, call the original linecache.checkcache() |
| 82 | (which destroys them), and then restore the saved entries. |
| 83 | |
| 84 | orig_checkcache is bound at definition time to the original |
| 85 | method, allowing it to be patched. |
Kurt B. Kaiser | 45186c4 | 2002-10-23 04:48:08 +0000 | [diff] [blame] | 86 | |
| 87 | """ |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 88 | cache = linecache.cache |
| 89 | save = {} |
| 90 | for filename in cache.keys(): |
| 91 | if filename[:1] + filename[-1:] == '<>': |
| 92 | save[filename] = cache[filename] |
| 93 | orig_checkcache() |
| 94 | cache.update(save) |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 95 | |
Kurt B. Kaiser | 8188559 | 2002-11-29 22:10:53 +0000 | [diff] [blame] | 96 | # Patch linecache.checkcache(): |
| 97 | linecache.checkcache = extended_linecache_checkcache |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 98 | |
Kurt B. Kaiser | 45186c4 | 2002-10-23 04:48:08 +0000 | [diff] [blame] | 99 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 100 | class PyShellEditorWindow(EditorWindow): |
Kurt B. Kaiser | 183403a | 2004-08-22 05:14:32 +0000 | [diff] [blame] | 101 | "Regular text edit window in IDLE, supports breakpoints" |
Kurt B. Kaiser | 45186c4 | 2002-10-23 04:48:08 +0000 | [diff] [blame] | 102 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 103 | def __init__(self, *args): |
Kurt B. Kaiser | 45186c4 | 2002-10-23 04:48:08 +0000 | [diff] [blame] | 104 | self.breakpoints = [] |
Raymond Hettinger | 931237e | 2003-07-09 18:48:24 +0000 | [diff] [blame] | 105 | EditorWindow.__init__(self, *args) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 106 | self.text.bind("<<set-breakpoint-here>>", self.set_breakpoint_here) |
Kurt B. Kaiser | 45186c4 | 2002-10-23 04:48:08 +0000 | [diff] [blame] | 107 | self.text.bind("<<clear-breakpoint-here>>", self.clear_breakpoint_here) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 108 | self.text.bind("<<open-python-shell>>", self.flist.open_shell) |
| 109 | |
Kurt B. Kaiser | bfed346 | 2002-12-14 04:38:51 +0000 | [diff] [blame] | 110 | self.breakpointPath = os.path.join(idleConf.GetUserCfgDir(), |
| 111 | 'breakpoints.lst') |
Chui Tey | a2adb0f | 2002-11-04 22:14:54 +0000 | [diff] [blame] | 112 | # whenever a file is changed, restore breakpoints |
| 113 | if self.io.filename: self.restore_file_breaks() |
Kurt B. Kaiser | bfed346 | 2002-12-14 04:38:51 +0000 | [diff] [blame] | 114 | def filename_changed_hook(old_hook=self.io.filename_change_hook, |
| 115 | self=self): |
Chui Tey | a2adb0f | 2002-11-04 22:14:54 +0000 | [diff] [blame] | 116 | self.restore_file_breaks() |
| 117 | old_hook() |
| 118 | self.io.set_filename_change_hook(filename_changed_hook) |
| 119 | |
Kurt B. Kaiser | 45186c4 | 2002-10-23 04:48:08 +0000 | [diff] [blame] | 120 | rmenu_specs = [("Set Breakpoint", "<<set-breakpoint-here>>"), |
| 121 | ("Clear Breakpoint", "<<clear-breakpoint-here>>")] |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 122 | |
Chui Tey | a2adb0f | 2002-11-04 22:14:54 +0000 | [diff] [blame] | 123 | def set_breakpoint(self, lineno): |
| 124 | text = self.text |
| 125 | filename = self.io.filename |
| 126 | text.tag_add("BREAK", "%d.0" % lineno, "%d.0" % (lineno+1)) |
| 127 | try: |
| 128 | i = self.breakpoints.index(lineno) |
| 129 | except ValueError: # only add if missing, i.e. do once |
| 130 | self.breakpoints.append(lineno) |
| 131 | try: # update the subprocess debugger |
| 132 | debug = self.flist.pyshell.interp.debugger |
| 133 | debug.set_breakpoint_here(filename, lineno) |
| 134 | except: # but debugger may not be active right now.... |
| 135 | pass |
| 136 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 137 | def set_breakpoint_here(self, event=None): |
Kurt B. Kaiser | 45186c4 | 2002-10-23 04:48:08 +0000 | [diff] [blame] | 138 | text = self.text |
| 139 | filename = self.io.filename |
| 140 | if not filename: |
| 141 | text.bell() |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 142 | return |
Kurt B. Kaiser | 45186c4 | 2002-10-23 04:48:08 +0000 | [diff] [blame] | 143 | lineno = int(float(text.index("insert"))) |
Chui Tey | a2adb0f | 2002-11-04 22:14:54 +0000 | [diff] [blame] | 144 | self.set_breakpoint(lineno) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 145 | |
Kurt B. Kaiser | 669f4c3 | 2002-06-20 04:01:47 +0000 | [diff] [blame] | 146 | def clear_breakpoint_here(self, event=None): |
Kurt B. Kaiser | 45186c4 | 2002-10-23 04:48:08 +0000 | [diff] [blame] | 147 | text = self.text |
| 148 | filename = self.io.filename |
| 149 | if not filename: |
| 150 | text.bell() |
Kurt B. Kaiser | 669f4c3 | 2002-06-20 04:01:47 +0000 | [diff] [blame] | 151 | return |
Kurt B. Kaiser | 45186c4 | 2002-10-23 04:48:08 +0000 | [diff] [blame] | 152 | lineno = int(float(text.index("insert"))) |
| 153 | try: |
| 154 | self.breakpoints.remove(lineno) |
| 155 | except: |
| 156 | pass |
| 157 | text.tag_remove("BREAK", "insert linestart",\ |
| 158 | "insert lineend +1char") |
| 159 | try: |
| 160 | debug = self.flist.pyshell.interp.debugger |
| 161 | debug.clear_breakpoint_here(filename, lineno) |
| 162 | except: |
| 163 | pass |
| 164 | |
| 165 | def clear_file_breaks(self): |
| 166 | if self.breakpoints: |
| 167 | text = self.text |
| 168 | filename = self.io.filename |
| 169 | if not filename: |
| 170 | text.bell() |
| 171 | return |
| 172 | self.breakpoints = [] |
| 173 | text.tag_remove("BREAK", "1.0", END) |
| 174 | try: |
| 175 | debug = self.flist.pyshell.interp.debugger |
| 176 | debug.clear_file_breaks(filename) |
| 177 | except: |
| 178 | pass |
| 179 | |
Chui Tey | a2adb0f | 2002-11-04 22:14:54 +0000 | [diff] [blame] | 180 | def store_file_breaks(self): |
Kurt B. Kaiser | bfed346 | 2002-12-14 04:38:51 +0000 | [diff] [blame] | 181 | "Save breakpoints when file is saved" |
| 182 | # XXX 13 Dec 2002 KBK Currently the file must be saved before it can |
| 183 | # be run. The breaks are saved at that time. If we introduce |
| 184 | # a temporary file save feature the save breaks functionality |
| 185 | # needs to be re-verified, since the breaks at the time the |
| 186 | # temp file is created may differ from the breaks at the last |
Kurt B. Kaiser | 7f38ec0 | 2003-05-15 03:19:42 +0000 | [diff] [blame] | 187 | # permanent save of the file. Currently, a break introduced |
| 188 | # after a save will be effective, but not persistent. |
| 189 | # This is necessary to keep the saved breaks synched with the |
| 190 | # saved file. |
Kurt B. Kaiser | bfed346 | 2002-12-14 04:38:51 +0000 | [diff] [blame] | 191 | # |
| 192 | # Breakpoints are set as tagged ranges in the text. Certain |
| 193 | # kinds of edits cause these ranges to be deleted: Inserting |
| 194 | # or deleting a line just before a breakpoint, and certain |
| 195 | # deletions prior to a breakpoint. These issues need to be |
| 196 | # investigated and understood. It's not clear if they are |
| 197 | # Tk issues or IDLE issues, or whether they can actually |
| 198 | # be fixed. Since a modified file has to be saved before it is |
| 199 | # run, and since self.breakpoints (from which the subprocess |
| 200 | # debugger is loaded) is updated during the save, the visible |
| 201 | # breaks stay synched with the subprocess even if one of these |
| 202 | # unexpected breakpoint deletions occurs. |
| 203 | breaks = self.breakpoints |
| 204 | filename = self.io.filename |
Chui Tey | a2adb0f | 2002-11-04 22:14:54 +0000 | [diff] [blame] | 205 | try: |
Kurt B. Kaiser | bfed346 | 2002-12-14 04:38:51 +0000 | [diff] [blame] | 206 | lines = open(self.breakpointPath,"r").readlines() |
Chui Tey | a2adb0f | 2002-11-04 22:14:54 +0000 | [diff] [blame] | 207 | except IOError: |
Kurt B. Kaiser | bfed346 | 2002-12-14 04:38:51 +0000 | [diff] [blame] | 208 | lines = [] |
| 209 | new_file = open(self.breakpointPath,"w") |
Chui Tey | a2adb0f | 2002-11-04 22:14:54 +0000 | [diff] [blame] | 210 | for line in lines: |
Kurt B. Kaiser | bfed346 | 2002-12-14 04:38:51 +0000 | [diff] [blame] | 211 | if not line.startswith(filename + '='): |
Chui Tey | a2adb0f | 2002-11-04 22:14:54 +0000 | [diff] [blame] | 212 | new_file.write(line) |
Kurt B. Kaiser | bfed346 | 2002-12-14 04:38:51 +0000 | [diff] [blame] | 213 | self.update_breakpoints() |
| 214 | breaks = self.breakpoints |
| 215 | if breaks: |
| 216 | new_file.write(filename + '=' + str(breaks) + '\n') |
Chui Tey | a2adb0f | 2002-11-04 22:14:54 +0000 | [diff] [blame] | 217 | new_file.close() |
| 218 | |
| 219 | def restore_file_breaks(self): |
| 220 | self.text.update() # this enables setting "BREAK" tags to be visible |
Kurt B. Kaiser | bfed346 | 2002-12-14 04:38:51 +0000 | [diff] [blame] | 221 | filename = self.io.filename |
| 222 | if filename is None: |
| 223 | return |
Chui Tey | 69371d6 | 2002-11-04 23:39:45 +0000 | [diff] [blame] | 224 | if os.path.isfile(self.breakpointPath): |
Kurt B. Kaiser | bfed346 | 2002-12-14 04:38:51 +0000 | [diff] [blame] | 225 | lines = open(self.breakpointPath,"r").readlines() |
Chui Tey | 69371d6 | 2002-11-04 23:39:45 +0000 | [diff] [blame] | 226 | for line in lines: |
Kurt B. Kaiser | bfed346 | 2002-12-14 04:38:51 +0000 | [diff] [blame] | 227 | if line.startswith(filename + '='): |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 228 | breakpoint_linenumbers = eval(line[len(filename)+1:]) |
Chui Tey | 69371d6 | 2002-11-04 23:39:45 +0000 | [diff] [blame] | 229 | for breakpoint_linenumber in breakpoint_linenumbers: |
| 230 | self.set_breakpoint(breakpoint_linenumber) |
Chui Tey | a2adb0f | 2002-11-04 22:14:54 +0000 | [diff] [blame] | 231 | |
Kurt B. Kaiser | bfed346 | 2002-12-14 04:38:51 +0000 | [diff] [blame] | 232 | def update_breakpoints(self): |
| 233 | "Retrieves all the breakpoints in the current window" |
Chui Tey | a2adb0f | 2002-11-04 22:14:54 +0000 | [diff] [blame] | 234 | text = self.text |
Kurt B. Kaiser | bfed346 | 2002-12-14 04:38:51 +0000 | [diff] [blame] | 235 | ranges = text.tag_ranges("BREAK") |
| 236 | linenumber_list = self.ranges_to_linenumbers(ranges) |
| 237 | self.breakpoints = linenumber_list |
| 238 | |
| 239 | def ranges_to_linenumbers(self, ranges): |
| 240 | lines = [] |
| 241 | for index in range(0, len(ranges), 2): |
| 242 | lineno = int(float(ranges[index])) |
| 243 | end = int(float(ranges[index+1])) |
| 244 | while lineno < end: |
| 245 | lines.append(lineno) |
| 246 | lineno += 1 |
| 247 | return lines |
| 248 | |
Kurt B. Kaiser | 11220fa | 2002-12-24 00:57:22 +0000 | [diff] [blame] | 249 | # XXX 13 Dec 2002 KBK Not used currently |
Kurt B. Kaiser | bfed346 | 2002-12-14 04:38:51 +0000 | [diff] [blame] | 250 | # def saved_change_hook(self): |
| 251 | # "Extend base method - clear breaks if module is modified" |
| 252 | # if not self.get_saved(): |
| 253 | # self.clear_file_breaks() |
| 254 | # EditorWindow.saved_change_hook(self) |
Kurt B. Kaiser | 45186c4 | 2002-10-23 04:48:08 +0000 | [diff] [blame] | 255 | |
| 256 | def _close(self): |
| 257 | "Extend base method - clear breaks when module is closed" |
| 258 | self.clear_file_breaks() |
| 259 | EditorWindow._close(self) |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 260 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 261 | |
| 262 | class PyShellFileList(FileList): |
Kurt B. Kaiser | 183403a | 2004-08-22 05:14:32 +0000 | [diff] [blame] | 263 | "Extend base class: IDLE supports a shell and breakpoints" |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 264 | |
Kurt B. Kaiser | 183403a | 2004-08-22 05:14:32 +0000 | [diff] [blame] | 265 | # override FileList's class variable, instances return PyShellEditorWindow |
| 266 | # instead of EditorWindow when new edit windows are created. |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 267 | EditorWindow = PyShellEditorWindow |
| 268 | |
| 269 | pyshell = None |
| 270 | |
| 271 | def open_shell(self, event=None): |
| 272 | if self.pyshell: |
Kurt B. Kaiser | 183403a | 2004-08-22 05:14:32 +0000 | [diff] [blame] | 273 | self.pyshell.top.wakeup() |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 274 | else: |
| 275 | self.pyshell = PyShell(self) |
Kurt B. Kaiser | af3eb87 | 2004-01-21 18:54:30 +0000 | [diff] [blame] | 276 | if self.pyshell: |
| 277 | if not self.pyshell.begin(): |
| 278 | return None |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 279 | return self.pyshell |
| 280 | |
| 281 | |
| 282 | class ModifiedColorDelegator(ColorDelegator): |
Kurt B. Kaiser | 83118c6 | 2002-06-24 17:03:37 +0000 | [diff] [blame] | 283 | "Extend base class: colorizer for the shell window itself" |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 284 | |
Steven M. Gava | b77d343 | 2002-03-02 07:16:21 +0000 | [diff] [blame] | 285 | def __init__(self): |
| 286 | ColorDelegator.__init__(self) |
| 287 | self.LoadTagDefs() |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 288 | |
| 289 | def recolorize_main(self): |
| 290 | self.tag_remove("TODO", "1.0", "iomark") |
| 291 | self.tag_add("SYNC", "1.0", "iomark") |
| 292 | ColorDelegator.recolorize_main(self) |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 293 | |
Steven M. Gava | b77d343 | 2002-03-02 07:16:21 +0000 | [diff] [blame] | 294 | def LoadTagDefs(self): |
| 295 | ColorDelegator.LoadTagDefs(self) |
| 296 | theme = idleConf.GetOption('main','Theme','name') |
| 297 | self.tagdefs.update({ |
| 298 | "stdin": {'background':None,'foreground':None}, |
| 299 | "stdout": idleConf.GetHighlight(theme, "stdout"), |
| 300 | "stderr": idleConf.GetHighlight(theme, "stderr"), |
| 301 | "console": idleConf.GetHighlight(theme, "console"), |
Steven M. Gava | b77d343 | 2002-03-02 07:16:21 +0000 | [diff] [blame] | 302 | None: idleConf.GetHighlight(theme, "normal"), |
| 303 | }) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 304 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 305 | class ModifiedUndoDelegator(UndoDelegator): |
Kurt B. Kaiser | 83118c6 | 2002-06-24 17:03:37 +0000 | [diff] [blame] | 306 | "Extend base class: forbid insert/delete before the I/O mark" |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 307 | |
| 308 | def insert(self, index, chars, tags=None): |
| 309 | try: |
| 310 | if self.delegate.compare(index, "<", "iomark"): |
| 311 | self.delegate.bell() |
| 312 | return |
| 313 | except TclError: |
| 314 | pass |
| 315 | UndoDelegator.insert(self, index, chars, tags) |
| 316 | |
| 317 | def delete(self, index1, index2=None): |
| 318 | try: |
| 319 | if self.delegate.compare(index1, "<", "iomark"): |
| 320 | self.delegate.bell() |
| 321 | return |
| 322 | except TclError: |
| 323 | pass |
| 324 | UndoDelegator.delete(self, index1, index2) |
| 325 | |
Kurt B. Kaiser | 67fd0ea | 2003-05-24 20:59:15 +0000 | [diff] [blame] | 326 | |
| 327 | class MyRPCClient(rpc.RPCClient): |
| 328 | |
| 329 | def handle_EOF(self): |
| 330 | "Override the base class - just re-raise EOFError" |
| 331 | raise EOFError |
| 332 | |
Kurt B. Kaiser | 8d1f11b | 2003-05-26 22:20:34 +0000 | [diff] [blame] | 333 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 334 | class ModifiedInterpreter(InteractiveInterpreter): |
| 335 | |
| 336 | def __init__(self, tkconsole): |
| 337 | self.tkconsole = tkconsole |
| 338 | locals = sys.modules['__main__'].__dict__ |
| 339 | InteractiveInterpreter.__init__(self, locals=locals) |
Kurt B. Kaiser | 94bd774 | 2001-07-14 00:13:28 +0000 | [diff] [blame] | 340 | self.save_warnings_filters = None |
Kurt B. Kaiser | 6f80594 | 2003-05-24 21:12:46 +0000 | [diff] [blame] | 341 | self.restarting = False |
Kurt B. Kaiser | 62df044 | 2003-05-28 01:47:46 +0000 | [diff] [blame] | 342 | self.subprocess_arglist = self.build_subprocess_arglist() |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 343 | |
Kurt B. Kaiser | 63857a4 | 2002-09-05 02:31:20 +0000 | [diff] [blame] | 344 | port = 8833 |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 345 | rpcclt = None |
| 346 | rpcpid = None |
| 347 | |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 348 | def spawn_subprocess(self): |
Kurt B. Kaiser | 62df044 | 2003-05-28 01:47:46 +0000 | [diff] [blame] | 349 | args = self.subprocess_arglist |
Kurt B. Kaiser | b785518 | 2003-08-14 14:54:28 +0000 | [diff] [blame] | 350 | self.rpcpid = os.spawnv(os.P_NOWAIT, sys.executable, args) |
Kurt B. Kaiser | 63857a4 | 2002-09-05 02:31:20 +0000 | [diff] [blame] | 351 | |
Tony Lownds | f53dec2 | 2002-12-20 04:24:43 +0000 | [diff] [blame] | 352 | def build_subprocess_arglist(self): |
Tony Lownds | 2398d57 | 2003-05-13 15:28:21 +0000 | [diff] [blame] | 353 | w = ['-W' + s for s in sys.warnoptions] |
| 354 | # Maybe IDLE is installed and is being accessed via sys.path, |
| 355 | # or maybe it's not installed and the idle.py script is being |
| 356 | # run from the IDLE source directory. |
Kurt B. Kaiser | 62df044 | 2003-05-28 01:47:46 +0000 | [diff] [blame] | 357 | del_exitf = idleConf.GetOption('main', 'General', 'delete-exitfunc', |
| 358 | default=False, type='bool') |
Tony Lownds | 2398d57 | 2003-05-13 15:28:21 +0000 | [diff] [blame] | 359 | if __name__ == 'idlelib.PyShell': |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 360 | command = "__import__('idlelib.run').run.main(%r)" % (del_exitf,) |
Tony Lownds | f2324b9 | 2002-09-29 00:34:10 +0000 | [diff] [blame] | 361 | else: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 362 | command = "__import__('run').main(%r)" % (del_exitf,) |
Kurt B. Kaiser | b785518 | 2003-08-14 14:54:28 +0000 | [diff] [blame] | 363 | if sys.platform[:3] == 'win' and ' ' in sys.executable: |
| 364 | # handle embedded space in path by quoting the argument |
| 365 | decorated_exec = '"%s"' % sys.executable |
| 366 | else: |
| 367 | decorated_exec = sys.executable |
| 368 | return [decorated_exec] + w + ["-c", command, str(self.port)] |
Tony Lownds | f2324b9 | 2002-09-29 00:34:10 +0000 | [diff] [blame] | 369 | |
Kurt B. Kaiser | 63857a4 | 2002-09-05 02:31:20 +0000 | [diff] [blame] | 370 | def start_subprocess(self): |
Kurt B. Kaiser | af3eb87 | 2004-01-21 18:54:30 +0000 | [diff] [blame] | 371 | # spawning first avoids passing a listening socket to the subprocess |
| 372 | self.spawn_subprocess() |
| 373 | #time.sleep(20) # test to simulate GUI not accepting connection |
Kurt B. Kaiser | 24d7e0c | 2003-06-05 23:51:29 +0000 | [diff] [blame] | 374 | addr = (LOCALHOST, self.port) |
Kurt B. Kaiser | 8dcdb77 | 2002-08-05 03:52:10 +0000 | [diff] [blame] | 375 | # Idle starts listening for connection on localhost |
Kurt B. Kaiser | 5db4843 | 2003-05-15 03:40:51 +0000 | [diff] [blame] | 376 | for i in range(3): |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 377 | time.sleep(i) |
| 378 | try: |
Kurt B. Kaiser | 67fd0ea | 2003-05-24 20:59:15 +0000 | [diff] [blame] | 379 | self.rpcclt = MyRPCClient(addr) |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 380 | break |
| 381 | except socket.error, err: |
Kurt B. Kaiser | af3eb87 | 2004-01-21 18:54:30 +0000 | [diff] [blame] | 382 | pass |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 383 | else: |
Kurt B. Kaiser | af3eb87 | 2004-01-21 18:54:30 +0000 | [diff] [blame] | 384 | self.display_port_binding_error() |
| 385 | return None |
Kurt B. Kaiser | b417936 | 2002-07-26 00:06:42 +0000 | [diff] [blame] | 386 | # Accept the connection from the Python execution server |
Kurt B. Kaiser | af3eb87 | 2004-01-21 18:54:30 +0000 | [diff] [blame] | 387 | self.rpcclt.listening_sock.settimeout(10) |
| 388 | try: |
| 389 | self.rpcclt.accept() |
| 390 | except socket.timeout, err: |
| 391 | self.display_no_subprocess_error() |
| 392 | return None |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 393 | self.rpcclt.register("stdin", self.tkconsole) |
| 394 | self.rpcclt.register("stdout", self.tkconsole.stdout) |
| 395 | self.rpcclt.register("stderr", self.tkconsole.stderr) |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 396 | self.rpcclt.register("flist", self.tkconsole.flist) |
Kurt B. Kaiser | 8cd0def | 2003-01-31 05:06:43 +0000 | [diff] [blame] | 397 | self.rpcclt.register("linecache", linecache) |
Kurt B. Kaiser | 9f36609 | 2003-06-02 01:50:19 +0000 | [diff] [blame] | 398 | self.rpcclt.register("interp", self) |
Kurt B. Kaiser | f06eed0 | 2002-12-11 04:42:04 +0000 | [diff] [blame] | 399 | self.transfer_path() |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 400 | self.poll_subprocess() |
Kurt B. Kaiser | af3eb87 | 2004-01-21 18:54:30 +0000 | [diff] [blame] | 401 | return self.rpcclt |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 402 | |
Kurt B. Kaiser | 63857a4 | 2002-09-05 02:31:20 +0000 | [diff] [blame] | 403 | def restart_subprocess(self): |
Kurt B. Kaiser | 6f80594 | 2003-05-24 21:12:46 +0000 | [diff] [blame] | 404 | if self.restarting: |
Kurt B. Kaiser | af3eb87 | 2004-01-21 18:54:30 +0000 | [diff] [blame] | 405 | return self.rpcclt |
Kurt B. Kaiser | 6f80594 | 2003-05-24 21:12:46 +0000 | [diff] [blame] | 406 | self.restarting = True |
Kurt B. Kaiser | 63857a4 | 2002-09-05 02:31:20 +0000 | [diff] [blame] | 407 | # close only the subprocess debugger |
Kurt B. Kaiser | 45186c4 | 2002-10-23 04:48:08 +0000 | [diff] [blame] | 408 | debug = self.getdebugger() |
| 409 | if debug: |
Kurt B. Kaiser | 003091c | 2003-02-17 18:57:16 +0000 | [diff] [blame] | 410 | try: |
Kurt B. Kaiser | 9ec454e | 2003-05-12 02:33:47 +0000 | [diff] [blame] | 411 | # Only close subprocess debugger, don't unregister gui_adap! |
Kurt B. Kaiser | 003091c | 2003-02-17 18:57:16 +0000 | [diff] [blame] | 412 | RemoteDebugger.close_subprocess_debugger(self.rpcclt) |
| 413 | except: |
| 414 | pass |
| 415 | # Kill subprocess, spawn a new one, accept connection. |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 416 | self.rpcclt.close() |
| 417 | self.unix_terminate() |
Kurt B. Kaiser | 7f38ec0 | 2003-05-15 03:19:42 +0000 | [diff] [blame] | 418 | console = self.tkconsole |
Kurt B. Kaiser | 6f80594 | 2003-05-24 21:12:46 +0000 | [diff] [blame] | 419 | was_executing = console.executing |
Kurt B. Kaiser | 7f38ec0 | 2003-05-15 03:19:42 +0000 | [diff] [blame] | 420 | console.executing = False |
Kurt B. Kaiser | 63857a4 | 2002-09-05 02:31:20 +0000 | [diff] [blame] | 421 | self.spawn_subprocess() |
Kurt B. Kaiser | af3eb87 | 2004-01-21 18:54:30 +0000 | [diff] [blame] | 422 | try: |
| 423 | self.rpcclt.accept() |
| 424 | except socket.timeout, err: |
| 425 | self.display_no_subprocess_error() |
| 426 | return None |
Kurt B. Kaiser | f06eed0 | 2002-12-11 04:42:04 +0000 | [diff] [blame] | 427 | self.transfer_path() |
Kurt B. Kaiser | 1061e72 | 2003-01-04 01:43:53 +0000 | [diff] [blame] | 428 | # annotate restart in shell window and mark it |
Kurt B. Kaiser | 4cc5ef5 | 2003-01-22 00:23:23 +0000 | [diff] [blame] | 429 | console.text.delete("iomark", "end-1c") |
Kurt B. Kaiser | 6f80594 | 2003-05-24 21:12:46 +0000 | [diff] [blame] | 430 | if was_executing: |
| 431 | console.write('\n') |
| 432 | console.showprompt() |
Kurt B. Kaiser | 1061e72 | 2003-01-04 01:43:53 +0000 | [diff] [blame] | 433 | halfbar = ((int(console.width) - 16) // 2) * '=' |
| 434 | console.write(halfbar + ' RESTART ' + halfbar) |
| 435 | console.text.mark_set("restart", "end-1c") |
| 436 | console.text.mark_gravity("restart", "left") |
Kurt B. Kaiser | 7f38ec0 | 2003-05-15 03:19:42 +0000 | [diff] [blame] | 437 | console.showprompt() |
Kurt B. Kaiser | 003091c | 2003-02-17 18:57:16 +0000 | [diff] [blame] | 438 | # restart subprocess debugger |
Kurt B. Kaiser | 45186c4 | 2002-10-23 04:48:08 +0000 | [diff] [blame] | 439 | if debug: |
Kurt B. Kaiser | 9ec454e | 2003-05-12 02:33:47 +0000 | [diff] [blame] | 440 | # Restarted debugger connects to current instance of debug GUI |
Kurt B. Kaiser | 63857a4 | 2002-09-05 02:31:20 +0000 | [diff] [blame] | 441 | gui = RemoteDebugger.restart_subprocess_debugger(self.rpcclt) |
Kurt B. Kaiser | 45186c4 | 2002-10-23 04:48:08 +0000 | [diff] [blame] | 442 | # reload remote debugger breakpoints for all PyShellEditWindows |
| 443 | debug.load_breakpoints() |
Kurt B. Kaiser | 6f80594 | 2003-05-24 21:12:46 +0000 | [diff] [blame] | 444 | self.restarting = False |
Kurt B. Kaiser | af3eb87 | 2004-01-21 18:54:30 +0000 | [diff] [blame] | 445 | return self.rpcclt |
Kurt B. Kaiser | 45186c4 | 2002-10-23 04:48:08 +0000 | [diff] [blame] | 446 | |
Kurt B. Kaiser | 003091c | 2003-02-17 18:57:16 +0000 | [diff] [blame] | 447 | def __request_interrupt(self): |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 448 | self.rpcclt.remotecall("exec", "interrupt_the_server", (), {}) |
Kurt B. Kaiser | 003091c | 2003-02-17 18:57:16 +0000 | [diff] [blame] | 449 | |
| 450 | def interrupt_subprocess(self): |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 451 | threading.Thread(target=self.__request_interrupt).start() |
Kurt B. Kaiser | 003091c | 2003-02-17 18:57:16 +0000 | [diff] [blame] | 452 | |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 453 | def kill_subprocess(self): |
Kurt B. Kaiser | af3eb87 | 2004-01-21 18:54:30 +0000 | [diff] [blame] | 454 | try: |
| 455 | self.rpcclt.close() |
| 456 | except AttributeError: # no socket |
| 457 | pass |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 458 | self.unix_terminate() |
| 459 | self.tkconsole.executing = False |
| 460 | self.rpcclt = None |
Kurt B. Kaiser | 11c53e2 | 2003-03-22 19:40:19 +0000 | [diff] [blame] | 461 | |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 462 | def unix_terminate(self): |
| 463 | "UNIX: make sure subprocess is terminated and collect status" |
| 464 | if hasattr(os, 'kill'): |
| 465 | try: |
| 466 | os.kill(self.rpcpid, SIGTERM) |
| 467 | except OSError: |
| 468 | # process already terminated: |
| 469 | return |
| 470 | else: |
| 471 | try: |
| 472 | os.waitpid(self.rpcpid, 0) |
| 473 | except OSError: |
| 474 | return |
Kurt B. Kaiser | 11c53e2 | 2003-03-22 19:40:19 +0000 | [diff] [blame] | 475 | |
Kurt B. Kaiser | f06eed0 | 2002-12-11 04:42:04 +0000 | [diff] [blame] | 476 | def transfer_path(self): |
| 477 | self.runcommand("""if 1: |
| 478 | import sys as _sys |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 479 | _sys.path = %r |
Kurt B. Kaiser | f06eed0 | 2002-12-11 04:42:04 +0000 | [diff] [blame] | 480 | del _sys |
Kurt B. Kaiser | b248733 | 2003-03-04 04:03:45 +0000 | [diff] [blame] | 481 | _msg = 'Use File/Exit or your end-of-file key to quit IDLE' |
| 482 | __builtins__.quit = __builtins__.exit = _msg |
| 483 | del _msg |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 484 | \n""" % (sys.path,)) |
Kurt B. Kaiser | f06eed0 | 2002-12-11 04:42:04 +0000 | [diff] [blame] | 485 | |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 486 | active_seq = None |
| 487 | |
| 488 | def poll_subprocess(self): |
| 489 | clt = self.rpcclt |
| 490 | if clt is None: |
| 491 | return |
Kurt B. Kaiser | 003091c | 2003-02-17 18:57:16 +0000 | [diff] [blame] | 492 | try: |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 493 | response = clt.pollresponse(self.active_seq, wait=0.05) |
| 494 | except (EOFError, IOError, KeyboardInterrupt): |
| 495 | # lost connection or subprocess terminated itself, restart |
| 496 | # [the KBI is from rpc.SocketIO.handle_EOF()] |
Kurt B. Kaiser | 67fd0ea | 2003-05-24 20:59:15 +0000 | [diff] [blame] | 497 | if self.tkconsole.closing: |
| 498 | return |
Kurt B. Kaiser | 003091c | 2003-02-17 18:57:16 +0000 | [diff] [blame] | 499 | response = None |
Kurt B. Kaiser | 003091c | 2003-02-17 18:57:16 +0000 | [diff] [blame] | 500 | self.restart_subprocess() |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 501 | if response: |
| 502 | self.tkconsole.resetoutput() |
| 503 | self.active_seq = None |
| 504 | how, what = response |
Kurt B. Kaiser | bc28613 | 2003-01-25 21:33:40 +0000 | [diff] [blame] | 505 | console = self.tkconsole.console |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 506 | if how == "OK": |
| 507 | if what is not None: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 508 | print >>console, repr(what) |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 509 | elif how == "EXCEPTION": |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 510 | if self.tkconsole.getvar("<<toggle-jit-stack-viewer>>"): |
| 511 | self.remote_stack_viewer() |
| 512 | elif how == "ERROR": |
Kurt B. Kaiser | 0930c43 | 2002-12-06 21:45:24 +0000 | [diff] [blame] | 513 | errmsg = "PyShell.ModifiedInterpreter: Subprocess ERROR:\n" |
| 514 | print >>sys.__stderr__, errmsg, what |
Kurt B. Kaiser | bc28613 | 2003-01-25 21:33:40 +0000 | [diff] [blame] | 515 | print >>console, errmsg, what |
| 516 | # we received a response to the currently active seq number: |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 517 | self.tkconsole.endexecuting() |
Kurt B. Kaiser | 88957d8 | 2003-05-19 23:11:51 +0000 | [diff] [blame] | 518 | # Reschedule myself |
| 519 | if not self.tkconsole.closing: |
| 520 | self.tkconsole.text.after(self.tkconsole.pollinterval, |
| 521 | self.poll_subprocess) |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 522 | |
Kurt B. Kaiser | 45186c4 | 2002-10-23 04:48:08 +0000 | [diff] [blame] | 523 | debugger = None |
| 524 | |
| 525 | def setdebugger(self, debugger): |
| 526 | self.debugger = debugger |
| 527 | |
| 528 | def getdebugger(self): |
| 529 | return self.debugger |
| 530 | |
Kurt B. Kaiser | 9f36609 | 2003-06-02 01:50:19 +0000 | [diff] [blame] | 531 | def open_remote_stack_viewer(self): |
| 532 | """Initiate the remote stack viewer from a separate thread. |
| 533 | |
| 534 | This method is called from the subprocess, and by returning from this |
| 535 | method we allow the subprocess to unblock. After a bit the shell |
| 536 | requests the subprocess to open the remote stack viewer which returns a |
| 537 | static object looking at the last exceptiopn. It is queried through |
| 538 | the RPC mechanism. |
| 539 | |
| 540 | """ |
| 541 | self.tkconsole.text.after(300, self.remote_stack_viewer) |
| 542 | return |
| 543 | |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 544 | def remote_stack_viewer(self): |
| 545 | import RemoteObjectBrowser |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 546 | oid = self.rpcclt.remotequeue("exec", "stackviewer", ("flist",), {}) |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 547 | if oid is None: |
| 548 | self.tkconsole.root.bell() |
| 549 | return |
| 550 | item = RemoteObjectBrowser.StubObjectTreeItem(self.rpcclt, oid) |
| 551 | from TreeWidget import ScrolledCanvas, TreeNode |
| 552 | top = Toplevel(self.tkconsole.root) |
Kurt B. Kaiser | 73360a3 | 2004-03-08 18:15:31 +0000 | [diff] [blame] | 553 | theme = idleConf.GetOption('main','Theme','name') |
| 554 | background = idleConf.GetHighlight(theme, 'normal')['background'] |
| 555 | sc = ScrolledCanvas(top, bg=background, highlightthickness=0) |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 556 | sc.frame.pack(expand=1, fill="both") |
| 557 | node = TreeNode(sc.canvas, None, item) |
| 558 | node.expand() |
| 559 | # XXX Should GC the remote tree when closing the window |
| 560 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 561 | gid = 0 |
| 562 | |
| 563 | def execsource(self, source): |
Kurt B. Kaiser | 83118c6 | 2002-06-24 17:03:37 +0000 | [diff] [blame] | 564 | "Like runsource() but assumes complete exec source" |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 565 | filename = self.stuffsource(source) |
| 566 | self.execfile(filename, source) |
| 567 | |
| 568 | def execfile(self, filename, source=None): |
Kurt B. Kaiser | 83118c6 | 2002-06-24 17:03:37 +0000 | [diff] [blame] | 569 | "Execute an existing file" |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 570 | if source is None: |
| 571 | source = open(filename, "r").read() |
| 572 | try: |
| 573 | code = compile(source, filename, "exec") |
| 574 | except (OverflowError, SyntaxError): |
| 575 | self.tkconsole.resetoutput() |
Kurt B. Kaiser | 7f38ec0 | 2003-05-15 03:19:42 +0000 | [diff] [blame] | 576 | tkerr = self.tkconsole.stderr |
| 577 | print>>tkerr, '*** Error in script or command!\n' |
| 578 | print>>tkerr, 'Traceback (most recent call last):' |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 579 | InteractiveInterpreter.showsyntaxerror(self, filename) |
Kurt B. Kaiser | 6e44cc2 | 2002-11-30 06:18:00 +0000 | [diff] [blame] | 580 | self.tkconsole.showprompt() |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 581 | else: |
| 582 | self.runcode(code) |
| 583 | |
| 584 | def runsource(self, source): |
Kurt B. Kaiser | 83118c6 | 2002-06-24 17:03:37 +0000 | [diff] [blame] | 585 | "Extend base class method: Stuff the source in the line cache first" |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 586 | filename = self.stuffsource(source) |
| 587 | self.more = 0 |
Kurt B. Kaiser | 94bd774 | 2001-07-14 00:13:28 +0000 | [diff] [blame] | 588 | self.save_warnings_filters = warnings.filters[:] |
| 589 | warnings.filterwarnings(action="error", category=SyntaxWarning) |
Kurt B. Kaiser | 837d15c | 2002-09-18 02:29:59 +0000 | [diff] [blame] | 590 | if isinstance(source, types.UnicodeType): |
| 591 | import IOBinding |
| 592 | try: |
| 593 | source = source.encode(IOBinding.encoding) |
| 594 | except UnicodeError: |
| 595 | self.tkconsole.resetoutput() |
| 596 | self.write("Unsupported characters in input") |
| 597 | return |
Kurt B. Kaiser | 94bd774 | 2001-07-14 00:13:28 +0000 | [diff] [blame] | 598 | try: |
Kurt B. Kaiser | 935ea9a | 2005-05-10 03:44:24 +0000 | [diff] [blame] | 599 | # InteractiveInterpreter.runsource() calls its runcode() method, |
| 600 | # which is overridden (see below) |
Kurt B. Kaiser | 94bd774 | 2001-07-14 00:13:28 +0000 | [diff] [blame] | 601 | return InteractiveInterpreter.runsource(self, source, filename) |
| 602 | finally: |
| 603 | if self.save_warnings_filters is not None: |
| 604 | warnings.filters[:] = self.save_warnings_filters |
| 605 | self.save_warnings_filters = None |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 606 | |
| 607 | def stuffsource(self, source): |
Kurt B. Kaiser | 83118c6 | 2002-06-24 17:03:37 +0000 | [diff] [blame] | 608 | "Stuff source in the filename cache" |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 609 | filename = "<pyshell#%d>" % self.gid |
| 610 | self.gid = self.gid + 1 |
Kurt B. Kaiser | 837d15c | 2002-09-18 02:29:59 +0000 | [diff] [blame] | 611 | lines = source.split("\n") |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 612 | linecache.cache[filename] = len(source)+1, 0, lines, filename |
| 613 | return filename |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 614 | |
Kurt B. Kaiser | 11659ad | 2003-05-15 23:23:21 +0000 | [diff] [blame] | 615 | def prepend_syspath(self, filename): |
| 616 | "Prepend sys.path with file's directory if not already included" |
| 617 | self.runcommand("""if 1: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 618 | _filename = %r |
Kurt B. Kaiser | 11659ad | 2003-05-15 23:23:21 +0000 | [diff] [blame] | 619 | import sys as _sys |
| 620 | from os.path import dirname as _dirname |
| 621 | _dir = _dirname(_filename) |
| 622 | if not _dir in _sys.path: |
| 623 | _sys.path.insert(0, _dir) |
| 624 | del _filename, _sys, _dirname, _dir |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 625 | \n""" % (filename,)) |
Kurt B. Kaiser | 11659ad | 2003-05-15 23:23:21 +0000 | [diff] [blame] | 626 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 627 | def showsyntaxerror(self, filename=None): |
Kurt B. Kaiser | 83118c6 | 2002-06-24 17:03:37 +0000 | [diff] [blame] | 628 | """Extend base class method: Add Colorizing |
| 629 | |
| 630 | Color the offending position instead of printing it and pointing at it |
| 631 | with a caret. |
| 632 | |
| 633 | """ |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 634 | text = self.tkconsole.text |
| 635 | stuff = self.unpackerror() |
Kurt B. Kaiser | 6e44cc2 | 2002-11-30 06:18:00 +0000 | [diff] [blame] | 636 | if stuff: |
| 637 | msg, lineno, offset, line = stuff |
| 638 | if lineno == 1: |
| 639 | pos = "iomark + %d chars" % (offset-1) |
| 640 | else: |
| 641 | pos = "iomark linestart + %d lines + %d chars" % \ |
| 642 | (lineno-1, offset-1) |
| 643 | text.tag_add("ERROR", pos) |
| 644 | text.see(pos) |
| 645 | char = text.get(pos) |
| 646 | if char and char in IDENTCHARS: |
| 647 | text.tag_add("ERROR", pos + " wordstart", pos) |
| 648 | self.tkconsole.resetoutput() |
| 649 | self.write("SyntaxError: %s\n" % str(msg)) |
| 650 | else: |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 651 | self.tkconsole.resetoutput() |
| 652 | InteractiveInterpreter.showsyntaxerror(self, filename) |
Kurt B. Kaiser | 6e44cc2 | 2002-11-30 06:18:00 +0000 | [diff] [blame] | 653 | self.tkconsole.showprompt() |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 654 | |
| 655 | def unpackerror(self): |
| 656 | type, value, tb = sys.exc_info() |
| 657 | ok = type is SyntaxError |
| 658 | if ok: |
| 659 | try: |
| 660 | msg, (dummy_filename, lineno, offset, line) = value |
Kurt B. Kaiser | bea57c6 | 2003-07-09 04:27:24 +0000 | [diff] [blame] | 661 | if not offset: |
| 662 | offset = 0 |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 663 | except: |
| 664 | ok = 0 |
| 665 | if ok: |
| 666 | return msg, lineno, offset, line |
| 667 | else: |
| 668 | return None |
| 669 | |
| 670 | def showtraceback(self): |
Kurt B. Kaiser | 83118c6 | 2002-06-24 17:03:37 +0000 | [diff] [blame] | 671 | "Extend base class method to reset output properly" |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 672 | self.tkconsole.resetoutput() |
| 673 | self.checklinecache() |
| 674 | InteractiveInterpreter.showtraceback(self) |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 675 | if self.tkconsole.getvar("<<toggle-jit-stack-viewer>>"): |
| 676 | self.tkconsole.open_stack_viewer() |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 677 | |
| 678 | def checklinecache(self): |
| 679 | c = linecache.cache |
| 680 | for key in c.keys(): |
| 681 | if key[:1] + key[-1:] != "<>": |
| 682 | del c[key] |
| 683 | |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 684 | def runcommand(self, code): |
Kurt B. Kaiser | 83118c6 | 2002-06-24 17:03:37 +0000 | [diff] [blame] | 685 | "Run the code without invoking the debugger" |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 686 | # The code better not raise an exception! |
| 687 | if self.tkconsole.executing: |
Neal Norwitz | f4c4f11 | 2002-11-30 18:49:10 +0000 | [diff] [blame] | 688 | self.display_executing_dialog() |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 689 | return 0 |
| 690 | if self.rpcclt: |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 691 | self.rpcclt.remotequeue("exec", "runcode", (code,), {}) |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 692 | else: |
| 693 | exec code in self.locals |
| 694 | return 1 |
| 695 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 696 | def runcode(self, code): |
Kurt B. Kaiser | 83118c6 | 2002-06-24 17:03:37 +0000 | [diff] [blame] | 697 | "Override base class method" |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 698 | if self.tkconsole.executing: |
Kurt B. Kaiser | 003091c | 2003-02-17 18:57:16 +0000 | [diff] [blame] | 699 | self.interp.restart_subprocess() |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 700 | self.checklinecache() |
Kurt B. Kaiser | 94bd774 | 2001-07-14 00:13:28 +0000 | [diff] [blame] | 701 | if self.save_warnings_filters is not None: |
| 702 | warnings.filters[:] = self.save_warnings_filters |
| 703 | self.save_warnings_filters = None |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 704 | debugger = self.debugger |
| 705 | try: |
Kurt B. Kaiser | 7f38ec0 | 2003-05-15 03:19:42 +0000 | [diff] [blame] | 706 | self.tkconsole.beginexecuting() |
| 707 | try: |
| 708 | if not debugger and self.rpcclt is not None: |
| 709 | self.active_seq = self.rpcclt.asyncqueue("exec", "runcode", |
| 710 | (code,), {}) |
| 711 | elif debugger: |
| 712 | debugger.run(code, self.locals) |
| 713 | else: |
| 714 | exec code in self.locals |
| 715 | except SystemExit: |
| 716 | if tkMessageBox.askyesno( |
| 717 | "Exit?", |
| 718 | "Do you want to exit altogether?", |
| 719 | default="yes", |
| 720 | master=self.tkconsole.text): |
| 721 | raise |
| 722 | else: |
| 723 | self.showtraceback() |
| 724 | except: |
Kurt B. Kaiser | 935ea9a | 2005-05-10 03:44:24 +0000 | [diff] [blame] | 725 | print>>sys.stderr, "IDLE internal error in runcode()" |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 726 | self.showtraceback() |
Kurt B. Kaiser | 7f38ec0 | 2003-05-15 03:19:42 +0000 | [diff] [blame] | 727 | finally: |
| 728 | if not use_subprocess: |
| 729 | self.tkconsole.endexecuting() |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 730 | |
| 731 | def write(self, s): |
Kurt B. Kaiser | 83118c6 | 2002-06-24 17:03:37 +0000 | [diff] [blame] | 732 | "Override base class method" |
Kurt B. Kaiser | 7f38ec0 | 2003-05-15 03:19:42 +0000 | [diff] [blame] | 733 | self.tkconsole.stderr.write(s) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 734 | |
Kurt B. Kaiser | af3eb87 | 2004-01-21 18:54:30 +0000 | [diff] [blame] | 735 | def display_port_binding_error(self): |
| 736 | tkMessageBox.showerror( |
| 737 | "Port Binding Error", |
| 738 | "IDLE can't bind TCP/IP port 8833, which is necessary to " |
| 739 | "communicate with its Python execution server. Either " |
| 740 | "no networking is installed on this computer or another " |
| 741 | "process (another IDLE?) is using the port. Run IDLE with the -n " |
| 742 | "command line switch to start without a subprocess and refer to " |
| 743 | "Help/IDLE Help 'Running without a subprocess' for further " |
| 744 | "details.", |
| 745 | master=self.tkconsole.text) |
| 746 | |
| 747 | def display_no_subprocess_error(self): |
| 748 | tkMessageBox.showerror( |
| 749 | "Subprocess Startup Error", |
| 750 | "IDLE's subprocess didn't make connection. Either IDLE can't " |
| 751 | "start a subprocess or personal firewall software is blocking " |
| 752 | "the connection.", |
| 753 | master=self.tkconsole.text) |
| 754 | |
| 755 | def display_executing_dialog(self): |
| 756 | tkMessageBox.showerror( |
| 757 | "Already executing", |
| 758 | "The Python Shell window is already executing a command; " |
| 759 | "please wait until it is finished.", |
| 760 | master=self.tkconsole.text) |
| 761 | |
| 762 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 763 | class PyShell(OutputWindow): |
| 764 | |
| 765 | shell_title = "Python Shell" |
| 766 | |
| 767 | # Override classes |
| 768 | ColorDelegator = ModifiedColorDelegator |
| 769 | UndoDelegator = ModifiedUndoDelegator |
| 770 | |
Kurt B. Kaiser | f06eed0 | 2002-12-11 04:42:04 +0000 | [diff] [blame] | 771 | # Override menus |
Kurt B. Kaiser | dc1e709 | 2002-07-11 04:33:41 +0000 | [diff] [blame] | 772 | menu_specs = [ |
| 773 | ("file", "_File"), |
| 774 | ("edit", "_Edit"), |
Kurt B. Kaiser | 4cc5ef5 | 2003-01-22 00:23:23 +0000 | [diff] [blame] | 775 | ("debug", "_Debug"), |
Kurt B. Kaiser | 1061e72 | 2003-01-04 01:43:53 +0000 | [diff] [blame] | 776 | ("options", "_Options"), |
Kurt B. Kaiser | dc1e709 | 2002-07-11 04:33:41 +0000 | [diff] [blame] | 777 | ("windows", "_Windows"), |
| 778 | ("help", "_Help"), |
| 779 | ] |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 780 | |
Ronald Oussoren | 19302d9 | 2006-06-11 14:33:36 +0000 | [diff] [blame^] | 781 | if macosxSupport.runningAsOSXApp(): |
| 782 | del menu_specs[-3] |
| 783 | menu_specs[-2] = ("windows", "_Window") |
| 784 | |
| 785 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 786 | # New classes |
| 787 | from IdleHistory import History |
| 788 | |
| 789 | def __init__(self, flist=None): |
Kurt B. Kaiser | 8f570a7 | 2003-05-15 18:52:51 +0000 | [diff] [blame] | 790 | if use_subprocess: |
Kurt B. Kaiser | 67fd0ea | 2003-05-24 20:59:15 +0000 | [diff] [blame] | 791 | ms = self.menu_specs |
| 792 | if ms[2][0] != "shell": |
| 793 | ms.insert(2, ("shell", "_Shell")) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 794 | self.interp = ModifiedInterpreter(self) |
| 795 | if flist is None: |
| 796 | root = Tk() |
| 797 | fixwordbreaks(root) |
| 798 | root.withdraw() |
| 799 | flist = PyShellFileList(root) |
Kurt B. Kaiser | 5afa1df | 2002-10-10 08:25:24 +0000 | [diff] [blame] | 800 | # |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 801 | OutputWindow.__init__(self, flist, None, None) |
Kurt B. Kaiser | 5afa1df | 2002-10-10 08:25:24 +0000 | [diff] [blame] | 802 | # |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 803 | import __builtin__ |
| 804 | __builtin__.quit = __builtin__.exit = "To exit, type Ctrl-D." |
Kurt B. Kaiser | 5afa1df | 2002-10-10 08:25:24 +0000 | [diff] [blame] | 805 | # |
Kurt B. Kaiser | 6af4498 | 2005-01-19 00:22:59 +0000 | [diff] [blame] | 806 | ## self.config(usetabs=1, indentwidth=8, context_use_ps1=1) |
| 807 | self.usetabs = True |
| 808 | # indentwidth must be 8 when using tabs. See note in EditorWindow: |
| 809 | self.indentwidth = 8 |
| 810 | self.context_use_ps1 = True |
Kurt B. Kaiser | 5afa1df | 2002-10-10 08:25:24 +0000 | [diff] [blame] | 811 | # |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 812 | text = self.text |
| 813 | text.configure(wrap="char") |
| 814 | text.bind("<<newline-and-indent>>", self.enter_callback) |
| 815 | text.bind("<<plain-newline-and-indent>>", self.linefeed_callback) |
| 816 | text.bind("<<interrupt-execution>>", self.cancel_callback) |
| 817 | text.bind("<<beginning-of-line>>", self.home_callback) |
| 818 | text.bind("<<end-of-file>>", self.eof_callback) |
| 819 | text.bind("<<open-stack-viewer>>", self.open_stack_viewer) |
Kurt B. Kaiser | 57bfe5d | 2003-05-10 00:09:52 +0000 | [diff] [blame] | 820 | text.bind("<<toggle-debugger>>", self.toggle_debugger) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 821 | text.bind("<<toggle-jit-stack-viewer>>", self.toggle_jit_stack_viewer) |
Kurt B. Kaiser | 8f570a7 | 2003-05-15 18:52:51 +0000 | [diff] [blame] | 822 | if use_subprocess: |
| 823 | text.bind("<<view-restart>>", self.view_restart_mark) |
| 824 | text.bind("<<restart-shell>>", self.restart_shell) |
Kurt B. Kaiser | 5afa1df | 2002-10-10 08:25:24 +0000 | [diff] [blame] | 825 | # |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 826 | self.save_stdout = sys.stdout |
| 827 | self.save_stderr = sys.stderr |
| 828 | self.save_stdin = sys.stdin |
Martin v. Löwis | bcc651a | 2003-06-22 07:52:56 +0000 | [diff] [blame] | 829 | import IOBinding |
| 830 | self.stdout = PseudoFile(self, "stdout", IOBinding.encoding) |
| 831 | self.stderr = PseudoFile(self, "stderr", IOBinding.encoding) |
| 832 | self.console = PseudoFile(self, "console", IOBinding.encoding) |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 833 | if not use_subprocess: |
| 834 | sys.stdout = self.stdout |
| 835 | sys.stderr = self.stderr |
| 836 | sys.stdin = self |
Kurt B. Kaiser | 5afa1df | 2002-10-10 08:25:24 +0000 | [diff] [blame] | 837 | # |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 838 | self.history = self.History(self.text) |
Kurt B. Kaiser | 5afa1df | 2002-10-10 08:25:24 +0000 | [diff] [blame] | 839 | # |
Kurt B. Kaiser | 88957d8 | 2003-05-19 23:11:51 +0000 | [diff] [blame] | 840 | self.pollinterval = 50 # millisec |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 841 | |
Kurt B. Kaiser | 4d5bc60 | 2004-06-06 01:29:22 +0000 | [diff] [blame] | 842 | def get_standard_extension_names(self): |
| 843 | return idleConf.GetExtensions(shell_only=True) |
| 844 | |
Kurt B. Kaiser | 003091c | 2003-02-17 18:57:16 +0000 | [diff] [blame] | 845 | reading = False |
| 846 | executing = False |
| 847 | canceled = False |
| 848 | endoffile = False |
| 849 | closing = False |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 850 | |
Kurt B. Kaiser | 49a5fe1 | 2004-07-04 01:25:56 +0000 | [diff] [blame] | 851 | def set_warning_stream(self, stream): |
Skip Montanaro | d916142 | 2004-07-06 21:53:27 +0000 | [diff] [blame] | 852 | global warning_stream |
| 853 | warning_stream = stream |
Kurt B. Kaiser | 49a5fe1 | 2004-07-04 01:25:56 +0000 | [diff] [blame] | 854 | |
| 855 | def get_warning_stream(self): |
| 856 | return warning_stream |
| 857 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 858 | def toggle_debugger(self, event=None): |
| 859 | if self.executing: |
| 860 | tkMessageBox.showerror("Don't debug now", |
| 861 | "You can only toggle the debugger when idle", |
| 862 | master=self.text) |
| 863 | self.set_debugger_indicator() |
| 864 | return "break" |
| 865 | else: |
| 866 | db = self.interp.getdebugger() |
| 867 | if db: |
| 868 | self.close_debugger() |
| 869 | else: |
| 870 | self.open_debugger() |
| 871 | |
| 872 | def set_debugger_indicator(self): |
| 873 | db = self.interp.getdebugger() |
| 874 | self.setvar("<<toggle-debugger>>", not not db) |
| 875 | |
Kurt B. Kaiser | 1061e72 | 2003-01-04 01:43:53 +0000 | [diff] [blame] | 876 | def toggle_jit_stack_viewer(self, event=None): |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 877 | pass # All we need is the variable |
| 878 | |
| 879 | def close_debugger(self): |
| 880 | db = self.interp.getdebugger() |
| 881 | if db: |
| 882 | self.interp.setdebugger(None) |
| 883 | db.close() |
Kurt B. Kaiser | ffd3a42 | 2002-06-26 02:32:09 +0000 | [diff] [blame] | 884 | if self.interp.rpcclt: |
| 885 | RemoteDebugger.close_remote_debugger(self.interp.rpcclt) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 886 | self.resetoutput() |
| 887 | self.console.write("[DEBUG OFF]\n") |
| 888 | sys.ps1 = ">>> " |
| 889 | self.showprompt() |
| 890 | self.set_debugger_indicator() |
| 891 | |
| 892 | def open_debugger(self): |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 893 | if self.interp.rpcclt: |
Kurt B. Kaiser | 7f38ec0 | 2003-05-15 03:19:42 +0000 | [diff] [blame] | 894 | dbg_gui = RemoteDebugger.start_remote_debugger(self.interp.rpcclt, |
| 895 | self) |
| 896 | else: |
| 897 | dbg_gui = Debugger.Debugger(self) |
| 898 | self.interp.setdebugger(dbg_gui) |
| 899 | dbg_gui.load_breakpoints() |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 900 | sys.ps1 = "[DEBUG ON]\n>>> " |
| 901 | self.showprompt() |
| 902 | self.set_debugger_indicator() |
| 903 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 904 | def beginexecuting(self): |
Kurt B. Kaiser | ffd3a42 | 2002-06-26 02:32:09 +0000 | [diff] [blame] | 905 | "Helper for ModifiedInterpreter" |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 906 | self.resetoutput() |
| 907 | self.executing = 1 |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 908 | |
| 909 | def endexecuting(self): |
Kurt B. Kaiser | 83118c6 | 2002-06-24 17:03:37 +0000 | [diff] [blame] | 910 | "Helper for ModifiedInterpreter" |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 911 | self.executing = 0 |
| 912 | self.canceled = 0 |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 913 | self.showprompt() |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 914 | |
| 915 | def close(self): |
Kurt B. Kaiser | 83118c6 | 2002-06-24 17:03:37 +0000 | [diff] [blame] | 916 | "Extend EditorWindow.close()" |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 917 | if self.executing: |
Kurt B. Kaiser | 003091c | 2003-02-17 18:57:16 +0000 | [diff] [blame] | 918 | response = tkMessageBox.askokcancel( |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 919 | "Kill?", |
Kurt B. Kaiser | 003091c | 2003-02-17 18:57:16 +0000 | [diff] [blame] | 920 | "The program is still running!\n Do you want to kill it?", |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 921 | default="ok", |
Kurt B. Kaiser | 7f38ec0 | 2003-05-15 03:19:42 +0000 | [diff] [blame] | 922 | parent=self.text) |
Kurt B. Kaiser | 003091c | 2003-02-17 18:57:16 +0000 | [diff] [blame] | 923 | if response == False: |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 924 | return "cancel" |
Kurt B. Kaiser | 5c3df35 | 2004-12-23 04:20:59 +0000 | [diff] [blame] | 925 | if self.reading: |
| 926 | self.top.quit() |
| 927 | self.canceled = True |
Kurt B. Kaiser | 67fd0ea | 2003-05-24 20:59:15 +0000 | [diff] [blame] | 928 | self.closing = True |
| 929 | # Wait for poll_subprocess() rescheduling to stop |
| 930 | self.text.after(2 * self.pollinterval, self.close2) |
Kurt B. Kaiser | 88957d8 | 2003-05-19 23:11:51 +0000 | [diff] [blame] | 931 | |
| 932 | def close2(self): |
| 933 | return EditorWindow.close(self) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 934 | |
| 935 | def _close(self): |
Kurt B. Kaiser | 83118c6 | 2002-06-24 17:03:37 +0000 | [diff] [blame] | 936 | "Extend EditorWindow._close(), shut down debugger and execution server" |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 937 | self.close_debugger() |
Kurt B. Kaiser | 7f38ec0 | 2003-05-15 03:19:42 +0000 | [diff] [blame] | 938 | if use_subprocess: |
| 939 | self.interp.kill_subprocess() |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 940 | # Restore std streams |
| 941 | sys.stdout = self.save_stdout |
| 942 | sys.stderr = self.save_stderr |
| 943 | sys.stdin = self.save_stdin |
| 944 | # Break cycles |
| 945 | self.interp = None |
| 946 | self.console = None |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 947 | self.flist.pyshell = None |
| 948 | self.history = None |
Kurt B. Kaiser | 83118c6 | 2002-06-24 17:03:37 +0000 | [diff] [blame] | 949 | EditorWindow._close(self) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 950 | |
| 951 | def ispythonsource(self, filename): |
Kurt B. Kaiser | 83118c6 | 2002-06-24 17:03:37 +0000 | [diff] [blame] | 952 | "Override EditorWindow method: never remove the colorizer" |
Kurt B. Kaiser | 837d15c | 2002-09-18 02:29:59 +0000 | [diff] [blame] | 953 | return True |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 954 | |
| 955 | def short_title(self): |
| 956 | return self.shell_title |
| 957 | |
Kurt B. Kaiser | 94bd774 | 2001-07-14 00:13:28 +0000 | [diff] [blame] | 958 | COPYRIGHT = \ |
Kurt B. Kaiser | 8f570a7 | 2003-05-15 18:52:51 +0000 | [diff] [blame] | 959 | 'Type "copyright", "credits" or "license()" for more information.' |
Kurt B. Kaiser | 94bd774 | 2001-07-14 00:13:28 +0000 | [diff] [blame] | 960 | |
Kurt B. Kaiser | 220fecf | 2003-07-27 03:24:19 +0000 | [diff] [blame] | 961 | firewallmessage = """ |
| 962 | **************************************************************** |
| 963 | Personal firewall software may warn about the connection IDLE |
| 964 | makes to its subprocess using this computer's internal loopback |
| 965 | interface. This connection is not visible on any external |
| 966 | interface and no data is sent to or received from the Internet. |
| 967 | **************************************************************** |
| 968 | """ |
| 969 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 970 | def begin(self): |
| 971 | self.resetoutput() |
Kurt B. Kaiser | 7f38ec0 | 2003-05-15 03:19:42 +0000 | [diff] [blame] | 972 | if use_subprocess: |
| 973 | nosub = '' |
Kurt B. Kaiser | af3eb87 | 2004-01-21 18:54:30 +0000 | [diff] [blame] | 974 | client = self.interp.start_subprocess() |
| 975 | if not client: |
| 976 | self.close() |
Kurt B. Kaiser | 7663729 | 2004-01-21 22:10:01 +0000 | [diff] [blame] | 977 | return False |
Kurt B. Kaiser | 7f38ec0 | 2003-05-15 03:19:42 +0000 | [diff] [blame] | 978 | else: |
| 979 | nosub = "==== No Subprocess ====" |
Kurt B. Kaiser | 220fecf | 2003-07-27 03:24:19 +0000 | [diff] [blame] | 980 | self.write("Python %s on %s\n%s\n%s\nIDLE %s %s\n" % |
Kurt B. Kaiser | 94bd774 | 2001-07-14 00:13:28 +0000 | [diff] [blame] | 981 | (sys.version, sys.platform, self.COPYRIGHT, |
Kurt B. Kaiser | 220fecf | 2003-07-27 03:24:19 +0000 | [diff] [blame] | 982 | self.firewallmessage, idlever.IDLE_VERSION, nosub)) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 983 | self.showprompt() |
| 984 | import Tkinter |
Kurt B. Kaiser | af3eb87 | 2004-01-21 18:54:30 +0000 | [diff] [blame] | 985 | Tkinter._default_root = None # 03Jan04 KBK What's this? |
Kurt B. Kaiser | 7663729 | 2004-01-21 22:10:01 +0000 | [diff] [blame] | 986 | return True |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 987 | |
| 988 | def readline(self): |
| 989 | save = self.reading |
| 990 | try: |
| 991 | self.reading = 1 |
Kurt B. Kaiser | 5c3df35 | 2004-12-23 04:20:59 +0000 | [diff] [blame] | 992 | self.top.mainloop() # nested mainloop() |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 993 | finally: |
| 994 | self.reading = save |
| 995 | line = self.text.get("iomark", "end-1c") |
Kurt B. Kaiser | 5c3df35 | 2004-12-23 04:20:59 +0000 | [diff] [blame] | 996 | if len(line) == 0: # may be EOF if we quit our mainloop with Ctrl-C |
| 997 | line = "\n" |
Martin v. Löwis | bcc651a | 2003-06-22 07:52:56 +0000 | [diff] [blame] | 998 | if isinstance(line, unicode): |
| 999 | import IOBinding |
| 1000 | try: |
| 1001 | line = line.encode(IOBinding.encoding) |
| 1002 | except UnicodeError: |
| 1003 | pass |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1004 | self.resetoutput() |
| 1005 | if self.canceled: |
| 1006 | self.canceled = 0 |
Kurt B. Kaiser | 5c3df35 | 2004-12-23 04:20:59 +0000 | [diff] [blame] | 1007 | if not use_subprocess: |
| 1008 | raise KeyboardInterrupt |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1009 | if self.endoffile: |
| 1010 | self.endoffile = 0 |
Kurt B. Kaiser | 5c3df35 | 2004-12-23 04:20:59 +0000 | [diff] [blame] | 1011 | line = "" |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1012 | return line |
| 1013 | |
| 1014 | def isatty(self): |
Kurt B. Kaiser | 837d15c | 2002-09-18 02:29:59 +0000 | [diff] [blame] | 1015 | return True |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1016 | |
Kurt B. Kaiser | 003091c | 2003-02-17 18:57:16 +0000 | [diff] [blame] | 1017 | def cancel_callback(self, event=None): |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1018 | try: |
| 1019 | if self.text.compare("sel.first", "!=", "sel.last"): |
| 1020 | return # Active selection -- always use default binding |
| 1021 | except: |
| 1022 | pass |
| 1023 | if not (self.executing or self.reading): |
| 1024 | self.resetoutput() |
Kurt B. Kaiser | 7f38ec0 | 2003-05-15 03:19:42 +0000 | [diff] [blame] | 1025 | self.interp.write("KeyboardInterrupt\n") |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1026 | self.showprompt() |
| 1027 | return "break" |
| 1028 | self.endoffile = 0 |
Kurt B. Kaiser | 003091c | 2003-02-17 18:57:16 +0000 | [diff] [blame] | 1029 | self.canceled = 1 |
Kurt B. Kaiser | 5c3df35 | 2004-12-23 04:20:59 +0000 | [diff] [blame] | 1030 | if (self.executing and self.interp.rpcclt): |
Kurt B. Kaiser | 67fd0ea | 2003-05-24 20:59:15 +0000 | [diff] [blame] | 1031 | if self.interp.getdebugger(): |
| 1032 | self.interp.restart_subprocess() |
| 1033 | else: |
| 1034 | self.interp.interrupt_subprocess() |
Kurt B. Kaiser | 5c3df35 | 2004-12-23 04:20:59 +0000 | [diff] [blame] | 1035 | if self.reading: |
| 1036 | self.top.quit() # exit the nested mainloop() in readline() |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1037 | return "break" |
| 1038 | |
| 1039 | def eof_callback(self, event): |
| 1040 | if self.executing and not self.reading: |
| 1041 | return # Let the default binding (delete next char) take over |
| 1042 | if not (self.text.compare("iomark", "==", "insert") and |
| 1043 | self.text.compare("insert", "==", "end-1c")): |
| 1044 | return # Let the default binding (delete next char) take over |
| 1045 | if not self.executing: |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1046 | self.resetoutput() |
| 1047 | self.close() |
| 1048 | else: |
| 1049 | self.canceled = 0 |
| 1050 | self.endoffile = 1 |
| 1051 | self.top.quit() |
| 1052 | return "break" |
| 1053 | |
| 1054 | def home_callback(self, event): |
| 1055 | if event.state != 0 and event.keysym == "Home": |
| 1056 | return # <Modifier-Home>; fall back to class binding |
| 1057 | if self.text.compare("iomark", "<=", "insert") and \ |
| 1058 | self.text.compare("insert linestart", "<=", "iomark"): |
| 1059 | self.text.mark_set("insert", "iomark") |
| 1060 | self.text.tag_remove("sel", "1.0", "end") |
| 1061 | self.text.see("insert") |
| 1062 | return "break" |
| 1063 | |
| 1064 | def linefeed_callback(self, event): |
| 1065 | # Insert a linefeed without entering anything (still autoindented) |
| 1066 | if self.reading: |
| 1067 | self.text.insert("insert", "\n") |
| 1068 | self.text.see("insert") |
| 1069 | else: |
Kurt B. Kaiser | 822a77f | 2002-12-16 02:07:11 +0000 | [diff] [blame] | 1070 | self.newline_and_indent_event(event) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1071 | return "break" |
| 1072 | |
| 1073 | def enter_callback(self, event): |
| 1074 | if self.executing and not self.reading: |
| 1075 | return # Let the default binding (insert '\n') take over |
| 1076 | # If some text is selected, recall the selection |
| 1077 | # (but only if this before the I/O mark) |
| 1078 | try: |
| 1079 | sel = self.text.get("sel.first", "sel.last") |
| 1080 | if sel: |
| 1081 | if self.text.compare("sel.last", "<=", "iomark"): |
Kurt B. Kaiser | a7daba6 | 2005-06-19 18:56:15 +0000 | [diff] [blame] | 1082 | self.recall(sel, event) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1083 | return "break" |
| 1084 | except: |
| 1085 | pass |
| 1086 | # If we're strictly before the line containing iomark, recall |
| 1087 | # the current line, less a leading prompt, less leading or |
| 1088 | # trailing whitespace |
| 1089 | if self.text.compare("insert", "<", "iomark linestart"): |
| 1090 | # Check if there's a relevant stdin range -- if so, use it |
| 1091 | prev = self.text.tag_prevrange("stdin", "insert") |
| 1092 | if prev and self.text.compare("insert", "<", prev[1]): |
Kurt B. Kaiser | a7daba6 | 2005-06-19 18:56:15 +0000 | [diff] [blame] | 1093 | self.recall(self.text.get(prev[0], prev[1]), event) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1094 | return "break" |
| 1095 | next = self.text.tag_nextrange("stdin", "insert") |
| 1096 | if next and self.text.compare("insert lineend", ">=", next[0]): |
Kurt B. Kaiser | a7daba6 | 2005-06-19 18:56:15 +0000 | [diff] [blame] | 1097 | self.recall(self.text.get(next[0], next[1]), event) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1098 | return "break" |
Kurt B. Kaiser | 4ada7ad | 2002-12-29 22:03:38 +0000 | [diff] [blame] | 1099 | # No stdin mark -- just get the current line, less any prompt |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 1100 | indices = self.text.tag_nextrange("console", "insert linestart") |
| 1101 | if indices and \ |
| 1102 | self.text.compare(indices[0], "<=", "insert linestart"): |
| 1103 | self.recall(self.text.get(indices[1], "insert lineend"), event) |
| 1104 | else: |
| 1105 | self.recall(self.text.get("insert linestart", "insert lineend"), event) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1106 | return "break" |
Kurt B. Kaiser | 822a77f | 2002-12-16 02:07:11 +0000 | [diff] [blame] | 1107 | # If we're between the beginning of the line and the iomark, i.e. |
Kurt B. Kaiser | 4ada7ad | 2002-12-29 22:03:38 +0000 | [diff] [blame] | 1108 | # in the prompt area, move to the end of the prompt |
Kurt B. Kaiser | 822a77f | 2002-12-16 02:07:11 +0000 | [diff] [blame] | 1109 | if self.text.compare("insert", "<", "iomark"): |
Kurt B. Kaiser | 4ada7ad | 2002-12-29 22:03:38 +0000 | [diff] [blame] | 1110 | self.text.mark_set("insert", "iomark") |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1111 | # If we're in the current input and there's only whitespace |
| 1112 | # beyond the cursor, erase that whitespace first |
| 1113 | s = self.text.get("insert", "end-1c") |
Kurt B. Kaiser | 837d15c | 2002-09-18 02:29:59 +0000 | [diff] [blame] | 1114 | if s and not s.strip(): |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1115 | self.text.delete("insert", "end-1c") |
| 1116 | # If we're in the current input before its last line, |
| 1117 | # insert a newline right at the insert point |
| 1118 | if self.text.compare("insert", "<", "end-1c linestart"): |
Kurt B. Kaiser | 822a77f | 2002-12-16 02:07:11 +0000 | [diff] [blame] | 1119 | self.newline_and_indent_event(event) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1120 | return "break" |
| 1121 | # We're in the last line; append a newline and submit it |
| 1122 | self.text.mark_set("insert", "end-1c") |
| 1123 | if self.reading: |
| 1124 | self.text.insert("insert", "\n") |
| 1125 | self.text.see("insert") |
| 1126 | else: |
Kurt B. Kaiser | 822a77f | 2002-12-16 02:07:11 +0000 | [diff] [blame] | 1127 | self.newline_and_indent_event(event) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1128 | self.text.tag_add("stdin", "iomark", "end-1c") |
| 1129 | self.text.update_idletasks() |
| 1130 | if self.reading: |
| 1131 | self.top.quit() # Break out of recursive mainloop() in raw_input() |
| 1132 | else: |
| 1133 | self.runit() |
| 1134 | return "break" |
| 1135 | |
Kurt B. Kaiser | a7daba6 | 2005-06-19 18:56:15 +0000 | [diff] [blame] | 1136 | def recall(self, s, event): |
| 1137 | self.text.undo_block_start() |
| 1138 | try: |
| 1139 | self.text.tag_remove("sel", "1.0", "end") |
| 1140 | self.text.mark_set("insert", "end-1c") |
| 1141 | s = s.strip() |
| 1142 | lines = s.split('\n') |
Kurt B. Kaiser | 8fa7eb5 | 2005-06-21 02:42:17 +0000 | [diff] [blame] | 1143 | prefix = self.text.get("insert linestart","insert").rstrip() |
| 1144 | if prefix and prefix[-1]==':': |
| 1145 | self.newline_and_indent_event(event) |
| 1146 | self.text.insert("insert",lines[0].strip()) |
| 1147 | if len(lines) > 1: |
| 1148 | self.newline_and_indent_event(event) |
| 1149 | for line in lines[1:]: |
| 1150 | self.text.insert("insert", line.strip()) |
Kurt B. Kaiser | a7daba6 | 2005-06-19 18:56:15 +0000 | [diff] [blame] | 1151 | self.newline_and_indent_event(event) |
Kurt B. Kaiser | a7daba6 | 2005-06-19 18:56:15 +0000 | [diff] [blame] | 1152 | finally: |
| 1153 | self.text.see("insert") |
| 1154 | self.text.undo_block_stop() |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1155 | |
| 1156 | def runit(self): |
| 1157 | line = self.text.get("iomark", "end-1c") |
| 1158 | # Strip off last newline and surrounding whitespace. |
| 1159 | # (To allow you to hit return twice to end a statement.) |
| 1160 | i = len(line) |
| 1161 | while i > 0 and line[i-1] in " \t": |
| 1162 | i = i-1 |
| 1163 | if i > 0 and line[i-1] == "\n": |
| 1164 | i = i-1 |
| 1165 | while i > 0 and line[i-1] in " \t": |
| 1166 | i = i-1 |
| 1167 | line = line[:i] |
| 1168 | more = self.interp.runsource(line) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1169 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1170 | def open_stack_viewer(self, event=None): |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 1171 | if self.interp.rpcclt: |
| 1172 | return self.interp.remote_stack_viewer() |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1173 | try: |
| 1174 | sys.last_traceback |
| 1175 | except: |
| 1176 | tkMessageBox.showerror("No stack trace", |
| 1177 | "There is no stack trace yet.\n" |
| 1178 | "(sys.last_traceback is not defined)", |
| 1179 | master=self.text) |
| 1180 | return |
| 1181 | from StackViewer import StackBrowser |
| 1182 | sv = StackBrowser(self.root, self.flist) |
| 1183 | |
Kurt B. Kaiser | 1061e72 | 2003-01-04 01:43:53 +0000 | [diff] [blame] | 1184 | def view_restart_mark(self, event=None): |
| 1185 | self.text.see("iomark") |
| 1186 | self.text.see("restart") |
| 1187 | |
| 1188 | def restart_shell(self, event=None): |
Kurt B. Kaiser | 67fd0ea | 2003-05-24 20:59:15 +0000 | [diff] [blame] | 1189 | self.interp.restart_subprocess() |
Kurt B. Kaiser | 1061e72 | 2003-01-04 01:43:53 +0000 | [diff] [blame] | 1190 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1191 | def showprompt(self): |
| 1192 | self.resetoutput() |
| 1193 | try: |
| 1194 | s = str(sys.ps1) |
| 1195 | except: |
| 1196 | s = "" |
| 1197 | self.console.write(s) |
| 1198 | self.text.mark_set("insert", "end-1c") |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 1199 | self.set_line_and_column() |
Kurt B. Kaiser | dc1e709 | 2002-07-11 04:33:41 +0000 | [diff] [blame] | 1200 | self.io.reset_undo() |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1201 | |
| 1202 | def resetoutput(self): |
| 1203 | source = self.text.get("iomark", "end-1c") |
| 1204 | if self.history: |
| 1205 | self.history.history_store(source) |
| 1206 | if self.text.get("end-2c") != "\n": |
| 1207 | self.text.insert("end-1c", "\n") |
| 1208 | self.text.mark_set("iomark", "end-1c") |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 1209 | self.set_line_and_column() |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1210 | sys.stdout.softspace = 0 |
| 1211 | |
| 1212 | def write(self, s, tags=()): |
Kurt B. Kaiser | 003091c | 2003-02-17 18:57:16 +0000 | [diff] [blame] | 1213 | try: |
| 1214 | self.text.mark_gravity("iomark", "right") |
| 1215 | OutputWindow.write(self, s, tags, "iomark") |
| 1216 | self.text.mark_gravity("iomark", "left") |
| 1217 | except: |
| 1218 | pass |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1219 | if self.canceled: |
| 1220 | self.canceled = 0 |
Kurt B. Kaiser | 7f38ec0 | 2003-05-15 03:19:42 +0000 | [diff] [blame] | 1221 | if not use_subprocess: |
| 1222 | raise KeyboardInterrupt |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1223 | |
Kurt B. Kaiser | dcba662 | 2004-12-21 22:10:32 +0000 | [diff] [blame] | 1224 | class PseudoFile(object): |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1225 | |
Martin v. Löwis | bcc651a | 2003-06-22 07:52:56 +0000 | [diff] [blame] | 1226 | def __init__(self, shell, tags, encoding=None): |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1227 | self.shell = shell |
| 1228 | self.tags = tags |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 1229 | self.softspace = 0 |
Martin v. Löwis | bcc651a | 2003-06-22 07:52:56 +0000 | [diff] [blame] | 1230 | self.encoding = encoding |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1231 | |
| 1232 | def write(self, s): |
| 1233 | self.shell.write(s, self.tags) |
| 1234 | |
| 1235 | def writelines(self, l): |
| 1236 | map(self.write, l) |
| 1237 | |
| 1238 | def flush(self): |
| 1239 | pass |
| 1240 | |
| 1241 | def isatty(self): |
Kurt B. Kaiser | 837d15c | 2002-09-18 02:29:59 +0000 | [diff] [blame] | 1242 | return True |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1243 | |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 1244 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1245 | usage_msg = """\ |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1246 | |
Kurt B. Kaiser | 11659ad | 2003-05-15 23:23:21 +0000 | [diff] [blame] | 1247 | USAGE: idle [-deins] [-t title] [file]* |
| 1248 | idle [-dns] [-t title] (-c cmd | -r file) [arg]* |
| 1249 | idle [-dns] [-t title] - [arg]* |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 1250 | |
Kurt B. Kaiser | f06eed0 | 2002-12-11 04:42:04 +0000 | [diff] [blame] | 1251 | -h print this help message and exit |
Kurt B. Kaiser | 8f570a7 | 2003-05-15 18:52:51 +0000 | [diff] [blame] | 1252 | -n run IDLE without a subprocess (see Help/IDLE Help for details) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1253 | |
Kurt B. Kaiser | f06eed0 | 2002-12-11 04:42:04 +0000 | [diff] [blame] | 1254 | The following options will override the IDLE 'settings' configuration: |
Kurt B. Kaiser | 96d8842 | 2001-07-17 04:59:01 +0000 | [diff] [blame] | 1255 | |
Kurt B. Kaiser | f06eed0 | 2002-12-11 04:42:04 +0000 | [diff] [blame] | 1256 | -e open an edit window |
| 1257 | -i open a shell window |
| 1258 | |
| 1259 | The following options imply -i and will open a shell: |
| 1260 | |
| 1261 | -c cmd run the command in a shell, or |
| 1262 | -r file run script from file |
| 1263 | |
| 1264 | -d enable the debugger |
| 1265 | -s run $IDLESTARTUP or $PYTHONSTARTUP before anything else |
| 1266 | -t title set title of shell window |
| 1267 | |
| 1268 | A default edit window will be bypassed when -c, -r, or - are used. |
| 1269 | |
| 1270 | [arg]* are passed to the command (-c) or script (-r) in sys.argv[1:]. |
| 1271 | |
| 1272 | Examples: |
| 1273 | |
| 1274 | idle |
| 1275 | Open an edit window or shell depending on IDLE's configuration. |
| 1276 | |
| 1277 | idle foo.py foobar.py |
| 1278 | Edit the files, also open a shell if configured to start with shell. |
| 1279 | |
| 1280 | idle -est "Baz" foo.py |
| 1281 | Run $IDLESTARTUP or $PYTHONSTARTUP, edit foo.py, and open a shell |
| 1282 | window with the title "Baz". |
| 1283 | |
| 1284 | idle -c "import sys; print sys.argv" "foo" |
| 1285 | Open a shell window and run the command, passing "-c" in sys.argv[0] |
| 1286 | and "foo" in sys.argv[1]. |
| 1287 | |
| 1288 | idle -d -s -r foo.py "Hello World" |
| 1289 | Open a shell window, run a startup script, enable the debugger, and |
| 1290 | run foo.py, passing "foo.py" in sys.argv[0] and "Hello World" in |
| 1291 | sys.argv[1]. |
| 1292 | |
| 1293 | echo "import sys; print sys.argv" | idle - "foobar" |
| 1294 | Open a shell window, run the script piped in, passing '' in sys.argv[0] |
| 1295 | and "foobar" in sys.argv[1]. |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1296 | """ |
| 1297 | |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 1298 | def main(): |
Kurt B. Kaiser | f06eed0 | 2002-12-11 04:42:04 +0000 | [diff] [blame] | 1299 | global flist, root, use_subprocess |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1300 | |
Kurt B. Kaiser | 8f570a7 | 2003-05-15 18:52:51 +0000 | [diff] [blame] | 1301 | use_subprocess = True |
Kurt B. Kaiser | f06eed0 | 2002-12-11 04:42:04 +0000 | [diff] [blame] | 1302 | enable_shell = False |
| 1303 | enable_edit = False |
| 1304 | debug = False |
| 1305 | cmd = None |
| 1306 | script = None |
| 1307 | startup = False |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 1308 | try: |
Kurt B. Kaiser | 4ada7ad | 2002-12-29 22:03:38 +0000 | [diff] [blame] | 1309 | sys.ps1 |
| 1310 | except AttributeError: |
| 1311 | sys.ps1 = '>>> ' |
| 1312 | try: |
Kurt B. Kaiser | 8f570a7 | 2003-05-15 18:52:51 +0000 | [diff] [blame] | 1313 | opts, args = getopt.getopt(sys.argv[1:], "c:deihnr:st:") |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 1314 | except getopt.error, msg: |
| 1315 | sys.stderr.write("Error: %s\n" % str(msg)) |
| 1316 | sys.stderr.write(usage_msg) |
| 1317 | sys.exit(2) |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 1318 | for o, a in opts: |
| 1319 | if o == '-c': |
| 1320 | cmd = a |
Kurt B. Kaiser | f06eed0 | 2002-12-11 04:42:04 +0000 | [diff] [blame] | 1321 | enable_shell = True |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 1322 | if o == '-d': |
Kurt B. Kaiser | f06eed0 | 2002-12-11 04:42:04 +0000 | [diff] [blame] | 1323 | debug = True |
| 1324 | enable_shell = True |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 1325 | if o == '-e': |
Kurt B. Kaiser | f06eed0 | 2002-12-11 04:42:04 +0000 | [diff] [blame] | 1326 | enable_edit = True |
| 1327 | if o == '-h': |
| 1328 | sys.stdout.write(usage_msg) |
| 1329 | sys.exit() |
| 1330 | if o == '-i': |
| 1331 | enable_shell = True |
Kurt B. Kaiser | 8f570a7 | 2003-05-15 18:52:51 +0000 | [diff] [blame] | 1332 | if o == '-n': |
| 1333 | use_subprocess = False |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 1334 | if o == '-r': |
| 1335 | script = a |
Kurt B. Kaiser | f06eed0 | 2002-12-11 04:42:04 +0000 | [diff] [blame] | 1336 | if os.path.isfile(script): |
| 1337 | pass |
| 1338 | else: |
| 1339 | print "No script file: ", script |
| 1340 | sys.exit() |
| 1341 | enable_shell = True |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 1342 | if o == '-s': |
Kurt B. Kaiser | f06eed0 | 2002-12-11 04:42:04 +0000 | [diff] [blame] | 1343 | startup = True |
| 1344 | enable_shell = True |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 1345 | if o == '-t': |
| 1346 | PyShell.shell_title = a |
Kurt B. Kaiser | f06eed0 | 2002-12-11 04:42:04 +0000 | [diff] [blame] | 1347 | enable_shell = True |
| 1348 | if args and args[0] == '-': |
| 1349 | cmd = sys.stdin.read() |
| 1350 | enable_shell = True |
Kurt B. Kaiser | f06eed0 | 2002-12-11 04:42:04 +0000 | [diff] [blame] | 1351 | # process sys.argv and sys.path: |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 1352 | for i in range(len(sys.path)): |
| 1353 | sys.path[i] = os.path.abspath(sys.path[i]) |
Kurt B. Kaiser | f06eed0 | 2002-12-11 04:42:04 +0000 | [diff] [blame] | 1354 | if args and args[0] == '-': |
| 1355 | sys.argv = [''] + args[1:] |
| 1356 | elif cmd: |
| 1357 | sys.argv = ['-c'] + args |
| 1358 | elif script: |
| 1359 | sys.argv = [script] + args |
| 1360 | elif args: |
| 1361 | enable_edit = True |
| 1362 | pathx = [] |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 1363 | for filename in args: |
| 1364 | pathx.append(os.path.dirname(filename)) |
Kurt B. Kaiser | f06eed0 | 2002-12-11 04:42:04 +0000 | [diff] [blame] | 1365 | for dir in pathx: |
| 1366 | dir = os.path.abspath(dir) |
| 1367 | if not dir in sys.path: |
| 1368 | sys.path.insert(0, dir) |
Kurt B. Kaiser | ff002b9 | 2002-12-21 21:39:11 +0000 | [diff] [blame] | 1369 | else: |
| 1370 | dir = os.getcwd() |
| 1371 | if not dir in sys.path: |
| 1372 | sys.path.insert(0, dir) |
Kurt B. Kaiser | f06eed0 | 2002-12-11 04:42:04 +0000 | [diff] [blame] | 1373 | # check the IDLE settings configuration (but command line overrides) |
| 1374 | edit_start = idleConf.GetOption('main', 'General', |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 1375 | 'editor-on-startup', type='bool') |
Kurt B. Kaiser | f06eed0 | 2002-12-11 04:42:04 +0000 | [diff] [blame] | 1376 | enable_edit = enable_edit or edit_start |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 1377 | enable_shell = enable_shell or not edit_start |
Kurt B. Kaiser | f06eed0 | 2002-12-11 04:42:04 +0000 | [diff] [blame] | 1378 | # start editor and/or shell windows: |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 1379 | root = Tk(className="Idle") |
Ronald Oussoren | 19302d9 | 2006-06-11 14:33:36 +0000 | [diff] [blame^] | 1380 | |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 1381 | fixwordbreaks(root) |
| 1382 | root.withdraw() |
| 1383 | flist = PyShellFileList(root) |
Ronald Oussoren | 19302d9 | 2006-06-11 14:33:36 +0000 | [diff] [blame^] | 1384 | macosxSupport.setupApp(root, flist) |
| 1385 | |
Kurt B. Kaiser | f06eed0 | 2002-12-11 04:42:04 +0000 | [diff] [blame] | 1386 | if enable_edit: |
| 1387 | if not (cmd or script): |
| 1388 | for filename in args: |
| 1389 | flist.open(filename) |
| 1390 | if not args: |
| 1391 | flist.new() |
Kurt B. Kaiser | af3eb87 | 2004-01-21 18:54:30 +0000 | [diff] [blame] | 1392 | if enable_shell: |
Ronald Oussoren | 19302d9 | 2006-06-11 14:33:36 +0000 | [diff] [blame^] | 1393 | shell = flist.open_shell() |
| 1394 | if not shell: |
Kurt B. Kaiser | af3eb87 | 2004-01-21 18:54:30 +0000 | [diff] [blame] | 1395 | return # couldn't open shell |
Ronald Oussoren | 19302d9 | 2006-06-11 14:33:36 +0000 | [diff] [blame^] | 1396 | |
| 1397 | if macosxSupport.runningAsOSXApp() and flist.dict: |
| 1398 | # On OSX: when the user has double-clicked on a file that causes |
| 1399 | # IDLE to be launched the shell window will open just in front of |
| 1400 | # the file she wants to see. Lower the interpreter window when |
| 1401 | # there are open files. |
| 1402 | shell.top.lower() |
| 1403 | |
Kurt B. Kaiser | f06eed0 | 2002-12-11 04:42:04 +0000 | [diff] [blame] | 1404 | shell = flist.pyshell |
| 1405 | # handle remaining options: |
| 1406 | if debug: |
| 1407 | shell.open_debugger() |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 1408 | if startup: |
| 1409 | filename = os.environ.get("IDLESTARTUP") or \ |
| 1410 | os.environ.get("PYTHONSTARTUP") |
| 1411 | if filename and os.path.isfile(filename): |
Kurt B. Kaiser | f06eed0 | 2002-12-11 04:42:04 +0000 | [diff] [blame] | 1412 | shell.interp.execfile(filename) |
Kurt B. Kaiser | af3eb87 | 2004-01-21 18:54:30 +0000 | [diff] [blame] | 1413 | if shell and cmd or script: |
Kurt B. Kaiser | f06eed0 | 2002-12-11 04:42:04 +0000 | [diff] [blame] | 1414 | shell.interp.runcommand("""if 1: |
| 1415 | import sys as _sys |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 1416 | _sys.argv = %r |
Kurt B. Kaiser | f06eed0 | 2002-12-11 04:42:04 +0000 | [diff] [blame] | 1417 | del _sys |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 1418 | \n""" % (sys.argv,)) |
Kurt B. Kaiser | f06eed0 | 2002-12-11 04:42:04 +0000 | [diff] [blame] | 1419 | if cmd: |
| 1420 | shell.interp.execsource(cmd) |
| 1421 | elif script: |
Kurt B. Kaiser | 11659ad | 2003-05-15 23:23:21 +0000 | [diff] [blame] | 1422 | shell.interp.prepend_syspath(script) |
Kurt B. Kaiser | f06eed0 | 2002-12-11 04:42:04 +0000 | [diff] [blame] | 1423 | shell.interp.execfile(script) |
Ronald Oussoren | 19302d9 | 2006-06-11 14:33:36 +0000 | [diff] [blame^] | 1424 | |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 1425 | root.mainloop() |
| 1426 | root.destroy() |
| 1427 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1428 | if __name__ == "__main__": |
Kurt B. Kaiser | 9e8b828 | 2003-06-15 17:38:45 +0000 | [diff] [blame] | 1429 | sys.modules['PyShell'] = sys.modules['__main__'] |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1430 | main() |