import readline as rl def setup_readline(): if 'libedit' in rl.__doc__: rl.parse_and_bind('bind ^I rl_complete') else: rl.parse_and_bind('tab: complete') def repl(): print( 'This script demonstrates an issue with the readline module.', 'Enter arbitrary text into stdin to be added to the input history.', 'Available commands include \'history\' and \'quit\'.', 'Upon termination of this REPL, the history will be displayed.', 'Note that non-empty lines are being added twice.', sep='\n' ) try: while True: line = input('--> ') if line.isupper(): print('ERROR: Is your caps lock on?') elif line == 'history': display_history() rl.add_history(line) elif line == 'quit': raise EOFError else: rl.add_history(line) except EOFError: print() def _get_history(): for i in range(1, rl.get_current_history_length() + 1): yield rl.get_history_item(i) def display_history(): for index, line in enumerate(_get_history(), start=1): print(index, ': ', line, sep='') if __name__ == '__main__': setup_readline() repl() display_history()