import atexit import os import sys def wrap_output(height, width): buf = [[]] def flush(): if hasattr(sys.stdout, '_stream'): sys.stdout = sys.stdout._stream return buf[0] = [] #atexit.register(flush) line_count = [0] col_count = [0] class FileProxy(object): def __init__(self, stream): self._stream = stream def __getattr__(self, name): return getattr(self._stream, name) def write(self, s): for c in s: if c == '\n': line_count[0] += 1 col_count[0] = 0 if line_count[0] > height: flush() sys.stdout = FileProxy(sys.stdout) def foo(): pass if __name__ == '__main__': wrap_output(23, 80) for i in xrange(24): print '-' * 80