17
17
18
18
try :
19
19
# Patch formatException
20
- logging .root .handlers [0 ].formatter .formatException = lambda exc_info : _formatException (* exc_info )
20
+ logging .root .handlers [
21
+ 0
22
+ ].formatter .formatException = lambda exc_info : _formatException (* exc_info )
21
23
except IndexError :
22
24
pass
23
25
37
39
_logger .addHandler (handler )
38
40
39
41
40
- class _flushfile () :
42
+ class _Unbuffered :
41
43
"""
42
44
Disable buffering for standard output and standard error.
43
45
44
- http://stackoverflow.com/a/231216
46
+ https://stackoverflow.com/a/107717
47
+ https://docs.python.org/3/library/io.html
45
48
"""
46
49
47
- def __init__ (self , f ):
48
- self .f = f
50
+ def __init__ (self , stream ):
51
+ self .stream = stream
49
52
50
- def __getattr__ (self , name ):
51
- return getattr (self .f , name )
53
+ def __getattr__ (self , attr ):
54
+ return getattr (self .stream , attr )
52
55
53
- def write (self , x ):
54
- self .f .write (x )
55
- self .f .flush ()
56
+ def write (self , b ):
57
+ self .stream .write (b )
58
+ self .stream .flush ()
56
59
60
+ def writelines (self , lines ):
61
+ self .stream .writelines (lines )
62
+ self .stream .flush ()
57
63
58
- sys .stderr = _flushfile (sys .stderr )
59
- sys .stdout = _flushfile (sys .stdout )
64
+
65
+ sys .stderr = _Unbuffered (sys .stderr )
66
+ sys .stdout = _Unbuffered (sys .stdout )
60
67
61
68
62
69
def _formatException (type , value , tb ):
@@ -78,19 +85,29 @@ def _formatException(type, value, tb):
78
85
lines += line
79
86
else :
80
87
matches = re .search (r"^(\s*)(.*?)(\s*)$" , line , re .DOTALL )
81
- lines .append (matches .group (1 ) + colored (matches .group (2 ), "yellow" ) + matches .group (3 ))
88
+ lines .append (
89
+ matches .group (1 )
90
+ + colored (matches .group (2 ), "yellow" )
91
+ + matches .group (3 )
92
+ )
82
93
return "" .join (lines ).rstrip ()
83
94
84
95
85
- sys .excepthook = lambda type , value , tb : print (_formatException (type , value , tb ), file = sys .stderr )
96
+ sys .excepthook = lambda type , value , tb : print (
97
+ _formatException (type , value , tb ), file = sys .stderr
98
+ )
86
99
87
100
88
101
def eprint (* args , ** kwargs ):
89
- raise RuntimeError ("The CS50 Library for Python no longer supports eprint, but you can use print instead!" )
102
+ raise RuntimeError (
103
+ "The CS50 Library for Python no longer supports eprint, but you can use print instead!"
104
+ )
90
105
91
106
92
107
def get_char (prompt ):
93
- raise RuntimeError ("The CS50 Library for Python no longer supports get_char, but you can use get_string instead!" )
108
+ raise RuntimeError (
109
+ "The CS50 Library for Python no longer supports get_char, but you can use get_string instead!"
110
+ )
94
111
95
112
96
113
def get_float (prompt ):
0 commit comments