RuntimeError:无法重新输入readline

时间:2017-03-28 11:10:19

标签: python runtime-error

我正在制作一个Python 3应用程序,其中包含用户输入命令的提示。我在macOS Sierra上运行Python 3.5。

我尝试阻止退出Ctrl+C并让用户退出,只需在提示符中输入exit即可。

我使用此方法执行此操作:https://stackoverflow.com/a/6019460/7450368

但是,再次提示用户时,我会收到RuntimeError: can't re-enter readline

我有办法解决这个问题吗?

这是我的代码:

import interpret
import signal
def SigIntHand(SIG, FRM):
    print("\nUse 'exit' to 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 as e:
        print(e)
    prompt()


def execute(command):
    command = command.lower()
    interpret.interpret(command)

prompt()
interpret.interpret("command")调用

main.py来执行命令command(如果存在)。

非常感谢任何帮助。谢谢!

1 个答案:

答案 0 :(得分:0)

你的控制流看起来有点奇怪。要处理用户多次按Ctrl+C,您应该使用循环来继续请求输入。

此外,您可以在Python中捕获KeyboardInterrupt,每次点击Ctrl+C时都会引发该version = ("v0.0.3") class CommandError(Exception): pass def prompt(): try: return input("Einstein " + version + " > ") except KeyboardInterrupt: print('') # Go to the next line so printing looks better. return None def force_prompt(): task = None while task is None: task = prompt() return task def get_and_execute_command(): task = force_prompt() try: execute(task) except CommandError as e: print(e) def execute(command): command = command.lower() print('Command: %s' % command) get_and_execute_command() 。它比搞乱信号处理程序容易一些。

<namespaces>
        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.WebPages" />
        <add namespace="Kendo.Mvc.UI" />
      </namespaces>