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