Skip to content

Commit

Permalink
Merge pull request #180 from cs50/updates
Browse files Browse the repository at this point in the history
updated IO wrapper, style, version
  • Loading branch information
dmalan authored Dec 17, 2023
2 parents 781c1c2 + d981368 commit c73d1d2
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 77 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
package_dir={"": "src"},
packages=["cs50"],
url="https://github.com/cs50/python-cs50",
version="9.3.1"
version="9.3.2"
)
1 change: 1 addition & 0 deletions src/cs50/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

# Import cs50_*
from .cs50 import get_char, get_float, get_int, get_string

try:
from .cs50 import get_long
except ImportError:
Expand Down
49 changes: 33 additions & 16 deletions src/cs50/cs50.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

try:
# Patch formatException
logging.root.handlers[0].formatter.formatException = lambda exc_info: _formatException(*exc_info)
logging.root.handlers[
0
].formatter.formatException = lambda exc_info: _formatException(*exc_info)
except IndexError:
pass

Expand All @@ -37,26 +39,31 @@
_logger.addHandler(handler)


class _flushfile():
class _Unbuffered:
"""
Disable buffering for standard output and standard error.
http://stackoverflow.com/a/231216
https://stackoverflow.com/a/107717
https://docs.python.org/3/library/io.html
"""

def __init__(self, f):
self.f = f
def __init__(self, stream):
self.stream = stream

def __getattr__(self, name):
return getattr(self.f, name)
def __getattr__(self, attr):
return getattr(self.stream, attr)

def write(self, x):
self.f.write(x)
self.f.flush()
def write(self, b):
self.stream.write(b)
self.stream.flush()

def writelines(self, lines):
self.stream.writelines(lines)
self.stream.flush()

sys.stderr = _flushfile(sys.stderr)
sys.stdout = _flushfile(sys.stdout)

sys.stderr = _Unbuffered(sys.stderr)
sys.stdout = _Unbuffered(sys.stdout)


def _formatException(type, value, tb):
Expand All @@ -78,19 +85,29 @@ def _formatException(type, value, tb):
lines += line
else:
matches = re.search(r"^(\s*)(.*?)(\s*)$", line, re.DOTALL)
lines.append(matches.group(1) + colored(matches.group(2), "yellow") + matches.group(3))
lines.append(
matches.group(1)
+ colored(matches.group(2), "yellow")
+ matches.group(3)
)
return "".join(lines).rstrip()


sys.excepthook = lambda type, value, tb: print(_formatException(type, value, tb), file=sys.stderr)
sys.excepthook = lambda type, value, tb: print(
_formatException(type, value, tb), file=sys.stderr
)


def eprint(*args, **kwargs):
raise RuntimeError("The CS50 Library for Python no longer supports eprint, but you can use print instead!")
raise RuntimeError(
"The CS50 Library for Python no longer supports eprint, but you can use print instead!"
)


def get_char(prompt):
raise RuntimeError("The CS50 Library for Python no longer supports get_char, but you can use get_string instead!")
raise RuntimeError(
"The CS50 Library for Python no longer supports get_char, but you can use get_string instead!"
)


def get_float(prompt):
Expand Down
10 changes: 8 additions & 2 deletions src/cs50/flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pkgutil
import sys


def _wrap_flask(f):
if f is None:
return
Expand All @@ -17,10 +18,15 @@ def _wrap_flask(f):

if os.getenv("CS50_IDE_TYPE") == "online":
from werkzeug.middleware.proxy_fix import ProxyFix

_flask_init_before = f.Flask.__init__

def _flask_init_after(self, *args, **kwargs):
_flask_init_before(self, *args, **kwargs)
self.wsgi_app = ProxyFix(self.wsgi_app, x_proto=1) # For HTTPS-to-HTTP proxy
self.wsgi_app = ProxyFix(
self.wsgi_app, x_proto=1
) # For HTTPS-to-HTTP proxy

f.Flask.__init__ = _flask_init_after


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

# If Flask wasn't imported
else:
flask_loader = pkgutil.get_loader('flask')
flask_loader = pkgutil.get_loader("flask")
if flask_loader:
_exec_module_before = flask_loader.exec_module

Expand Down
Loading

0 comments on commit c73d1d2

Please sign in to comment.