diff options
author | Jack Jansen <jack.jansen@cwi.nl> | 1997-06-20 16:22:07 (GMT) |
---|---|---|
committer | Jack Jansen <jack.jansen@cwi.nl> | 1997-06-20 16:22:07 (GMT) |
commit | 1b3904b998d6f59a9ad88bda306f725c8c048f3c (patch) | |
tree | 26ce365837da451719b2c4c1c171662d94491ae6 /Mac | |
parent | 0b34dd51b554516265427a3f30ee18c06f8b381b (diff) | |
download | cpython-1b3904b998d6f59a9ad88bda306f725c8c048f3c.zip cpython-1b3904b998d6f59a9ad88bda306f725c8c048f3c.tar.gz cpython-1b3904b998d6f59a9ad88bda306f725c8c048f3c.tar.bz2 |
Module to buffer stdout/stderr until stdin is read. Useful for
windowing programs, together with option to keep console window closed
until needed.
Diffstat (limited to 'Mac')
-rw-r--r-- | Mac/Lib/quietconsole.py | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/Mac/Lib/quietconsole.py b/Mac/Lib/quietconsole.py new file mode 100644 index 0000000..21e7755 --- /dev/null +++ b/Mac/Lib/quietconsole.py @@ -0,0 +1,110 @@ +"""quietconsole - A module to keep console I/O quiet but dump it when needed""" +import types +import sys + +class _PseudoStdin: + def __init__(self, stdouterr): + self.keep_stdin = sys.stdin + sys.stdin = self + self.keep_stdouterr = stdouterr + + def __del__(self): + self.keep_stdin = self.keep_stdouterr = None + + def _revert(self): + """Return to old state, with true stdio""" + if self.keep_stdin == None: + return + sys.stdin = self.keep_stdin + self.keep_stdin = None + self.keep_stdouterr._revert(1) + self.keep_stdouterr = None + + def read(self, *args): + self._revert() + return apply(sys.stdin.read, args) + + def readlines(self, *args): + self._revert() + return apply(sys.stdin.readlines, args) + + def readline(self, *args): + self._revert() + return apply(sys.stdin.readline, args) + + def close(self): + self._revert() + sys.stdin.close() + +class _PseudoStdouterr: + def __init__(self): + self.keep_stdout = sys.stdout + self.keep_stderr = sys.stderr + sys.stdout = sys.stderr = self + self.data = [] + + def __del__(self): + self.keep_stdout = self.keep_stderr = None + + def _revert(self, dumpdata=0): + if self.keep_stdout == None: + return + sys.stdout = self.keep_stdout + sys.stderr = self.keep_stderr + sys.keep_stdout = self.keep_stderr = None + if dumpdata and self.data: + for d in self.data: + sys.stdout.write(d) + self.data = None + + def write(self, arg): + self.data.append(arg) + + def writelines(self, arg): + for d in arg: + self.data.append(arg) + + def close(self): + self.keep_stdout = self.keep_stderr = self.data = None + +beenhere = 0 + +def install(): + global beenhere + if beenhere: + return + beenhere = 1 + # There's no point in re-installing if the console has been active + obj = _PseudoStdouterr() + _PseudoStdin(obj) + # No need to keep the objects, they're saved in sys.std{in,out,err} + +def revert(): + if type(sys.stdin) == types.FileType: + return # Not installed + sys.stdin._revert() + +def _test(): + import time + install() + print "You will not see this yet" + time.sleep(1) + print "You will not see this yet" + time.sleep(1) + print "You will not see this yet" + time.sleep(1) + print "You will not see this yet" + time.sleep(1) + print "You will not see this yet" + time.sleep(1) + print "5 seconds have passed, now you may type something" + rv = sys.stdin.readline() + print "You typed", rv + +if __name__ == '__main__': + _test() + + + + + |