Skip to content

v2.4.2 #58

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 10 commits into from
Oct 28, 2018
Merged
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
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2012-2018 CS50

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:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

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.
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -10,11 +10,11 @@
"Topic :: Software Development :: Libraries :: Python Modules"
],
description="CS50 library for Python",
install_requires=["SQLAlchemy", "sqlparse", "termcolor"],
install_requires=["Flask>=1.0", "SQLAlchemy", "sqlparse", "termcolor"],
keywords="cs50",
name="cs50",
package_dir={"": "src"},
packages=["cs50"],
url="https://github.com/cs50/python-cs50",
version="2.4.1"
version="2.4.2"
)
29 changes: 3 additions & 26 deletions src/cs50/__init__.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,6 @@
import os
import sys


class CustomImporter(object):
"""
Import cs50.SQL lazily so that rest of library can be used without SQLAlchemy installed.

https://docs.python.org/3/library/imp.html
http://xion.org.pl/2012/05/06/hacking-python-imports/
http://dangerontheranger.blogspot.com/2012/07/how-to-use-sysmetapath-with-python.html
"""

def find_module(self, fullname, path=None):
if fullname == "cs50.SQL":
return self
return None

def load_module(self, name):
if name in sys.modules:
return sys.modules[name]
from .sql import SQL
sys.modules[name] = SQL
return SQL


try:

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

# Replace Flask's logger
from . import flask

# Lazily load CS50.SQL
sys.meta_path.append(CustomImporter())
# Wrap SQLAlchemy
from .sql import SQL

finally:

18 changes: 18 additions & 0 deletions src/cs50/cs50.py
Original file line number Diff line number Diff line change
@@ -28,7 +28,25 @@ def write(self, x):
self.f.flush()


class Reader:
"""
Disable buffering for input() as well.

https://bugs.python.org/issue24402
"""

def __getattr__(self, name):
return getattr(sys.__stdin__, name)

def fileno():
raise OSError()

def read(self, size):
return sys.__stdin__.read(size)


sys.stderr = flushfile(sys.stderr)
sys.stdin = Reader()
sys.stdout = flushfile(sys.stdout)


25 changes: 6 additions & 19 deletions src/cs50/flask.py
Original file line number Diff line number Diff line change
@@ -6,28 +6,15 @@
# Try to monkey-patch Flask, if installed
try:

# Only patch 0.12 (in case logging changes in 0.13)
# Only patch >= 1.0
version = StrictVersion(get_distribution("flask").version)
assert version >= StrictVersion("0.10") and version < StrictVersion("0.13")

# Get default logger
assert version >= StrictVersion("1.0")
import flask.logging
f = flask.logging.create_logger

def create_logger(app):
"""Wrap default logger"""

# Create default logger
logger = f(app)

# Reformat default logger's exceptions
# https://docs.python.org/3/library/logging.html#logging.Formatter.formatException
for handler in logger.handlers:
handler.formatter.formatException = lambda exc_info: formatException(*exc_info)
return logger

# Replace default logger
flask.logging.create_logger = create_logger
# Reformat logger's exceptions
# http://flask.pocoo.org/docs/1.0/logging/
# https://docs.python.org/3/library/logging.html#logging.Formatter.formatException
flask.logging.default_handler.formatter.formatException = lambda exc_info: formatException(*exc_info)

except:
pass
1 change: 1 addition & 0 deletions src/cs50/sql.py
Original file line number Diff line number Diff line change
@@ -42,6 +42,7 @@ def __init__(self, url, **kwargs):
# Enable foreign key constraints
if foreign_keys:
sqlalchemy.event.listen(self.engine, "connect", _connect)

else:

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