Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sage
Path: blob/develop/build/sage_bootstrap/stdio.py
4052 views
1
# -*- coding: utf-8 -*-
2
"""
3
Set Up Input/output
4
5
Output should always be unbuffered so that it appears immediately on
6
the terminal.
7
"""
8
9
# ****************************************************************************
10
# Copyright (C) 2015 Volker Braun <[email protected]>
11
#
12
# This program is free software: you can redistribute it and/or modify
13
# it under the terms of the GNU General Public License as published by
14
# the Free Software Foundation, either version 2 of the License, or
15
# (at your option) any later version.
16
# https://www.gnu.org/licenses/
17
# ****************************************************************************
18
import sys
19
20
21
class UnbufferedStream(object):
22
23
def __init__(self, stream):
24
self.stream = stream
25
26
def write(self, data):
27
self.stream.write(data)
28
self.stream.flush()
29
30
def __getattr__(self, attr):
31
return getattr(self.stream, attr)
32
33
34
REAL_STDOUT = sys.stdout
35
REAL_STDERR = sys.stderr
36
37
38
def init_streams(config):
39
if not config.interactive:
40
sys.stdout = UnbufferedStream(REAL_STDOUT)
41
42
43
def flush():
44
REAL_STDOUT.flush()
45
REAL_STDERR.flush()
46
47