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