Skip to content

Commit 9183228

Browse files
authored
Merge pull request #106 from cs50/develop
v5.0.0
2 parents 6b15a87 + 19b86b3 commit 9183228

File tree

5 files changed

+233
-148
lines changed

5 files changed

+233
-148
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@
1616
package_dir={"": "src"},
1717
packages=["cs50"],
1818
url="https://github.com/cs50/python-cs50",
19-
version="4.0.4"
19+
version="5.0.0"
2020
)

src/cs50/__init__.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
1+
import logging
12
import os
23
import sys
34

4-
try:
5-
6-
# Save student's sys.path
7-
_path = sys.path[:]
85

9-
# In case student has files that shadow packages
10-
sys.path = [p for p in sys.path if p not in ("", os.getcwd())]
6+
# Disable cs50 logger by default
7+
logging.getLogger("cs50").disabled = True
118

12-
# Import cs50_*
13-
from .cs50 import get_char, get_float, get_int, get_string
9+
# In case student has files that shadow packages
10+
for p in ("", os.getcwd()):
1411
try:
15-
from .cs50 import get_long
16-
except ImportError:
12+
sys.path.remove(p)
13+
except ValueError:
1714
pass
1815

19-
# Replace Flask's logger
20-
from . import flask
21-
22-
# Wrap SQLAlchemy
23-
from .sql import SQL
16+
# Import cs50_*
17+
from .cs50 import get_char, get_float, get_int, get_string
18+
try:
19+
from .cs50 import get_long
20+
except ImportError:
21+
pass
2422

25-
finally:
23+
# Hook into flask importing
24+
from . import flask
2625

27-
# Restore student's sys.path (just in case library raised an exception that caller caught)
28-
sys.path = _path
26+
# Wrap SQLAlchemy
27+
from .sql import SQL

src/cs50/flask.py

Lines changed: 40 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,40 @@
1-
import logging
2-
3-
from distutils.version import StrictVersion
4-
from pkg_resources import get_distribution
5-
6-
from .cs50 import _formatException
7-
8-
# Try to monkey-patch Flask, if installed
9-
try:
10-
11-
# Only patch >= 1.0
12-
_version = StrictVersion(get_distribution("flask").version)
13-
assert _version >= StrictVersion("1.0")
14-
15-
# Reformat logger's exceptions
16-
# http://flask.pocoo.org/docs/1.0/logging/
17-
# https://docs.python.org/3/library/logging.html#logging.Formatter.formatException
18-
try:
19-
import flask.logging
20-
flask.logging.default_handler.formatter.formatException = lambda exc_info: _formatException(*exc_info)
21-
except Exception:
22-
pass
23-
24-
# Enable logging when Flask is in use,
25-
# monkey-patching own SQL module, which shouldn't need to know about Flask
26-
logging.getLogger("cs50").disabled = True
27-
try:
28-
import flask
29-
from .sql import SQL
30-
except ImportError:
31-
pass
32-
else:
33-
_execute_before = SQL.execute
34-
def _execute_after(*args, **kwargs):
35-
disabled = logging.getLogger("cs50").disabled
36-
if flask.current_app:
37-
logging.getLogger("cs50").disabled = False
38-
try:
39-
return _execute_before(*args, **kwargs)
40-
finally:
41-
logging.getLogger("cs50").disabled = disabled
42-
SQL.execute = _execute_after
43-
44-
# When behind CS50 IDE's proxy, ensure that flask.redirect doesn't redirect from HTTPS to HTTP
45-
# https://werkzeug.palletsprojects.com/en/0.15.x/middleware/proxy_fix/#module-werkzeug.middleware.proxy_fix
46-
from os import getenv
47-
if getenv("CS50_IDE_TYPE") == "online":
48-
try:
49-
import flask
50-
from werkzeug.middleware.proxy_fix import ProxyFix
51-
_flask_init_before = flask.Flask.__init__
52-
def _flask_init_after(self, *args, **kwargs):
53-
_flask_init_before(self, *args, **kwargs)
54-
self.wsgi_app = ProxyFix(self.wsgi_app, x_proto=1)
55-
flask.Flask.__init__ = _flask_init_after
56-
except:
57-
pass
58-
59-
except Exception:
60-
pass
1+
import os
2+
import pkgutil
3+
import sys
4+
5+
def _wrap_flask(f):
6+
if f is None:
7+
return
8+
9+
from distutils.version import StrictVersion
10+
from .cs50 import _formatException
11+
12+
if f.__version__ < StrictVersion("1.0"):
13+
return
14+
15+
f.logging.default_handler.formatter.formatException = lambda exc_info: _formatException(*exc_info)
16+
17+
if os.getenv("CS50_IDE_TYPE") == "online":
18+
from werkzeug.middleware.proxy_fix import ProxyFix
19+
_flask_init_before = f.Flask.__init__
20+
def _flask_init_after(self, *args, **kwargs):
21+
_flask_init_before(self, *args, **kwargs)
22+
self.wsgi_app = ProxyFix(self.wsgi_app, x_proto=1)
23+
f.Flask.__init__ = _flask_init_after
24+
25+
26+
# Flask was imported before cs50
27+
if "flask" in sys.modules:
28+
_wrap_flask(sys.modules["flask"])
29+
30+
# Flask wasn't imported
31+
else:
32+
flask_loader = pkgutil.get_loader('flask')
33+
if flask_loader:
34+
_exec_module_before = flask_loader.exec_module
35+
36+
def _exec_module_after(*args, **kwargs):
37+
_exec_module_before(*args, **kwargs)
38+
_wrap_flask(sys.modules["flask"])
39+
40+
flask_loader.exec_module = _exec_module_after

0 commit comments

Comments
 (0)