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 in par
(self, banner=None, exitmsg=None)
| 428 | self.buffer = [] |
| 429 | |
| 430 | def interact(self, banner=None, exitmsg=None): |
| 431 | """Closely emulate the interactive Python console. |
| 432 | |
| 433 | The optional banner argument specifies the banner to print |
| 434 | before the first interaction; by default it prints a banner |
| 435 | similar to the one printed by the real Python interpreter, |
| 436 | followed by the current class name in parentheses (so as not |
| 437 | to confuse this with the real interpreter -- since it's so |
| 438 | close!). |
| 439 | |
| 440 | The optional exitmsg argument specifies the exit message |
| 441 | printed when exiting. Pass the empty string to suppress |
| 442 | printing an exit message. If exitmsg is not given or None, |
| 443 | a default message is printed. |
| 444 | |
| 445 | """ |
| 446 | try: |
| 447 | sys.ps1 |
| 448 | except AttributeError: |
| 449 | sys.ps1 = ">>> " |
| 450 | try: |
| 451 | sys.ps2 |
| 452 | except AttributeError: |
| 453 | sys.ps2 = "... " |
| 454 | cprt = 'Type "help", "copyright", "credits" or "license" for more information.' |
| 455 | if banner is None: |
| 456 | self.write("Python %s on %s\n%s\n(%s)\n" % (sys.version, sys.platform, cprt, self.__class__.__name__)) |
| 457 | elif banner: |
| 458 | self.write("%s\n" % str(banner)) |
| 459 | more = 0 |
| 460 | while 1: |
| 461 | try: |
| 462 | if more: |
| 463 | prompt = sys.ps2 |
| 464 | else: |
| 465 | prompt = sys.ps1 |
| 466 | try: |
| 467 | line = self.raw_input(prompt) |
| 468 | except EOFError: |
| 469 | self.write("\n") |
| 470 | break |
| 471 | else: |
| 472 | more = self.push(line) |
| 473 | except KeyboardInterrupt: |
| 474 | self.write("\nKeyboardInterrupt\n") |
| 475 | self.resetbuffer() |
| 476 | more = 0 |
| 477 | if exitmsg is None: |
| 478 | self.write("now exiting %s...\n" % self.__class__.__name__) |
| 479 | elif exitmsg != "": |
| 480 | self.write("%s\n" % exitmsg) |
| 481 | |
| 482 | def push(self, line): |
| 483 | """Push a line to the interpreter. |
no test coverage detected