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