Closely emulate the behavior of the interactive Python interpreter. This class builds on InteractiveInterpreter and adds prompting using the familiar sys.ps1 and sys.ps2, and input buffering.
| 402 | |
| 403 | |
| 404 | class InteractiveConsole(InteractiveInterpreter): |
| 405 | """Closely emulate the behavior of the interactive Python interpreter. |
| 406 | |
| 407 | This class builds on InteractiveInterpreter and adds prompting |
| 408 | using the familiar sys.ps1 and sys.ps2, and input buffering. |
| 409 | |
| 410 | """ |
| 411 | |
| 412 | def __init__(self, locals=None, filename="<console>"): |
| 413 | """Constructor. |
| 414 | |
| 415 | The optional locals argument will be passed to the |
| 416 | InteractiveInterpreter base class. |
| 417 | |
| 418 | The optional filename argument should specify the (file)name |
| 419 | of the input stream; it will show up in tracebacks. |
| 420 | |
| 421 | """ |
| 422 | InteractiveInterpreter.__init__(self, locals) |
| 423 | self.filename = filename |
| 424 | self.resetbuffer() |
| 425 | |
| 426 | def resetbuffer(self): |
| 427 | """Reset the input buffer.""" |
| 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: |