Skip to content

Commit c73d1d2

Browse files
authored
Merge pull request #180 from cs50/updates
updated IO wrapper, style, version
2 parents 781c1c2 + d981368 commit c73d1d2

File tree

5 files changed

+184
-77
lines changed

5 files changed

+184
-77
lines changed

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@
1818
package_dir={"": "src"},
1919
packages=["cs50"],
2020
url="https://github.com/cs50/python-cs50",
21-
version="9.3.1"
21+
version="9.3.2"
2222
)

src/cs50/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
# Import cs50_*
1010
from .cs50 import get_char, get_float, get_int, get_string
11+
1112
try:
1213
from .cs50 import get_long
1314
except ImportError:

src/cs50/cs50.py

+33-16
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717

1818
try:
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)
2123
except IndexError:
2224
pass
2325

@@ -37,26 +39,31 @@
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

6269
def _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

88101
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+
)
90105

91106

92107
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+
)
94111

95112

96113
def get_float(prompt):

src/cs50/flask.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import pkgutil
33
import sys
44

5+
56
def _wrap_flask(f):
67
if f is None:
78
return
@@ -17,10 +18,15 @@ def _wrap_flask(f):
1718

1819
if os.getenv("CS50_IDE_TYPE") == "online":
1920
from werkzeug.middleware.proxy_fix import ProxyFix
21+
2022
_flask_init_before = f.Flask.__init__
23+
2124
def _flask_init_after(self, *args, **kwargs):
2225
_flask_init_before(self, *args, **kwargs)
23-
self.wsgi_app = ProxyFix(self.wsgi_app, x_proto=1) # For HTTPS-to-HTTP proxy
26+
self.wsgi_app = ProxyFix(
27+
self.wsgi_app, x_proto=1
28+
) # For HTTPS-to-HTTP proxy
29+
2430
f.Flask.__init__ = _flask_init_after
2531

2632

@@ -30,7 +36,7 @@ def _flask_init_after(self, *args, **kwargs):
3036

3137
# If Flask wasn't imported
3238
else:
33-
flask_loader = pkgutil.get_loader('flask')
39+
flask_loader = pkgutil.get_loader("flask")
3440
if flask_loader:
3541
_exec_module_before = flask_loader.exec_module
3642

0 commit comments

Comments
 (0)