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