Skip to content

updated IO wrapper, style, version #180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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