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.
| 175 | |
| 176 | |
| 177 | class InteractiveConsole(InteractiveInterpreter): |
| 178 | """Closely emulate the behavior of the interactive Python interpreter. |
| 179 | |
| 180 | This class builds on InteractiveInterpreter and adds prompting |
| 181 | using the familiar sys.ps1 and sys.ps2, and input buffering. |
| 182 | |
| 183 | """ |
| 184 | |
| 185 | def __init__(self, locals=None, filename="<console>", *, local_exit=False): |
| 186 | """Constructor. |
| 187 | |
| 188 | The optional locals argument will be passed to the |
| 189 | InteractiveInterpreter base class. |
| 190 | |
| 191 | The optional filename argument should specify the (file)name |
| 192 | of the input stream; it will show up in tracebacks. |
| 193 | |
| 194 | """ |
| 195 | InteractiveInterpreter.__init__(self, locals) |
| 196 | self.filename = filename |
| 197 | self.local_exit = local_exit |
| 198 | self.resetbuffer() |
| 199 | |
| 200 | def resetbuffer(self): |
| 201 | """Reset the input buffer.""" |
| 202 | self.buffer = [] |
| 203 | |
| 204 | def interact(self, banner=None, exitmsg=None): |
| 205 | """Closely emulate the interactive Python console. |
| 206 | |
| 207 | The optional banner argument specifies the banner to print |
| 208 | before the first interaction; by default it prints a banner |
| 209 | similar to the one printed by the real Python interpreter, |
| 210 | followed by the current class name in parentheses (so as not |
| 211 | to confuse this with the real interpreter -- since it's so |
| 212 | close!). |
| 213 | |
| 214 | The optional exitmsg argument specifies the exit message |
| 215 | printed when exiting. Pass the empty string to suppress |
| 216 | printing an exit message. If exitmsg is not given or None, |
| 217 | a default message is printed. |
| 218 | |
| 219 | """ |
| 220 | try: |
| 221 | sys.ps1 |
| 222 | delete_ps1_after = False |
| 223 | except AttributeError: |
| 224 | sys.ps1 = ">>> " |
| 225 | delete_ps1_after = True |
| 226 | try: |
| 227 | sys.ps2 |
| 228 | delete_ps2_after = False |
| 229 | except AttributeError: |
| 230 | sys.ps2 = "... " |
| 231 | delete_ps2_after = True |
| 232 | |
| 233 | cprt = 'Type "help", "copyright", "credits" or "license" for more information.' |
| 234 | if banner is None: |
no outgoing calls
no test coverage detected
searching dependent graphs…