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)
| 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: |
| 235 | self.write("Python %s on %s\n%s\n(%s)\n" % |
| 236 | (sys.version, sys.platform, cprt, |
| 237 | self.__class__.__name__)) |
| 238 | elif banner: |
| 239 | self.write("%s\n" % str(banner)) |
| 240 | more = 0 |
| 241 | |
| 242 | # When the user uses exit() or quit() in their interactive shell |
| 243 | # they probably just want to exit the created shell, not the whole |
| 244 | # process. exit and quit in builtins closes sys.stdin which makes |
| 245 | # it super difficult to restore |
| 246 | # |
| 247 | # When self.local_exit is True, we overwrite the builtins so |
| 248 | # exit() and quit() only raises SystemExit and we can catch that |
| 249 | # to only exit the interactive shell |
| 250 | |
| 251 | _exit = None |
| 252 | _quit = None |
| 253 | |
| 254 | if self.local_exit: |
| 255 | if hasattr(builtins, "exit"): |
| 256 | _exit = builtins.exit |
| 257 | builtins.exit = Quitter("exit") |
| 258 | |
| 259 | if hasattr(builtins, "quit"): |
| 260 | _quit = builtins.quit |
| 261 | builtins.quit = Quitter("quit") |