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