David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1 | """Extension to execute code outside the Python shell window. |
| 2 | |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 3 | This adds the following commands: |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 4 | |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 5 | - Check module does a full syntax check of the current module. |
Kurt B. Kaiser | 92b5ca3 | 2002-12-17 21:16:12 +0000 | [diff] [blame] | 6 | It also runs the tabnanny to catch any inconsistent tabs. |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 7 | |
Kurt B. Kaiser | 63857a4 | 2002-09-05 02:31:20 +0000 | [diff] [blame] | 8 | - Run module executes the module's code in the __main__ namespace. The window |
Kurt B. Kaiser | 92b5ca3 | 2002-12-17 21:16:12 +0000 | [diff] [blame] | 9 | must have been saved previously. The module is added to sys.modules, and is |
| 10 | also added to the __main__ namespace. |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 11 | |
Kurt B. Kaiser | 92b5ca3 | 2002-12-17 21:16:12 +0000 | [diff] [blame] | 12 | XXX GvR Redesign this interface (yet again) as follows: |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 13 | |
Kurt B. Kaiser | eb9637e | 2003-01-26 04:17:16 +0000 | [diff] [blame] | 14 | - Present a dialog box for ``Run Module'' |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 15 | |
| 16 | - Allow specify command line arguments in the dialog box |
| 17 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 18 | """ |
| 19 | |
Kurt B. Kaiser | ce5b6d5 | 2003-05-31 23:44:18 +0000 | [diff] [blame] | 20 | import os |
Kurt B. Kaiser | 92b5ca3 | 2002-12-17 21:16:12 +0000 | [diff] [blame] | 21 | import tabnanny |
| 22 | import tokenize |
Georg Brandl | 14fc427 | 2008-05-17 18:39:55 +0000 | [diff] [blame] | 23 | import tkinter.messagebox as tkMessageBox |
Terry Jan Reedy | 2733618 | 2015-05-14 18:10:50 -0400 | [diff] [blame] | 24 | from idlelib import PyShell |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 25 | |
Kurt B. Kaiser | 2d7f6a0 | 2007-08-22 23:01:33 +0000 | [diff] [blame] | 26 | from idlelib.configHandler import idleConf |
Ronald Oussoren | 5ee0567 | 2011-05-17 14:48:40 +0200 | [diff] [blame] | 27 | from idlelib import macosxSupport |
Kurt B. Kaiser | 6c638b6 | 2003-05-26 06:23:10 +0000 | [diff] [blame] | 28 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 29 | indent_message = """Error: Inconsistent indentation detected! |
| 30 | |
Kurt B. Kaiser | ca7329c | 2005-06-12 05:19:23 +0000 | [diff] [blame] | 31 | 1) Your indentation is outright incorrect (easy to fix), OR |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 32 | |
Kurt B. Kaiser | ca7329c | 2005-06-12 05:19:23 +0000 | [diff] [blame] | 33 | 2) Your indentation mixes tabs and spaces. |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 34 | |
Kurt B. Kaiser | ca7329c | 2005-06-12 05:19:23 +0000 | [diff] [blame] | 35 | To fix case 2, change all tabs to spaces by using Edit->Select All followed \ |
| 36 | by Format->Untabify Region and specify the number of columns used by each tab. |
| 37 | """ |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 38 | |
Terry Jan Reedy | 5c28e9f | 2015-08-06 00:54:07 -0400 | [diff] [blame] | 39 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 40 | class ScriptBinding: |
Steven M. Gava | 42f6c64 | 2001-07-12 06:46:53 +0000 | [diff] [blame] | 41 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 42 | menudefs = [ |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 43 | ('run', [None, |
Kurt B. Kaiser | 92b5ca3 | 2002-12-17 21:16:12 +0000 | [diff] [blame] | 44 | ('Check Module', '<<check-module>>'), |
Kurt B. Kaiser | eb9637e | 2003-01-26 04:17:16 +0000 | [diff] [blame] | 45 | ('Run Module', '<<run-module>>'), ]), ] |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 46 | |
| 47 | def __init__(self, editwin): |
| 48 | self.editwin = editwin |
| 49 | # Provide instance variables referenced by Debugger |
| 50 | # XXX This should be done differently |
| 51 | self.flist = self.editwin.flist |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 52 | self.root = self.editwin.root |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 53 | |
Ned Deily | b760167 | 2014-03-27 20:49:14 -0700 | [diff] [blame] | 54 | if macosxSupport.isCocoaTk(): |
Ronald Oussoren | 5ee0567 | 2011-05-17 14:48:40 +0200 | [diff] [blame] | 55 | self.editwin.text_frame.bind('<<run-module-event-2>>', self._run_module_event) |
| 56 | |
Kurt B. Kaiser | 0cd233f | 2005-08-23 03:25:38 +0000 | [diff] [blame] | 57 | def check_module_event(self, event): |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 58 | filename = self.getfilename() |
| 59 | if not filename: |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 60 | return 'break' |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 61 | if not self.checksyntax(filename): |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 62 | return 'break' |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 63 | if not self.tabnanny(filename): |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 64 | return 'break' |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 65 | |
| 66 | def tabnanny(self, filename): |
Martin v. Löwis | 975a079 | 2009-01-18 20:15:42 +0000 | [diff] [blame] | 67 | # XXX: tabnanny should work on binary files as well |
Victor Stinner | 85c6772 | 2011-09-02 00:57:04 +0200 | [diff] [blame] | 68 | with tokenize.open(filename) as f: |
| 69 | try: |
| 70 | tabnanny.process_tokens(tokenize.generate_tokens(f.readline)) |
| 71 | except tokenize.TokenError as msg: |
Terry Jan Reedy | c6dd5b1 | 2015-08-14 16:59:42 -0400 | [diff] [blame] | 72 | msgtxt, (lineno, start) = msg.args |
Victor Stinner | 85c6772 | 2011-09-02 00:57:04 +0200 | [diff] [blame] | 73 | self.editwin.gotoline(lineno) |
| 74 | self.errorbox("Tabnanny Tokenizing Error", |
| 75 | "Token Error: %s" % msgtxt) |
| 76 | return False |
| 77 | except tabnanny.NannyNag as nag: |
| 78 | # The error messages from tabnanny are too confusing... |
| 79 | self.editwin.gotoline(nag.get_lineno()) |
| 80 | self.errorbox("Tab/space error", indent_message) |
| 81 | return False |
Neal Norwitz | 6453c1f | 2002-11-30 19:18:46 +0000 | [diff] [blame] | 82 | return True |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 83 | |
| 84 | def checksyntax(self, filename): |
Kurt B. Kaiser | 49a5fe1 | 2004-07-04 01:25:56 +0000 | [diff] [blame] | 85 | self.shell = shell = self.flist.open_shell() |
| 86 | saved_stream = shell.get_warning_stream() |
| 87 | shell.set_warning_stream(shell.stderr) |
Terry Jan Reedy | 95f34ab | 2013-08-04 15:39:03 -0400 | [diff] [blame] | 88 | with open(filename, 'rb') as f: |
| 89 | source = f.read() |
Martin v. Löwis | 975a079 | 2009-01-18 20:15:42 +0000 | [diff] [blame] | 90 | if b'\r' in source: |
| 91 | source = source.replace(b'\r\n', b'\n') |
| 92 | source = source.replace(b'\r', b'\n') |
| 93 | if source and source[-1] != ord(b'\n'): |
| 94 | source = source + b'\n' |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 95 | editwin = self.editwin |
| 96 | text = editwin.text |
Kurt B. Kaiser | 3f8ace9 | 2003-06-05 02:38:32 +0000 | [diff] [blame] | 97 | text.tag_remove("ERROR", "1.0", "end") |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 98 | try: |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 99 | # If successful, return the compiled code |
| 100 | return compile(source, filename, "exec") |
Ned Deily | 7974642 | 2011-09-14 14:49:14 -0700 | [diff] [blame] | 101 | except (SyntaxError, OverflowError, ValueError) as value: |
| 102 | msg = getattr(value, 'msg', '') or value or "<no detail available>" |
| 103 | lineno = getattr(value, 'lineno', '') or 1 |
| 104 | offset = getattr(value, 'offset', '') or 0 |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 105 | if offset == 0: |
| 106 | lineno += 1 #mark end of offending line |
| 107 | pos = "0.0 + %d lines + %d chars" % (lineno-1, offset-1) |
| 108 | editwin.colorize_syntax_error(text, pos) |
| 109 | self.errorbox("SyntaxError", "%-20s" % msg) |
| 110 | return False |
Kurt B. Kaiser | 49a5fe1 | 2004-07-04 01:25:56 +0000 | [diff] [blame] | 111 | finally: |
| 112 | shell.set_warning_stream(saved_stream) |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 113 | |
Kurt B. Kaiser | eb9637e | 2003-01-26 04:17:16 +0000 | [diff] [blame] | 114 | def run_module_event(self, event): |
Ned Deily | b760167 | 2014-03-27 20:49:14 -0700 | [diff] [blame] | 115 | if macosxSupport.isCocoaTk(): |
Ronald Oussoren | 5ee0567 | 2011-05-17 14:48:40 +0200 | [diff] [blame] | 116 | # Tk-Cocoa in MacOSX is broken until at least |
| 117 | # Tk 8.5.9, and without this rather |
| 118 | # crude workaround IDLE would hang when a user |
| 119 | # tries to run a module using the keyboard shortcut |
| 120 | # (the menu item works fine). |
| 121 | self.editwin.text_frame.after(200, |
| 122 | lambda: self.editwin.text_frame.event_generate('<<run-module-event-2>>')) |
| 123 | return 'break' |
| 124 | else: |
| 125 | return self._run_module_event(event) |
| 126 | |
| 127 | def _run_module_event(self, event): |
Kurt B. Kaiser | ce5b6d5 | 2003-05-31 23:44:18 +0000 | [diff] [blame] | 128 | """Run the module after setting up the environment. |
| 129 | |
| 130 | First check the syntax. If OK, make sure the shell is active and |
| 131 | then transfer the arguments, set the run environment's working |
| 132 | directory to the directory of the module being executed and also |
| 133 | add that directory to its sys.path if not already included. |
Kurt B. Kaiser | ce5b6d5 | 2003-05-31 23:44:18 +0000 | [diff] [blame] | 134 | """ |
Ronald Oussoren | 5ee0567 | 2011-05-17 14:48:40 +0200 | [diff] [blame] | 135 | |
Kurt B. Kaiser | 0cd233f | 2005-08-23 03:25:38 +0000 | [diff] [blame] | 136 | filename = self.getfilename() |
| 137 | if not filename: |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 138 | return 'break' |
Kurt B. Kaiser | 0cd233f | 2005-08-23 03:25:38 +0000 | [diff] [blame] | 139 | code = self.checksyntax(filename) |
Kurt B. Kaiser | 92b5ca3 | 2002-12-17 21:16:12 +0000 | [diff] [blame] | 140 | if not code: |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 141 | return 'break' |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 142 | if not self.tabnanny(filename): |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 143 | return 'break' |
Terry Jan Reedy | da4c467 | 2012-01-31 02:26:32 -0500 | [diff] [blame] | 144 | interp = self.shell.interp |
Kurt B. Kaiser | 7f38ec0 | 2003-05-15 03:19:42 +0000 | [diff] [blame] | 145 | if PyShell.use_subprocess: |
Terry Jan Reedy | 5c28e9f | 2015-08-06 00:54:07 -0400 | [diff] [blame] | 146 | interp.restart_subprocess(with_cwd=False, filename= |
| 147 | self.editwin._filename_to_unicode(filename)) |
Kurt B. Kaiser | ce5b6d5 | 2003-05-31 23:44:18 +0000 | [diff] [blame] | 148 | dirname = os.path.dirname(filename) |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 149 | # XXX Too often this discards arguments the user just set... |
| 150 | interp.runcommand("""if 1: |
Andrew Svetlov | dfe980b | 2012-04-05 21:54:39 +0300 | [diff] [blame] | 151 | __file__ = {filename!r} |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 152 | import sys as _sys |
| 153 | from os.path import basename as _basename |
| 154 | if (not _sys.argv or |
Andrew Svetlov | dfe980b | 2012-04-05 21:54:39 +0300 | [diff] [blame] | 155 | _basename(_sys.argv[0]) != _basename(__file__)): |
| 156 | _sys.argv = [__file__] |
Kurt B. Kaiser | ce5b6d5 | 2003-05-31 23:44:18 +0000 | [diff] [blame] | 157 | import os as _os |
Andrew Svetlov | dfe980b | 2012-04-05 21:54:39 +0300 | [diff] [blame] | 158 | _os.chdir({dirname!r}) |
| 159 | del _sys, _basename, _os |
| 160 | \n""".format(filename=filename, dirname=dirname)) |
Kurt B. Kaiser | 11659ad | 2003-05-15 23:23:21 +0000 | [diff] [blame] | 161 | interp.prepend_syspath(filename) |
Kurt B. Kaiser | 49a5fe1 | 2004-07-04 01:25:56 +0000 | [diff] [blame] | 162 | # XXX KBK 03Jul04 When run w/o subprocess, runtime warnings still |
| 163 | # go to __stderr__. With subprocess, they go to the shell. |
| 164 | # Need to change streams in PyShell.ModifiedInterpreter. |
Kurt B. Kaiser | 92b5ca3 | 2002-12-17 21:16:12 +0000 | [diff] [blame] | 165 | interp.runcode(code) |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 166 | return 'break' |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 167 | |
| 168 | def getfilename(self): |
Kurt B. Kaiser | d5e1cef | 2002-12-19 03:25:34 +0000 | [diff] [blame] | 169 | """Get source filename. If not saved, offer to save (or create) file |
| 170 | |
| 171 | The debugger requires a source file. Make sure there is one, and that |
| 172 | the current version of the source buffer has been saved. If the user |
| 173 | declines to save or cancels the Save As dialog, return None. |
Kurt B. Kaiser | 6c638b6 | 2003-05-26 06:23:10 +0000 | [diff] [blame] | 174 | |
| 175 | If the user has configured IDLE for Autosave, the file will be |
| 176 | silently saved if it already exists and is dirty. |
Kurt B. Kaiser | 8d1f11b | 2003-05-26 22:20:34 +0000 | [diff] [blame] | 177 | |
Kurt B. Kaiser | d5e1cef | 2002-12-19 03:25:34 +0000 | [diff] [blame] | 178 | """ |
Kurt B. Kaiser | 6c638b6 | 2003-05-26 06:23:10 +0000 | [diff] [blame] | 179 | filename = self.editwin.io.filename |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 180 | if not self.editwin.get_saved(): |
Kurt B. Kaiser | 6c638b6 | 2003-05-26 06:23:10 +0000 | [diff] [blame] | 181 | autosave = idleConf.GetOption('main', 'General', |
| 182 | 'autosave', type='bool') |
| 183 | if autosave and filename: |
Kurt B. Kaiser | d5e1cef | 2002-12-19 03:25:34 +0000 | [diff] [blame] | 184 | self.editwin.io.save(None) |
| 185 | else: |
Kurt B. Kaiser | 0a42982 | 2011-05-12 15:25:24 -0400 | [diff] [blame] | 186 | confirm = self.ask_save_dialog() |
Kurt B. Kaiser | 6c638b6 | 2003-05-26 06:23:10 +0000 | [diff] [blame] | 187 | self.editwin.text.focus_set() |
Kurt B. Kaiser | 0a42982 | 2011-05-12 15:25:24 -0400 | [diff] [blame] | 188 | if confirm: |
Kurt B. Kaiser | 6c638b6 | 2003-05-26 06:23:10 +0000 | [diff] [blame] | 189 | self.editwin.io.save(None) |
| 190 | filename = self.editwin.io.filename |
| 191 | else: |
| 192 | filename = None |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 193 | return filename |
| 194 | |
Kurt B. Kaiser | 6c638b6 | 2003-05-26 06:23:10 +0000 | [diff] [blame] | 195 | def ask_save_dialog(self): |
| 196 | msg = "Source Must Be Saved\n" + 5*' ' + "OK to Save?" |
Kurt B. Kaiser | 0a42982 | 2011-05-12 15:25:24 -0400 | [diff] [blame] | 197 | confirm = tkMessageBox.askokcancel(title="Save Before Run or Check", |
| 198 | message=msg, |
| 199 | default=tkMessageBox.OK, |
Terry Jan Reedy | 3be2e54 | 2015-09-25 22:22:55 -0400 | [diff] [blame] | 200 | parent=self.editwin.text) |
Kurt B. Kaiser | 0a42982 | 2011-05-12 15:25:24 -0400 | [diff] [blame] | 201 | return confirm |
Kurt B. Kaiser | 6c638b6 | 2003-05-26 06:23:10 +0000 | [diff] [blame] | 202 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 203 | def errorbox(self, title, message): |
| 204 | # XXX This should really be a function of EditorWindow... |
Terry Jan Reedy | 3be2e54 | 2015-09-25 22:22:55 -0400 | [diff] [blame] | 205 | tkMessageBox.showerror(title, message, parent=self.editwin.text) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 206 | self.editwin.text.focus_set() |