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 re |
| 22 | import string |
| 23 | import tabnanny |
| 24 | import tokenize |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 25 | import tkMessageBox |
Kurt B. Kaiser | 7f38ec0 | 2003-05-15 03:19:42 +0000 | [diff] [blame] | 26 | import PyShell |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 27 | |
Kurt B. Kaiser | 6c638b6 | 2003-05-26 06:23:10 +0000 | [diff] [blame] | 28 | from configHandler import idleConf |
| 29 | |
Kurt B. Kaiser | 92b5ca3 | 2002-12-17 21:16:12 +0000 | [diff] [blame] | 30 | IDENTCHARS = string.ascii_letters + string.digits + "_" |
| 31 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 32 | indent_message = """Error: Inconsistent indentation detected! |
| 33 | |
Kurt B. Kaiser | ca7329c | 2005-06-12 05:19:23 +0000 | [diff] [blame] | 34 | 1) Your indentation is outright incorrect (easy to fix), OR |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 35 | |
Kurt B. Kaiser | ca7329c | 2005-06-12 05:19:23 +0000 | [diff] [blame] | 36 | 2) Your indentation mixes tabs and spaces. |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 37 | |
Kurt B. Kaiser | ca7329c | 2005-06-12 05:19:23 +0000 | [diff] [blame] | 38 | To fix case 2, change all tabs to spaces by using Edit->Select All followed \ |
| 39 | by Format->Untabify Region and specify the number of columns used by each tab. |
| 40 | """ |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 41 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 42 | class ScriptBinding: |
Steven M. Gava | 42f6c64 | 2001-07-12 06:46:53 +0000 | [diff] [blame] | 43 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 44 | menudefs = [ |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 45 | ('run', [None, |
Kurt B. Kaiser | 92b5ca3 | 2002-12-17 21:16:12 +0000 | [diff] [blame] | 46 | ('Check Module', '<<check-module>>'), |
Kurt B. Kaiser | eb9637e | 2003-01-26 04:17:16 +0000 | [diff] [blame] | 47 | ('Run Module', '<<run-module>>'), ]), ] |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 48 | |
| 49 | def __init__(self, editwin): |
| 50 | self.editwin = editwin |
| 51 | # Provide instance variables referenced by Debugger |
| 52 | # XXX This should be done differently |
| 53 | self.flist = self.editwin.flist |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 54 | self.root = self.editwin.root |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 55 | |
Kurt B. Kaiser | 0cd233f | 2005-08-23 03:25:38 +0000 | [diff] [blame] | 56 | def check_module_event(self, event): |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 57 | filename = self.getfilename() |
| 58 | if not filename: |
| 59 | return |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 60 | if not self.checksyntax(filename): |
| 61 | return |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 62 | if not self.tabnanny(filename): |
| 63 | return |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 64 | |
| 65 | def tabnanny(self, filename): |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 66 | f = open(filename, 'r') |
| 67 | try: |
Kurt B. Kaiser | 05bab1e | 2002-09-18 03:05:19 +0000 | [diff] [blame] | 68 | tabnanny.process_tokens(tokenize.generate_tokens(f.readline)) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 69 | except tokenize.TokenError as msg: |
Kurt B. Kaiser | 3f8ace9 | 2003-06-05 02:38:32 +0000 | [diff] [blame] | 70 | msgtxt, (lineno, start) = msg |
| 71 | self.editwin.gotoline(lineno) |
| 72 | self.errorbox("Tabnanny Tokenizing Error", |
| 73 | "Token Error: %s" % msgtxt) |
Neal Norwitz | 6453c1f | 2002-11-30 19:18:46 +0000 | [diff] [blame] | 74 | return False |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 75 | except tabnanny.NannyNag as nag: |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 76 | # The error messages from tabnanny are too confusing... |
| 77 | self.editwin.gotoline(nag.get_lineno()) |
| 78 | self.errorbox("Tab/space error", indent_message) |
Neal Norwitz | 6453c1f | 2002-11-30 19:18:46 +0000 | [diff] [blame] | 79 | return False |
| 80 | return True |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 81 | |
| 82 | def checksyntax(self, filename): |
Kurt B. Kaiser | 49a5fe1 | 2004-07-04 01:25:56 +0000 | [diff] [blame] | 83 | self.shell = shell = self.flist.open_shell() |
| 84 | saved_stream = shell.get_warning_stream() |
| 85 | shell.set_warning_stream(shell.stderr) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 86 | f = open(filename, 'r') |
| 87 | source = f.read() |
| 88 | f.close() |
| 89 | if '\r' in source: |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 90 | source = re.sub(r"\r\n", "\n", source) |
Kurt B. Kaiser | 2618c7f | 2005-08-23 02:27:23 +0000 | [diff] [blame] | 91 | source = re.sub(r"\r", "\n", source) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 92 | if source and source[-1] != '\n': |
| 93 | source = source + '\n' |
Kurt B. Kaiser | 3f8ace9 | 2003-06-05 02:38:32 +0000 | [diff] [blame] | 94 | text = self.editwin.text |
| 95 | text.tag_remove("ERROR", "1.0", "end") |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 96 | try: |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 97 | try: |
Kurt B. Kaiser | 49a5fe1 | 2004-07-04 01:25:56 +0000 | [diff] [blame] | 98 | # If successful, return the compiled code |
| 99 | return compile(source, filename, "exec") |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 100 | except (SyntaxError, OverflowError) as err: |
Kurt B. Kaiser | 49a5fe1 | 2004-07-04 01:25:56 +0000 | [diff] [blame] | 101 | try: |
| 102 | msg, (errorfilename, lineno, offset, line) = err |
| 103 | if not errorfilename: |
| 104 | err.args = msg, (filename, lineno, offset, line) |
| 105 | err.filename = filename |
| 106 | self.colorize_syntax_error(msg, lineno, offset) |
| 107 | except: |
| 108 | msg = "*** " + str(err) |
| 109 | self.errorbox("Syntax error", |
| 110 | "There's an error in your program:\n" + msg) |
| 111 | return False |
| 112 | finally: |
| 113 | shell.set_warning_stream(saved_stream) |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 114 | |
Kurt B. Kaiser | 92b5ca3 | 2002-12-17 21:16:12 +0000 | [diff] [blame] | 115 | def colorize_syntax_error(self, msg, lineno, offset): |
| 116 | text = self.editwin.text |
| 117 | pos = "0.0 + %d lines + %d chars" % (lineno-1, offset-1) |
| 118 | text.tag_add("ERROR", pos) |
| 119 | char = text.get(pos) |
| 120 | if char and char in IDENTCHARS: |
| 121 | text.tag_add("ERROR", pos + " wordstart", pos) |
| 122 | if '\n' == text.get(pos): # error at line end |
| 123 | text.mark_set("insert", pos) |
| 124 | else: |
| 125 | text.mark_set("insert", pos + "+1c") |
| 126 | text.see(pos) |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 127 | |
Kurt B. Kaiser | eb9637e | 2003-01-26 04:17:16 +0000 | [diff] [blame] | 128 | def run_module_event(self, event): |
Kurt B. Kaiser | ce5b6d5 | 2003-05-31 23:44:18 +0000 | [diff] [blame] | 129 | """Run the module after setting up the environment. |
| 130 | |
| 131 | First check the syntax. If OK, make sure the shell is active and |
| 132 | then transfer the arguments, set the run environment's working |
| 133 | directory to the directory of the module being executed and also |
| 134 | add that directory to its sys.path if not already included. |
| 135 | |
| 136 | """ |
Kurt B. Kaiser | 0cd233f | 2005-08-23 03:25:38 +0000 | [diff] [blame] | 137 | filename = self.getfilename() |
| 138 | if not filename: |
| 139 | return |
Kurt B. Kaiser | 0cd233f | 2005-08-23 03:25:38 +0000 | [diff] [blame] | 140 | code = self.checksyntax(filename) |
Kurt B. Kaiser | 92b5ca3 | 2002-12-17 21:16:12 +0000 | [diff] [blame] | 141 | if not code: |
| 142 | return |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 143 | if not self.tabnanny(filename): |
| 144 | return |
Kurt B. Kaiser | 49a5fe1 | 2004-07-04 01:25:56 +0000 | [diff] [blame] | 145 | shell = self.shell |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 146 | interp = shell.interp |
Kurt B. Kaiser | 7f38ec0 | 2003-05-15 03:19:42 +0000 | [diff] [blame] | 147 | if PyShell.use_subprocess: |
| 148 | shell.restart_shell() |
Kurt B. Kaiser | ce5b6d5 | 2003-05-31 23:44:18 +0000 | [diff] [blame] | 149 | dirname = os.path.dirname(filename) |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 150 | # XXX Too often this discards arguments the user just set... |
| 151 | interp.runcommand("""if 1: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 152 | _filename = %r |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 153 | import sys as _sys |
| 154 | from os.path import basename as _basename |
| 155 | if (not _sys.argv or |
| 156 | _basename(_sys.argv[0]) != _basename(_filename)): |
| 157 | _sys.argv = [_filename] |
Kurt B. Kaiser | ce5b6d5 | 2003-05-31 23:44:18 +0000 | [diff] [blame] | 158 | import os as _os |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 159 | _os.chdir(%r) |
Kurt B. Kaiser | ce5b6d5 | 2003-05-31 23:44:18 +0000 | [diff] [blame] | 160 | del _filename, _sys, _basename, _os |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 161 | \n""" % (filename, dirname)) |
Kurt B. Kaiser | 11659ad | 2003-05-15 23:23:21 +0000 | [diff] [blame] | 162 | interp.prepend_syspath(filename) |
Kurt B. Kaiser | 49a5fe1 | 2004-07-04 01:25:56 +0000 | [diff] [blame] | 163 | # XXX KBK 03Jul04 When run w/o subprocess, runtime warnings still |
| 164 | # go to __stderr__. With subprocess, they go to the shell. |
| 165 | # Need to change streams in PyShell.ModifiedInterpreter. |
Kurt B. Kaiser | 92b5ca3 | 2002-12-17 21:16:12 +0000 | [diff] [blame] | 166 | interp.runcode(code) |
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 | 6c638b6 | 2003-05-26 06:23:10 +0000 | [diff] [blame] | 186 | reply = self.ask_save_dialog() |
| 187 | self.editwin.text.focus_set() |
| 188 | if reply == "ok": |
| 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?" |
| 197 | mb = tkMessageBox.Message(title="Save Before Run or Check", |
| 198 | message=msg, |
| 199 | icon=tkMessageBox.QUESTION, |
| 200 | type=tkMessageBox.OKCANCEL, |
| 201 | default=tkMessageBox.OK, |
| 202 | master=self.editwin.text) |
| 203 | return mb.show() |
| 204 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 205 | def errorbox(self, title, message): |
| 206 | # XXX This should really be a function of EditorWindow... |
| 207 | tkMessageBox.showerror(title, message, master=self.editwin.text) |
| 208 | self.editwin.text.focus_set() |