Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 诅咒,线的尽头断了_Python_Python 2.7_Ncurses_Curses_Python Curses - Fatal编程技术网

Python 诅咒,线的尽头断了

Python 诅咒,线的尽头断了,python,python-2.7,ncurses,curses,python-curses,Python,Python 2.7,Ncurses,Curses,Python Curses,我用它来开发一个界面。在开发过程中,它经常崩溃,并在stdout或stderr上抛出随机错误 输出格式不正确;错误应如下所示: Error in line 100: Exception foo called from bar 看起来像: Error in line 100: Exception foo called from bar 因此,显然\n没有得到应有的解释(看起来它需要\r)。我通过重

我用它来开发一个界面。在开发过程中,它经常崩溃,并在
stdout
stderr
上抛出随机错误

输出格式不正确;错误应如下所示:

Error in line 100:
Exception foo
called from bar
看起来像:

Error in line 100:
                  Exception foo
                               called from bar
因此,显然
\n
没有得到应有的解释(看起来它需要
\r
)。我通过重定向
stderr
到文件或其他终端来处理这个问题,但它能在代码中修复吗

编辑

这是我的一段代码(curses用户界面“包装器”的一部分)


听起来你没有使用诅咒。包装器

比较

class MyApp:
    def __init__(self, stdscr):
        raise Exception('Arggh')

if __name__ == '__main__':
    curses.wrapper(MyApp)        # This will print your error properly
    # MyApp(curses.initscr())    # This gives the behavior you see
您发布的示例没有显示如何调用
\u setup
\u restore
。在使用
try:>打印回溯之前,必须确保调用了
close
\u restore
方法。。。最后:…
block

class CursesUI(object):
    def __init__(self):
        try:
            self._stdscr = self._setup()
            self.main()
        finally:
            self._restore()

    def main(self):
        curses.napms(500)
        raise Exception('Arggh')
使用
\u设置
\u还原
方法,可以正确打印回溯

编辑:

至于预期的回车,您是正确的。
\n
被解释为向下移动一列,
\r
返回到行开始处。因此,如果您是手动打印,您可以使用
sys.stdout.write('msg\r\n')
,但是我假设您只是想在程序崩溃时正确读取错误

再次编辑:
更新了包装器示例以匹配您发布的示例。

我已经在使用包装器了。我不想手动发出
\r\n
,因为代码应该与POSIX和Windows@JakubM. 你是在使用PDC课程还是其他什么?Curses本身仅限于POSIX,
\r\n
不应限制平台。另外,如果您使用的是包装器,那么它没有正确调用
curses.endwin()
。你能发布一个使用包装器打印不正确的最小示例吗?我编辑了问题并添加了包装器调用。我没有添加格式错误的输出,因为它会产生V字形的长线,因此无法正确显示它们
class CursesUI(object):
    def __init__(self):
        try:
            self._stdscr = self._setup()
            self.main()
        finally:
            self._restore()

    def main(self):
        curses.napms(500)
        raise Exception('Arggh')