Guido van Rossum | 5d42b5b | 1998-10-22 21:56:44 +0000 | [diff] [blame] | 1 | """Utilities needed to emulate Python's interactive interpreter. |
Guido van Rossum | 5227f0f | 1998-09-22 20:38:53 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | 5227f0f | 1998-09-22 20:38:53 +0000 | [diff] [blame] | 3 | """ |
Guido van Rossum | 1557a73 | 1997-07-18 16:57:52 +0000 | [diff] [blame] | 4 | |
Guido van Rossum | 98d9fd3 | 2000-02-28 15:12:25 +0000 | [diff] [blame] | 5 | # Inspired by similar code by Jeff Epler and Fredrik Lundh. |
| 6 | |
Guido van Rossum | 5d42b5b | 1998-10-22 21:56:44 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | a93b848 | 1998-06-23 19:31:19 +0000 | [diff] [blame] | 8 | import sys |
Guido van Rossum | a93b848 | 1998-06-23 19:31:19 +0000 | [diff] [blame] | 9 | import traceback |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 10 | from codeop import CommandCompiler, compile_command |
Guido van Rossum | 90981e0 | 1997-10-07 14:47:24 +0000 | [diff] [blame] | 11 | |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 12 | __all__ = ["InteractiveInterpreter", "InteractiveConsole", "interact", |
Skip Montanaro | e99d5ea | 2001-01-20 19:54:20 +0000 | [diff] [blame] | 13 | "compile_command"] |
| 14 | |
Guido van Rossum | 4ec59c7 | 2001-01-13 22:10:41 +0000 | [diff] [blame] | 15 | def softspace(file, newvalue): |
| 16 | oldvalue = 0 |
| 17 | try: |
| 18 | oldvalue = file.softspace |
| 19 | except AttributeError: |
| 20 | pass |
| 21 | try: |
| 22 | file.softspace = newvalue |
Guido van Rossum | 555d12f | 2001-09-18 13:33:01 +0000 | [diff] [blame] | 23 | except (AttributeError, TypeError): |
| 24 | # "attribute-less object" or "read-only attributes" |
Guido van Rossum | 4ec59c7 | 2001-01-13 22:10:41 +0000 | [diff] [blame] | 25 | pass |
| 26 | return oldvalue |
Guido van Rossum | 90981e0 | 1997-10-07 14:47:24 +0000 | [diff] [blame] | 27 | |
Guido van Rossum | 5227f0f | 1998-09-22 20:38:53 +0000 | [diff] [blame] | 28 | class InteractiveInterpreter: |
| 29 | """Base class for InteractiveConsole. |
Guido van Rossum | a93b848 | 1998-06-23 19:31:19 +0000 | [diff] [blame] | 30 | |
Guido van Rossum | 5227f0f | 1998-09-22 20:38:53 +0000 | [diff] [blame] | 31 | This class deals with parsing and interpreter state (the user's |
| 32 | namespace); it doesn't deal with input buffering or prompting or |
| 33 | input file naming (the filename is always passed in explicitly). |
| 34 | |
Guido van Rossum | a93b848 | 1998-06-23 19:31:19 +0000 | [diff] [blame] | 35 | """ |
| 36 | |
Guido van Rossum | 5227f0f | 1998-09-22 20:38:53 +0000 | [diff] [blame] | 37 | def __init__(self, locals=None): |
Guido van Rossum | a93b848 | 1998-06-23 19:31:19 +0000 | [diff] [blame] | 38 | """Constructor. |
| 39 | |
Guido van Rossum | 5227f0f | 1998-09-22 20:38:53 +0000 | [diff] [blame] | 40 | The optional 'locals' argument specifies the dictionary in |
| 41 | which code will be executed; it defaults to a newly created |
| 42 | dictionary with key "__name__" set to "__console__" and key |
| 43 | "__doc__" set to None. |
Guido van Rossum | a93b848 | 1998-06-23 19:31:19 +0000 | [diff] [blame] | 44 | |
| 45 | """ |
Guido van Rossum | a93b848 | 1998-06-23 19:31:19 +0000 | [diff] [blame] | 46 | if locals is None: |
Guido van Rossum | 5227f0f | 1998-09-22 20:38:53 +0000 | [diff] [blame] | 47 | locals = {"__name__": "__console__", "__doc__": None} |
Guido van Rossum | a93b848 | 1998-06-23 19:31:19 +0000 | [diff] [blame] | 48 | self.locals = locals |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 49 | self.compile = CommandCompiler() |
Guido van Rossum | 5227f0f | 1998-09-22 20:38:53 +0000 | [diff] [blame] | 50 | |
| 51 | def runsource(self, source, filename="<input>", symbol="single"): |
| 52 | """Compile and run some source in the interpreter. |
| 53 | |
| 54 | Arguments are as for compile_command(). |
| 55 | |
| 56 | One several things can happen: |
| 57 | |
| 58 | 1) The input is incorrect; compile_command() raised an |
| 59 | exception (SyntaxError or OverflowError). A syntax traceback |
| 60 | will be printed by calling the showsyntaxerror() method. |
| 61 | |
| 62 | 2) The input is incomplete, and more input is required; |
| 63 | compile_command() returned None. Nothing happens. |
| 64 | |
| 65 | 3) The input is complete; compile_command() returned a code |
| 66 | object. The code is executed by calling self.runcode() (which |
| 67 | also handles run-time exceptions, except for SystemExit). |
| 68 | |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 69 | The return value is True in case 2, False in the other cases (unless |
Guido van Rossum | 5227f0f | 1998-09-22 20:38:53 +0000 | [diff] [blame] | 70 | an exception is raised). The return value can be used to |
| 71 | decide whether to use sys.ps1 or sys.ps2 to prompt the next |
| 72 | line. |
| 73 | |
| 74 | """ |
| 75 | try: |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 76 | code = self.compile(source, filename, symbol) |
Guido van Rossum | 48450cf | 2001-01-15 18:13:35 +0000 | [diff] [blame] | 77 | except (OverflowError, SyntaxError, ValueError): |
Guido van Rossum | 5227f0f | 1998-09-22 20:38:53 +0000 | [diff] [blame] | 78 | # Case 1 |
| 79 | self.showsyntaxerror(filename) |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 80 | return False |
Guido van Rossum | 5227f0f | 1998-09-22 20:38:53 +0000 | [diff] [blame] | 81 | |
| 82 | if code is None: |
| 83 | # Case 2 |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 84 | return True |
Guido van Rossum | 5227f0f | 1998-09-22 20:38:53 +0000 | [diff] [blame] | 85 | |
| 86 | # Case 3 |
| 87 | self.runcode(code) |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 88 | return False |
Guido van Rossum | 5227f0f | 1998-09-22 20:38:53 +0000 | [diff] [blame] | 89 | |
| 90 | def runcode(self, code): |
| 91 | """Execute a code object. |
| 92 | |
| 93 | When an exception occurs, self.showtraceback() is called to |
| 94 | display a traceback. All exceptions are caught except |
| 95 | SystemExit, which is reraised. |
| 96 | |
| 97 | A note about KeyboardInterrupt: this exception may occur |
| 98 | elsewhere in this code, and may not always be caught. The |
| 99 | caller should be prepared to deal with it. |
| 100 | |
| 101 | """ |
| 102 | try: |
| 103 | exec code in self.locals |
| 104 | except SystemExit: |
| 105 | raise |
| 106 | except: |
| 107 | self.showtraceback() |
Guido van Rossum | 4ec59c7 | 2001-01-13 22:10:41 +0000 | [diff] [blame] | 108 | else: |
| 109 | if softspace(sys.stdout, 0): |
| 110 | print |
Guido van Rossum | 5227f0f | 1998-09-22 20:38:53 +0000 | [diff] [blame] | 111 | |
| 112 | def showsyntaxerror(self, filename=None): |
| 113 | """Display the syntax error that just occurred. |
| 114 | |
| 115 | This doesn't display a stack trace because there isn't one. |
| 116 | |
| 117 | If a filename is given, it is stuffed in the exception instead |
| 118 | of what was there before (because Python's parser always uses |
| 119 | "<string>" when reading from a string). |
| 120 | |
| 121 | The output is written by self.write(), below. |
| 122 | |
| 123 | """ |
| 124 | type, value, sys.last_traceback = sys.exc_info() |
| 125 | sys.last_type = type |
| 126 | sys.last_value = value |
| 127 | if filename and type is SyntaxError: |
| 128 | # Work hard to stuff the correct filename in the exception |
| 129 | try: |
| 130 | msg, (dummy_filename, lineno, offset, line) = value |
| 131 | except: |
| 132 | # Not the format we expect; leave it alone |
| 133 | pass |
| 134 | else: |
| 135 | # Stuff in the right filename |
Skip Montanaro | 0679a73 | 2002-03-25 21:44:07 +0000 | [diff] [blame] | 136 | value = SyntaxError(msg, (filename, lineno, offset, line)) |
Fred Drake | c7745d4 | 2001-05-03 04:58:49 +0000 | [diff] [blame] | 137 | sys.last_value = value |
Guido van Rossum | 5227f0f | 1998-09-22 20:38:53 +0000 | [diff] [blame] | 138 | list = traceback.format_exception_only(type, value) |
| 139 | map(self.write, list) |
| 140 | |
| 141 | def showtraceback(self): |
| 142 | """Display the exception that just occurred. |
| 143 | |
| 144 | We remove the first stack item because it is our own code. |
| 145 | |
| 146 | The output is written by self.write(), below. |
| 147 | |
| 148 | """ |
| 149 | try: |
| 150 | type, value, tb = sys.exc_info() |
| 151 | sys.last_type = type |
| 152 | sys.last_value = value |
| 153 | sys.last_traceback = tb |
| 154 | tblist = traceback.extract_tb(tb) |
| 155 | del tblist[:1] |
| 156 | list = traceback.format_list(tblist) |
| 157 | if list: |
Guido van Rossum | 7dd0696 | 2000-12-27 19:12:58 +0000 | [diff] [blame] | 158 | list.insert(0, "Traceback (most recent call last):\n") |
Guido van Rossum | 5227f0f | 1998-09-22 20:38:53 +0000 | [diff] [blame] | 159 | list[len(list):] = traceback.format_exception_only(type, value) |
| 160 | finally: |
| 161 | tblist = tb = None |
| 162 | map(self.write, list) |
| 163 | |
| 164 | def write(self, data): |
| 165 | """Write a string. |
| 166 | |
| 167 | The base implementation writes to sys.stderr; a subclass may |
| 168 | replace this with a different implementation. |
| 169 | |
| 170 | """ |
| 171 | sys.stderr.write(data) |
| 172 | |
| 173 | |
| 174 | class InteractiveConsole(InteractiveInterpreter): |
| 175 | """Closely emulate the behavior of the interactive Python interpreter. |
| 176 | |
| 177 | This class builds on InteractiveInterpreter and adds prompting |
| 178 | using the familiar sys.ps1 and sys.ps2, and input buffering. |
| 179 | |
| 180 | """ |
| 181 | |
| 182 | def __init__(self, locals=None, filename="<console>"): |
| 183 | """Constructor. |
| 184 | |
| 185 | The optional locals argument will be passed to the |
| 186 | InteractiveInterpreter base class. |
| 187 | |
| 188 | The optional filename argument should specify the (file)name |
| 189 | of the input stream; it will show up in tracebacks. |
| 190 | |
| 191 | """ |
| 192 | InteractiveInterpreter.__init__(self, locals) |
| 193 | self.filename = filename |
Guido van Rossum | a93b848 | 1998-06-23 19:31:19 +0000 | [diff] [blame] | 194 | self.resetbuffer() |
| 195 | |
| 196 | def resetbuffer(self): |
Guido van Rossum | 5227f0f | 1998-09-22 20:38:53 +0000 | [diff] [blame] | 197 | """Reset the input buffer.""" |
Guido van Rossum | a93b848 | 1998-06-23 19:31:19 +0000 | [diff] [blame] | 198 | self.buffer = [] |
| 199 | |
| 200 | def interact(self, banner=None): |
Guido van Rossum | 5227f0f | 1998-09-22 20:38:53 +0000 | [diff] [blame] | 201 | """Closely emulate the interactive Python console. |
| 202 | |
| 203 | The optional banner argument specify the banner to print |
| 204 | before the first interaction; by default it prints a banner |
| 205 | similar to the one printed by the real Python interpreter, |
| 206 | followed by the current class name in parentheses (so as not |
| 207 | to confuse this with the real interpreter -- since it's so |
| 208 | close!). |
| 209 | |
| 210 | """ |
Guido van Rossum | a93b848 | 1998-06-23 19:31:19 +0000 | [diff] [blame] | 211 | try: |
| 212 | sys.ps1 |
| 213 | except AttributeError: |
| 214 | sys.ps1 = ">>> " |
| 215 | try: |
| 216 | sys.ps2 |
| 217 | except AttributeError: |
| 218 | sys.ps2 = "... " |
Skip Montanaro | acb29ae | 2002-03-25 22:04:23 +0000 | [diff] [blame] | 219 | cprt = 'Type "help", "copyright", "credits" or "license" for more information.' |
Guido van Rossum | a93b848 | 1998-06-23 19:31:19 +0000 | [diff] [blame] | 220 | if banner is None: |
| 221 | self.write("Python %s on %s\n%s\n(%s)\n" % |
Guido van Rossum | c5f15b0 | 2001-01-14 23:04:22 +0000 | [diff] [blame] | 222 | (sys.version, sys.platform, cprt, |
Guido van Rossum | a93b848 | 1998-06-23 19:31:19 +0000 | [diff] [blame] | 223 | self.__class__.__name__)) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 224 | else: |
Guido van Rossum | a93b848 | 1998-06-23 19:31:19 +0000 | [diff] [blame] | 225 | self.write("%s\n" % str(banner)) |
| 226 | more = 0 |
| 227 | while 1: |
| 228 | try: |
| 229 | if more: |
| 230 | prompt = sys.ps2 |
| 231 | else: |
| 232 | prompt = sys.ps1 |
| 233 | try: |
| 234 | line = self.raw_input(prompt) |
| 235 | except EOFError: |
| 236 | self.write("\n") |
| 237 | break |
| 238 | else: |
| 239 | more = self.push(line) |
| 240 | except KeyboardInterrupt: |
| 241 | self.write("\nKeyboardInterrupt\n") |
| 242 | self.resetbuffer() |
| 243 | more = 0 |
| 244 | |
| 245 | def push(self, line): |
| 246 | """Push a line to the interpreter. |
| 247 | |
Guido van Rossum | 5227f0f | 1998-09-22 20:38:53 +0000 | [diff] [blame] | 248 | The line should not have a trailing newline; it may have |
| 249 | internal newlines. The line is appended to a buffer and the |
| 250 | interpreter's runsource() method is called with the |
| 251 | concatenated contents of the buffer as source. If this |
| 252 | indicates that the command was executed or invalid, the buffer |
| 253 | is reset; otherwise, the command is incomplete, and the buffer |
| 254 | is left as it was after the line was appended. The return |
| 255 | value is 1 if more input is required, 0 if the line was dealt |
| 256 | with in some way (this is the same as runsource()). |
Guido van Rossum | a93b848 | 1998-06-23 19:31:19 +0000 | [diff] [blame] | 257 | |
| 258 | """ |
| 259 | self.buffer.append(line) |
Eric S. Raymond | 6b71e74 | 2001-02-09 08:56:30 +0000 | [diff] [blame] | 260 | source = "\n".join(self.buffer) |
Guido van Rossum | 5227f0f | 1998-09-22 20:38:53 +0000 | [diff] [blame] | 261 | more = self.runsource(source, self.filename) |
| 262 | if not more: |
Guido van Rossum | a93b848 | 1998-06-23 19:31:19 +0000 | [diff] [blame] | 263 | self.resetbuffer() |
Guido van Rossum | 5227f0f | 1998-09-22 20:38:53 +0000 | [diff] [blame] | 264 | return more |
Guido van Rossum | a93b848 | 1998-06-23 19:31:19 +0000 | [diff] [blame] | 265 | |
| 266 | def raw_input(self, prompt=""): |
| 267 | """Write a prompt and read a line. |
| 268 | |
| 269 | The returned line does not include the trailing newline. |
| 270 | When the user enters the EOF key sequence, EOFError is raised. |
| 271 | |
Neal Norwitz | ce96f69 | 2006-03-17 06:49:51 +0000 | [diff] [blame^] | 272 | The base implementation uses sys.stdin.readline(); a subclass |
| 273 | may replace this with a different implementation. |
Guido van Rossum | a93b848 | 1998-06-23 19:31:19 +0000 | [diff] [blame] | 274 | |
| 275 | """ |
Neal Norwitz | ce96f69 | 2006-03-17 06:49:51 +0000 | [diff] [blame^] | 276 | sys.stdout.write(prompt) |
| 277 | sys.stdout.flush() |
| 278 | return sys.stdin.readline() |
| 279 | |
Guido van Rossum | a93b848 | 1998-06-23 19:31:19 +0000 | [diff] [blame] | 280 | |
| 281 | |
Guido van Rossum | d90ae19 | 1998-10-19 18:42:53 +0000 | [diff] [blame] | 282 | def interact(banner=None, readfunc=None, local=None): |
Guido van Rossum | a93b848 | 1998-06-23 19:31:19 +0000 | [diff] [blame] | 283 | """Closely emulate the interactive Python interpreter. |
| 284 | |
| 285 | This is a backwards compatible interface to the InteractiveConsole |
Guido van Rossum | 5227f0f | 1998-09-22 20:38:53 +0000 | [diff] [blame] | 286 | class. When readfunc is not specified, it attempts to import the |
| 287 | readline module to enable GNU readline if it is available. |
Guido van Rossum | a93b848 | 1998-06-23 19:31:19 +0000 | [diff] [blame] | 288 | |
| 289 | Arguments (all optional, all default to None): |
| 290 | |
| 291 | banner -- passed to InteractiveConsole.interact() |
| 292 | readfunc -- if not None, replaces InteractiveConsole.raw_input() |
Guido van Rossum | d90ae19 | 1998-10-19 18:42:53 +0000 | [diff] [blame] | 293 | local -- passed to InteractiveInterpreter.__init__() |
Guido van Rossum | a93b848 | 1998-06-23 19:31:19 +0000 | [diff] [blame] | 294 | |
| 295 | """ |
Guido van Rossum | d90ae19 | 1998-10-19 18:42:53 +0000 | [diff] [blame] | 296 | console = InteractiveConsole(local) |
Guido van Rossum | a93b848 | 1998-06-23 19:31:19 +0000 | [diff] [blame] | 297 | if readfunc is not None: |
| 298 | console.raw_input = readfunc |
Guido van Rossum | 5227f0f | 1998-09-22 20:38:53 +0000 | [diff] [blame] | 299 | else: |
| 300 | try: |
| 301 | import readline |
Skip Montanaro | 0679a73 | 2002-03-25 21:44:07 +0000 | [diff] [blame] | 302 | except ImportError: |
Guido van Rossum | 5227f0f | 1998-09-22 20:38:53 +0000 | [diff] [blame] | 303 | pass |
Guido van Rossum | a93b848 | 1998-06-23 19:31:19 +0000 | [diff] [blame] | 304 | console.interact(banner) |
Guido van Rossum | 5227f0f | 1998-09-22 20:38:53 +0000 | [diff] [blame] | 305 | |
| 306 | |
Guido van Rossum | 90981e0 | 1997-10-07 14:47:24 +0000 | [diff] [blame] | 307 | if __name__ == '__main__': |
Guido van Rossum | 4b499dd3 | 2003-02-13 22:07:59 +0000 | [diff] [blame] | 308 | import pdb |
| 309 | pdb.run("interact()\n") |