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