python - RuntimeError: can't re-enter readline -
i making python 3 app has prompt user enters commands. i'm running python 3.5 on macos sierra.
i'm trying prevent exiting ctrl+c
, having users exit instead typing exit
prompt.
i doing using method: https://stackoverflow.com/a/6019460/7450368
however, when prompting user again, runtimeerror: can't re-enter readline
.
is there way can fix this?
here's code:
import interpret import signal def siginthand(sig, frm): print("\nuse 'exit' exit.") prompt() signal.signal(signal.sigint, siginthand) version = ("v0.0.3") class commanderror(exception): pass def prompt(): try: task = input("einstein " + version + " > ") except runtimeerror: task = input("einstein " + version + " > ") try: execute(task) except commanderror e: print(e) prompt() def execute(command): command = command.lower() interpret.interpret(command) prompt()
interpret.interpret("command")
called main.py
execute command command
if exists.
any hugely appreciated. thanks!
your control flow looks bit strange. handle user pressing ctrl+c
more once, should use loop keep asking input.
also, can catch keyboardinterrupt
in python gets raised every time hit ctrl+c
. it's bit easier messing signal handlers.
version = ("v0.0.3") class commanderror(exception): pass def prompt(): try: return input("einstein " + version + " > ") except keyboardinterrupt: print('') # go next line printing looks better. return none def force_prompt(): task = none while task none: task = prompt() return task def get_and_execute_command(): task = force_prompt() try: execute(task) except commanderror e: print(e) def execute(command): command = command.lower() print('command: %s' % command) get_and_execute_command()
Comments
Post a Comment