def cmdloop(self): """Implement an interactive command interpreter which grabs a line of input and passes it to onecmd() until the postcmd() function returns True. This method will also: * Execute a preloop() method before starting the interpreter * Install a complete() readline completer function * Write the string intro followed by a newline to stdout * Read from a list of commands called cmdqueue, or * Read from stdin, and * Call precmd() with the line as an argument, * Call onecmd() with the line as an argument, * Call postcmd() with the stop flag and the line as an argument. * Finally, restore the previous readline completer, if any. """ self.preloop() old_completer = readline.get_completer() readline.set_completer(self.complete) readline.parse_and_bind(self.completekey + ": complete") try: if self.intro: self.stdout.write(str(self.intro) + "\n") stop = None while not stop: if self.cmdqueue: line = self.cmdqueue.pop() else: try: line = self.inp(self.prompt) except EOFError: self.stdout.write("\n") line = 'EOF' except KeyboardInterrupt as exc: self.ctrl_c(exc) self.cancel() continue try: line = self.precmd(line) stop = self.onecmd(line) stop = self.postcmd(stop, line) except KeyboardInterrupt as exc: self.ctrl_c(exc) self.cancel() self.postloop() finally: readline.set_completer(old_completer)
def parse_and_bind(self, line): """Parse one line of a readline initialization file.""" readline.parse_and_bind(line)
def test_parse_and_bind_empty_string(self): self.assertEqual(readline.parse_and_bind(''), None)