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