File: stdio.py

package info (click to toggle)
sagemath 9.5-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 144,264 kB
  • sloc: python: 1,113,444; cpp: 37,499; ansic: 4,606; sh: 4,105; makefile: 1,097; javascript: 326; lisp: 5
file content (45 lines) | stat: -rw-r--r-- 1,172 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# -*- coding: utf-8 -*-
"""
Set Up Input/output

Output should always be unbuffered so that it appears immediately on
the terminal.
"""

# ****************************************************************************
#       Copyright (C) 2015 Volker Braun <vbraun.name@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#                  https://www.gnu.org/licenses/
# ****************************************************************************
import sys


class UnbufferedStream(object):
    
    def __init__(self, stream):
        self.stream = stream
       
    def write(self, data):
        self.stream.write(data)
        self.stream.flush()
       
    def __getattr__(self, attr):
        return getattr(self.stream, attr)


REAL_STDOUT = sys.stdout
REAL_STDERR = sys.stderr


def init_streams(config):
    if not config.interactive:
        sys.stdout = UnbufferedStream(REAL_STDOUT)


def flush():
    REAL_STDOUT.flush()
    REAL_STDERR.flush()