Closely emulate the interactive Python console. The optional banner argument specifies the banner to print before the first interaction; by default it prints a banner similar to the one printed by the real Python interpreter, followed by the current class name i
(self, banner=None, exitmsg=None)
| 186 | self.buffer = [] |
| 187 | |
| 188 | def interact(self, banner=None, exitmsg=None): |
| 189 | """Closely emulate the interactive Python console. |
| 190 | |
| 191 | The optional banner argument specifies the banner to print |
| 192 | before the first interaction; by default it prints a banner |
| 193 | similar to the one printed by the real Python interpreter, |
| 194 | followed by the current class name in parentheses (so as not |
| 195 | to confuse this with the real interpreter -- since it's so |
| 196 | close!). |
| 197 | |
| 198 | The optional exitmsg argument specifies the exit message |
| 199 | printed when exiting. Pass the empty string to suppress |
| 200 | printing an exit message. If exitmsg is not given or None, |
| 201 | a default message is printed. |
| 202 | |
| 203 | """ |
| 204 | try: |
| 205 | sys.ps1 |
| 206 | except AttributeError: |
| 207 | sys.ps1 = ">>> " |
| 208 | try: |
| 209 | sys.ps2 |
| 210 | except AttributeError: |
| 211 | sys.ps2 = "... " |
| 212 | cprt = 'Type "help", "copyright", "credits" or "license" for more information.' |
| 213 | if banner is None: |
| 214 | self.write("Python %s on %s\n%s\n(%s)\n" % |
| 215 | (sys.version, sys.platform, cprt, |
| 216 | self.__class__.__name__)) |
| 217 | elif banner: |
| 218 | self.write("%s\n" % str(banner)) |
| 219 | more = 0 |
| 220 | while 1: |
| 221 | try: |
| 222 | if more: |
| 223 | prompt = sys.ps2 |
| 224 | else: |
| 225 | prompt = sys.ps1 |
| 226 | try: |
| 227 | line = self.raw_input(prompt) |
| 228 | except EOFError: |
| 229 | self.write("\n") |
| 230 | break |
| 231 | else: |
| 232 | more = self.push(line) |
| 233 | except KeyboardInterrupt: |
| 234 | self.write("\nKeyboardInterrupt\n") |
| 235 | self.resetbuffer() |
| 236 | more = 0 |
| 237 | if exitmsg is None: |
| 238 | self.write('now exiting %s...\n' % self.__class__.__name__) |
| 239 | elif exitmsg != '': |
| 240 | self.write('%s\n' % exitmsg) |
| 241 | |
| 242 | def push(self, line): |
| 243 | """Push a line to the interpreter. |