1717
1818try :
1919 # 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 )
2123except IndexError :
2224 pass
2325
3739_logger .addHandler (handler )
3840
3941
40- class _flushfile () :
42+ class _Unbuffered :
4143 """
4244 Disable buffering for standard output and standard error.
4345
44- http://stackoverflow.com/a/231216
46+ https://stackoverflow.com/a/107717
47+ https://docs.python.org/3/library/io.html
4548 """
4649
47- def __init__ (self , f ):
48- self .f = f
50+ def __init__ (self , stream ):
51+ self .stream = stream
4952
50- def __getattr__ (self , name ):
51- return getattr (self .f , name )
53+ def __getattr__ (self , attr ):
54+ return getattr (self .stream , attr )
5255
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 ()
5659
60+ def writelines (self , lines ):
61+ self .stream .writelines (lines )
62+ self .stream .flush ()
5763
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 )
6067
6168
6269def _formatException (type , value , tb ):
@@ -78,19 +85,29 @@ def _formatException(type, value, tb):
7885 lines += line
7986 else :
8087 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+ )
8293 return "" .join (lines ).rstrip ()
8394
8495
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+ )
8699
87100
88101def 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+ )
90105
91106
92107def 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+ )
94111
95112
96113def get_float (prompt ):
0 commit comments