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