Skip to content

Commit f2878e1

Browse files
author
Kareem Zidane
authoredOct 28, 2018
Merge pull request #58 from cs50/develop
v2.4.2
2 parents c538744 + d03d296 commit f2878e1

File tree

6 files changed

+37
-47
lines changed

6 files changed

+37
-47
lines changed
 

‎LICENSE

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright 2012-2018 CS50
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

‎setup.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
"Topic :: Software Development :: Libraries :: Python Modules"
1111
],
1212
description="CS50 library for Python",
13-
install_requires=["SQLAlchemy", "sqlparse", "termcolor"],
13+
install_requires=["Flask>=1.0", "SQLAlchemy", "sqlparse", "termcolor"],
1414
keywords="cs50",
1515
name="cs50",
1616
package_dir={"": "src"},
1717
packages=["cs50"],
1818
url="https://github.com/cs50/python-cs50",
19-
version="2.4.1"
19+
version="2.4.2"
2020
)

‎src/cs50/__init__.py

+3-26
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,6 @@
11
import os
22
import sys
33

4-
5-
class CustomImporter(object):
6-
"""
7-
Import cs50.SQL lazily so that rest of library can be used without SQLAlchemy installed.
8-
9-
https://docs.python.org/3/library/imp.html
10-
http://xion.org.pl/2012/05/06/hacking-python-imports/
11-
http://dangerontheranger.blogspot.com/2012/07/how-to-use-sysmetapath-with-python.html
12-
"""
13-
14-
def find_module(self, fullname, path=None):
15-
if fullname == "cs50.SQL":
16-
return self
17-
return None
18-
19-
def load_module(self, name):
20-
if name in sys.modules:
21-
return sys.modules[name]
22-
from .sql import SQL
23-
sys.modules[name] = SQL
24-
return SQL
25-
26-
274
try:
285

296
# Save student's sys.path
@@ -36,14 +13,14 @@ def load_module(self, name):
3613
from .cs50 import eprint, get_char, get_float, get_int, get_string
3714
try:
3815
from .cs50 import get_long
39-
except Exception:
16+
except ImportError:
4017
pass
4118

4219
# Replace Flask's logger
4320
from . import flask
4421

45-
# Lazily load CS50.SQL
46-
sys.meta_path.append(CustomImporter())
22+
# Wrap SQLAlchemy
23+
from .sql import SQL
4724

4825
finally:
4926

‎src/cs50/cs50.py

+18
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,25 @@ def write(self, x):
2828
self.f.flush()
2929

3030

31+
class Reader:
32+
"""
33+
Disable buffering for input() as well.
34+
35+
https://bugs.python.org/issue24402
36+
"""
37+
38+
def __getattr__(self, name):
39+
return getattr(sys.__stdin__, name)
40+
41+
def fileno():
42+
raise OSError()
43+
44+
def read(self, size):
45+
return sys.__stdin__.read(size)
46+
47+
3148
sys.stderr = flushfile(sys.stderr)
49+
sys.stdin = Reader()
3250
sys.stdout = flushfile(sys.stdout)
3351

3452

‎src/cs50/flask.py

+6-19
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,15 @@
66
# Try to monkey-patch Flask, if installed
77
try:
88

9-
# Only patch 0.12 (in case logging changes in 0.13)
9+
# Only patch >= 1.0
1010
version = StrictVersion(get_distribution("flask").version)
11-
assert version >= StrictVersion("0.10") and version < StrictVersion("0.13")
12-
13-
# Get default logger
11+
assert version >= StrictVersion("1.0")
1412
import flask.logging
15-
f = flask.logging.create_logger
16-
17-
def create_logger(app):
18-
"""Wrap default logger"""
19-
20-
# Create default logger
21-
logger = f(app)
22-
23-
# Reformat default logger's exceptions
24-
# https://docs.python.org/3/library/logging.html#logging.Formatter.formatException
25-
for handler in logger.handlers:
26-
handler.formatter.formatException = lambda exc_info: formatException(*exc_info)
27-
return logger
2813

29-
# Replace default logger
30-
flask.logging.create_logger = create_logger
14+
# Reformat logger's exceptions
15+
# http://flask.pocoo.org/docs/1.0/logging/
16+
# https://docs.python.org/3/library/logging.html#logging.Formatter.formatException
17+
flask.logging.default_handler.formatter.formatException = lambda exc_info: formatException(*exc_info)
3118

3219
except:
3320
pass

‎src/cs50/sql.py

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def __init__(self, url, **kwargs):
4242
# Enable foreign key constraints
4343
if foreign_keys:
4444
sqlalchemy.event.listen(self.engine, "connect", _connect)
45+
4546
else:
4647

4748
# Create engine, raising exception if back end's module not installed

0 commit comments

Comments
 (0)