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 |
Georg Brandl | 14fc427 | 2008-05-17 18:39:55 +0000 | [diff] [blame] | 25 | import tkinter.messagebox as tkMessageBox |
Kurt B. Kaiser | 2d7f6a0 | 2007-08-22 23:01:33 +0000 | [diff] [blame] | 26 | from idlelib.EditorWindow import EditorWindow |
| 27 | from idlelib import PyShell |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 28 | |
Kurt B. Kaiser | 2d7f6a0 | 2007-08-22 23:01:33 +0000 | [diff] [blame] | 29 | from idlelib.configHandler import idleConf |
Kurt B. Kaiser | 6c638b6 | 2003-05-26 06:23:10 +0000 | [diff] [blame] | 30 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 31 | indent_message = """Error: Inconsistent indentation detected! |
| 32 | |
Kurt B. Kaiser | ca7329c | 2005-06-12 05:19:23 +0000 | [diff] [blame] | 33 | 1) Your indentation is outright incorrect (easy to fix), OR |
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 | 2) Your indentation mixes tabs and spaces. |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 36 | |
Kurt B. Kaiser | ca7329c | 2005-06-12 05:19:23 +0000 | [diff] [blame] | 37 | To fix case 2, change all tabs to spaces by using Edit->Select All followed \ |
| 38 | by Format->Untabify Region and specify the number of columns used by each tab. |
| 39 | """ |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 40 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 41 | class ScriptBinding: |
Steven M. Gava | 42f6c64 | 2001-07-12 06:46:53 +0000 | [diff] [blame] | 42 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 43 | menudefs = [ |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 44 | ('run', [None, |
Kurt B. Kaiser | 92b5ca3 | 2002-12-17 21:16:12 +0000 | [diff] [blame] | 45 | ('Check Module', '<<check-module>>'), |
Kurt B. Kaiser | eb9637e | 2003-01-26 04:17:16 +0000 | [diff] [blame] | 46 | ('Run Module', '<<run-module>>'), ]), ] |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 47 | |
| 48 | def __init__(self, editwin): |
| 49 | self.editwin = editwin |
| 50 | # Provide instance variables referenced by Debugger |
| 51 | # XXX This should be done differently |
| 52 | self.flist = self.editwin.flist |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 53 | self.root = self.editwin.root |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 54 | |
Kurt B. Kaiser | 0cd233f | 2005-08-23 03:25:38 +0000 | [diff] [blame] | 55 | def check_module_event(self, event): |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 56 | filename = self.getfilename() |
| 57 | if not filename: |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 58 | return 'break' |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 59 | if not self.checksyntax(filename): |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 60 | return 'break' |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 61 | if not self.tabnanny(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 | |
| 64 | def tabnanny(self, filename): |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 65 | f = open(filename, 'r') |
| 66 | try: |
Kurt B. Kaiser | 05bab1e | 2002-09-18 03:05:19 +0000 | [diff] [blame] | 67 | tabnanny.process_tokens(tokenize.generate_tokens(f.readline)) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 68 | except tokenize.TokenError as msg: |
Kurt B. Kaiser | 3f8ace9 | 2003-06-05 02:38:32 +0000 | [diff] [blame] | 69 | msgtxt, (lineno, start) = msg |
| 70 | self.editwin.gotoline(lineno) |
| 71 | self.errorbox("Tabnanny Tokenizing Error", |
| 72 | "Token Error: %s" % msgtxt) |
Neal Norwitz | 6453c1f | 2002-11-30 19:18:46 +0000 | [diff] [blame] | 73 | return False |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 74 | except tabnanny.NannyNag as nag: |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 75 | # The error messages from tabnanny are too confusing... |
| 76 | self.editwin.gotoline(nag.get_lineno()) |
| 77 | self.errorbox("Tab/space error", indent_message) |
Neal Norwitz | 6453c1f | 2002-11-30 19:18:46 +0000 | [diff] [blame] | 78 | return False |
| 79 | return True |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 80 | |
| 81 | def checksyntax(self, filename): |
Kurt B. Kaiser | 49a5fe1 | 2004-07-04 01:25:56 +0000 | [diff] [blame] | 82 | self.shell = shell = self.flist.open_shell() |
| 83 | saved_stream = shell.get_warning_stream() |
| 84 | shell.set_warning_stream(shell.stderr) |
Kurt B. Kaiser | 7a4dbaf | 2007-08-22 21:34:28 +0000 | [diff] [blame] | 85 | f = open(filename, 'r') |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 86 | source = f.read() |
| 87 | f.close() |
| 88 | if '\r' in source: |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 89 | source = re.sub(r"\r\n", "\n", source) |
Kurt B. Kaiser | 2618c7f | 2005-08-23 02:27:23 +0000 | [diff] [blame] | 90 | source = re.sub(r"\r", "\n", source) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 91 | if source and source[-1] != '\n': |
| 92 | source = source + '\n' |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 93 | editwin = self.editwin |
| 94 | text = editwin.text |
Kurt B. Kaiser | 3f8ace9 | 2003-06-05 02:38:32 +0000 | [diff] [blame] | 95 | text.tag_remove("ERROR", "1.0", "end") |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 96 | try: |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 97 | # If successful, return the compiled code |
| 98 | return compile(source, filename, "exec") |
| 99 | except (SyntaxError, OverflowError) as value: |
| 100 | msg = value.msg or "<no detail available>" |
| 101 | lineno = value.lineno or 1 |
| 102 | offset = value.offset or 0 |
| 103 | if offset == 0: |
| 104 | lineno += 1 #mark end of offending line |
| 105 | pos = "0.0 + %d lines + %d chars" % (lineno-1, offset-1) |
| 106 | editwin.colorize_syntax_error(text, pos) |
| 107 | self.errorbox("SyntaxError", "%-20s" % msg) |
| 108 | return False |
Kurt B. Kaiser | 49a5fe1 | 2004-07-04 01:25:56 +0000 | [diff] [blame] | 109 | finally: |
| 110 | shell.set_warning_stream(saved_stream) |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 111 | |
Kurt B. Kaiser | eb9637e | 2003-01-26 04:17:16 +0000 | [diff] [blame] | 112 | def run_module_event(self, event): |
Kurt B. Kaiser | ce5b6d5 | 2003-05-31 23:44:18 +0000 | [diff] [blame] | 113 | """Run the module after setting up the environment. |
| 114 | |
| 115 | First check the syntax. If OK, make sure the shell is active and |
| 116 | then transfer the arguments, set the run environment's working |
| 117 | directory to the directory of the module being executed and also |
| 118 | add that directory to its sys.path if not already included. |
| 119 | |
| 120 | """ |
Kurt B. Kaiser | 0cd233f | 2005-08-23 03:25:38 +0000 | [diff] [blame] | 121 | filename = self.getfilename() |
| 122 | if not filename: |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 123 | return 'break' |
Kurt B. Kaiser | 0cd233f | 2005-08-23 03:25:38 +0000 | [diff] [blame] | 124 | code = self.checksyntax(filename) |
Kurt B. Kaiser | 92b5ca3 | 2002-12-17 21:16:12 +0000 | [diff] [blame] | 125 | if not code: |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 126 | return 'break' |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 127 | if not self.tabnanny(filename): |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 128 | return 'break' |
Kurt B. Kaiser | 49a5fe1 | 2004-07-04 01:25:56 +0000 | [diff] [blame] | 129 | shell = self.shell |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 130 | interp = shell.interp |
Kurt B. Kaiser | 7f38ec0 | 2003-05-15 03:19:42 +0000 | [diff] [blame] | 131 | if PyShell.use_subprocess: |
| 132 | shell.restart_shell() |
Kurt B. Kaiser | ce5b6d5 | 2003-05-31 23:44:18 +0000 | [diff] [blame] | 133 | dirname = os.path.dirname(filename) |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 134 | # XXX Too often this discards arguments the user just set... |
| 135 | interp.runcommand("""if 1: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 136 | _filename = %r |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 137 | import sys as _sys |
| 138 | from os.path import basename as _basename |
| 139 | if (not _sys.argv or |
| 140 | _basename(_sys.argv[0]) != _basename(_filename)): |
| 141 | _sys.argv = [_filename] |
Kurt B. Kaiser | ce5b6d5 | 2003-05-31 23:44:18 +0000 | [diff] [blame] | 142 | import os as _os |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 143 | _os.chdir(%r) |
Kurt B. Kaiser | ce5b6d5 | 2003-05-31 23:44:18 +0000 | [diff] [blame] | 144 | del _filename, _sys, _basename, _os |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 145 | \n""" % (filename, dirname)) |
Kurt B. Kaiser | 11659ad | 2003-05-15 23:23:21 +0000 | [diff] [blame] | 146 | interp.prepend_syspath(filename) |
Kurt B. Kaiser | 49a5fe1 | 2004-07-04 01:25:56 +0000 | [diff] [blame] | 147 | # XXX KBK 03Jul04 When run w/o subprocess, runtime warnings still |
| 148 | # go to __stderr__. With subprocess, they go to the shell. |
| 149 | # Need to change streams in PyShell.ModifiedInterpreter. |
Kurt B. Kaiser | 92b5ca3 | 2002-12-17 21:16:12 +0000 | [diff] [blame] | 150 | interp.runcode(code) |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 151 | return 'break' |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 152 | |
| 153 | def getfilename(self): |
Kurt B. Kaiser | d5e1cef | 2002-12-19 03:25:34 +0000 | [diff] [blame] | 154 | """Get source filename. If not saved, offer to save (or create) file |
| 155 | |
| 156 | The debugger requires a source file. Make sure there is one, and that |
| 157 | the current version of the source buffer has been saved. If the user |
| 158 | declines to save or cancels the Save As dialog, return None. |
Kurt B. Kaiser | 6c638b6 | 2003-05-26 06:23:10 +0000 | [diff] [blame] | 159 | |
| 160 | If the user has configured IDLE for Autosave, the file will be |
| 161 | silently saved if it already exists and is dirty. |
Kurt B. Kaiser | 8d1f11b | 2003-05-26 22:20:34 +0000 | [diff] [blame] | 162 | |
Kurt B. Kaiser | d5e1cef | 2002-12-19 03:25:34 +0000 | [diff] [blame] | 163 | """ |
Kurt B. Kaiser | 6c638b6 | 2003-05-26 06:23:10 +0000 | [diff] [blame] | 164 | filename = self.editwin.io.filename |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 165 | if not self.editwin.get_saved(): |
Kurt B. Kaiser | 6c638b6 | 2003-05-26 06:23:10 +0000 | [diff] [blame] | 166 | autosave = idleConf.GetOption('main', 'General', |
| 167 | 'autosave', type='bool') |
| 168 | if autosave and filename: |
Kurt B. Kaiser | d5e1cef | 2002-12-19 03:25:34 +0000 | [diff] [blame] | 169 | self.editwin.io.save(None) |
| 170 | else: |
Kurt B. Kaiser | 6c638b6 | 2003-05-26 06:23:10 +0000 | [diff] [blame] | 171 | reply = self.ask_save_dialog() |
| 172 | self.editwin.text.focus_set() |
| 173 | if reply == "ok": |
| 174 | self.editwin.io.save(None) |
| 175 | filename = self.editwin.io.filename |
| 176 | else: |
| 177 | filename = None |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 178 | return filename |
| 179 | |
Kurt B. Kaiser | 6c638b6 | 2003-05-26 06:23:10 +0000 | [diff] [blame] | 180 | def ask_save_dialog(self): |
| 181 | msg = "Source Must Be Saved\n" + 5*' ' + "OK to Save?" |
| 182 | mb = tkMessageBox.Message(title="Save Before Run or Check", |
| 183 | message=msg, |
| 184 | icon=tkMessageBox.QUESTION, |
| 185 | type=tkMessageBox.OKCANCEL, |
| 186 | default=tkMessageBox.OK, |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 187 | parent=self.editwin.text) |
Kurt B. Kaiser | 6c638b6 | 2003-05-26 06:23:10 +0000 | [diff] [blame] | 188 | return mb.show() |
| 189 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 190 | def errorbox(self, title, message): |
| 191 | # XXX This should really be a function of EditorWindow... |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 192 | tkMessageBox.showerror(title, message, parent=self.editwin.text) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 193 | self.editwin.text.focus_set() |