Where do stdout and stderr go when in curses mode?

情到浓时终转凉″ 提交于 2021-02-08 14:21:25

问题


Where do stdout and stderr go when curses is active?

import curses, sys

def test_streams():
    print "stdout"
    print >>sys.stderr, "stderr"

def curses_mode(stdscr):
    test_streams()

test_streams()
curses.wrapper(curses_mode)

Actual output is

stdout
stderr

Update0

Expected output is

stdout
stderr
stdout
stderr

entering, and then exiting curses mode with no change to the final text shown in the terminal.


回答1:


Activating curses saves the terminal text screen's current contents and clears said screen; exiting curses restores the screen's contents (tossing away whatever's been put on screen during the reign of curses itself). Try with this variant of your code and you'll see better what's happening:

import curses, sys, time

def test_streams(wot):
    print wot, "stdout"
    print >>sys.stderr, wot, "stderr"

def curses_mode(stdscr):
    test_streams("wrap")
    time.sleep(1.0)

test_streams("before")
curses.wrapper(curses_mode)
test_streams("after")

You'll note the wrap stderr on the screen for a second (during the sleep) -- it's overwritten the stdout part -- then it disappears and you see the four before and after lines on the now-quiescent screen (you can add other sleeps to follow what's happening in even more details, if you care).



来源:https://stackoverflow.com/questions/2233006/where-do-stdout-and-stderr-go-when-in-curses-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!