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.
| 160 | |
| 161 | |
| 162 | class InteractiveConsole(InteractiveInterpreter): |
| 163 | """Closely emulate the behavior of the interactive Python interpreter. |
| 164 | |
| 165 | This class builds on InteractiveInterpreter and adds prompting |
| 166 | using the familiar sys.ps1 and sys.ps2, and input buffering. |
| 167 | |
| 168 | """ |
| 169 | |
| 170 | def __init__(self, locals=None, filename="<console>"): |
| 171 | """Constructor. |
| 172 | |
| 173 | The optional locals argument will be passed to the |
| 174 | InteractiveInterpreter base class. |
| 175 | |
| 176 | The optional filename argument should specify the (file)name |
| 177 | of the input stream; it will show up in tracebacks. |
| 178 | |
| 179 | """ |
| 180 | InteractiveInterpreter.__init__(self, locals) |
| 181 | self.filename = filename |
| 182 | self.resetbuffer() |
| 183 | |
| 184 | def resetbuffer(self): |
| 185 | """Reset the input buffer.""" |
| 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 |