Skip to content

Commit 12747c7

Browse files
authored
Merge pull request #73 from cs50/3.2
adds support for other paramstyles, disables logging by default when Flask not running
2 parents f1dbb26 + 4ef94a2 commit 12747c7

File tree

10 files changed

+528
-187
lines changed

10 files changed

+528
-187
lines changed

.travis.yml

+20-31
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,30 @@
11
language: python
2-
python:
3-
- '2.7'
4-
- '3.6'
2+
python: '3.6'
53
branches:
64
except: "/^v\\d/"
75
services:
8-
- mysql
9-
- postgresql
6+
- mysql
7+
- postgresql
108
install:
11-
- python setup.py install
12-
- pip install mysqlclient
13-
- pip install psycopg2-binary
9+
- python setup.py install
10+
- pip install mysqlclient
11+
- pip install psycopg2-binary
1412
before_script:
15-
- mysql -e 'CREATE DATABASE IF NOT EXISTS test;'
16-
- psql -c 'create database test;' -U postgres
17-
- touch test.db test1.db
13+
- mysql -e 'CREATE DATABASE IF NOT EXISTS test;'
14+
- psql -c 'create database test;' -U postgres
15+
- touch test.db test1.db
1816
script: python tests/sql.py
19-
after_script: rm -f test.db
20-
jobs:
21-
include:
22-
- stage: deploy
23-
python: '3.6'
24-
install: skip
25-
before_script: skip
26-
script: skip
27-
deploy:
28-
- provider: script
29-
script: 'curl --fail --data "{ \"tag_name\": \"v$(python setup.py --version)\",
30-
\"target_commitish\": \"$TRAVIS_COMMIT\", \"name\": \"v$(python setup.py --version)\"
31-
}" --user bot50:$GITHUB_TOKEN https://api.github.com/repos/$TRAVIS_REPO_SLUG/releases'
32-
on:
33-
branch: master
34-
- provider: pypi
35-
user: "$PYPI_USERNAME"
36-
password: "$PYPI_PASSWORD"
37-
on:
38-
branch: master
17+
deploy:
18+
- provider: script
19+
script: 'curl --fail --data "{ \"tag_name\": \"v$(python setup.py --version)\",
20+
\"target_commitish\": \"$TRAVIS_COMMIT\", \"name\": \"v$(python setup.py --version)\"
21+
}" --user bot50:$GITHUB_TOKEN https://api.github.com/repos/$TRAVIS_REPO_SLUG/releases'
22+
on:
23+
branch: master
24+
- provider: pypi
25+
user: "$PYPI_USERNAME"
26+
password: "$PYPI_PASSWORD"
27+
on: master
3928
notifications:
4029
slack:
4130
secure: lJklhcBVjDT6KzUNa3RFHXdXSeH7ytuuGrkZ5ZcR72CXMoTf2pMJTzPwRLWOp6lCSdDC9Y8MWLrcg/e33dJga4Jlp9alOmWqeqesaFjfee4st8vAsgNbv8/RajPH1gD2bnkt8oIwUzdHItdb5AucKFYjbH2g0d8ndoqYqUeBLrnsT1AP5G/Vi9OHC9OWNpR0FKaZIJE0Wt52vkPMH3sV2mFeIskByPB+56U5y547mualKxn61IVR/dhYBEtZQJuSvnwKHPOn9Pkk7cCa+SSSeTJ4w5LboY8T17otaYNauXo46i1bKIoGiBcCcrJyQHHiPQmcq/YU540MC5Wzt9YXUycmJzRi347oyQeDee27wV3XJlWMXuuhbtJiKCFny7BTQ160VATlj/dbwIzN99Ra6/BtTumv/6LyTdKIuVjdAkcN8dtdDW1nlrQ29zuPNCcXXzJ7zX7kQaOCUV1c2OrsbiH/0fE9nknUORn97txqhlYVi0QMS7764wFo6kg0vpmFQRkkQySsJl+TmgcZ01AlsJc2EMMWVuaj9Af9JU4/4yalqDiXIh1fOYYUZnLfOfWS+MsnI+/oLfqJFyMbrsQQTIjs+kTzbiEdhd2R4EZgusU/xRFWokS2NAvahexrRhRQ6tpAI+LezPrkNOR3aHiykBf+P9BkUa0wPp6V2Ayc6q0=

setup.py

+1-1
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="3.1.0"
19+
version="3.2.0"
2020
)

src/cs50/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
try:
55

66
# Save student's sys.path
7-
path = sys.path[:]
7+
_path = sys.path[:]
88

99
# In case student has files that shadow packages
1010
sys.path = [p for p in sys.path if p not in ("", os.getcwd())]
@@ -25,4 +25,4 @@
2525
finally:
2626

2727
# Restore student's sys.path (just in case library raised an exception that caller caught)
28-
sys.path = path
28+
sys.path = _path

src/cs50/flask.py

+30-8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import logging
2+
13
from distutils.version import StrictVersion
24
from os import getenv
35
from pkg_resources import get_distribution
@@ -8,31 +10,51 @@
810
try:
911

1012
# Only patch >= 1.0
11-
version = StrictVersion(get_distribution("flask").version)
12-
assert version >= StrictVersion("1.0")
13+
_version = StrictVersion(get_distribution("flask").version)
14+
assert _version >= StrictVersion("1.0")
1315

1416
# Reformat logger's exceptions
1517
# http://flask.pocoo.org/docs/1.0/logging/
1618
# https://docs.python.org/3/library/logging.html#logging.Formatter.formatException
1719
try:
1820
import flask.logging
1921
flask.logging.default_handler.formatter.formatException = lambda exc_info: formatException(*exc_info)
20-
except:
22+
except Exception:
23+
pass
24+
25+
# Enable logging when Flask is in use,
26+
# monkey-patching own SQL module, which shouldn't need to know about Flask
27+
logging.getLogger("cs50").disabled = True
28+
try:
29+
import flask
30+
from .sql import SQL
31+
except ImportError:
2132
pass
33+
else:
34+
_before = SQL.execute
35+
def _after(*args, **kwargs):
36+
disabled = logging.getLogger("cs50").disabled
37+
if flask.current_app:
38+
logging.getLogger("cs50").disabled = False
39+
try:
40+
return _before(*args, **kwargs)
41+
finally:
42+
logging.getLogger("cs50").disabled = disabled
43+
SQL.execute = _after
2244

2345
# Add support for Cloud9 proxy so that flask.redirect doesn't redirect from HTTPS to HTTP
2446
# http://stackoverflow.com/a/23504684/5156190
2547
if getenv("C9_HOSTNAME") and not getenv("IDE_OFFLINE"):
2648
try:
2749
import flask
2850
from werkzeug.contrib.fixers import ProxyFix
29-
before = flask.Flask.__init__
30-
def after(self, *args, **kwargs):
31-
before(self, *args, **kwargs)
51+
_before = flask.Flask.__init__
52+
def _after(*args, **kwargs):
53+
_before(*args, **kwargs)
3254
self.wsgi_app = ProxyFix(self.wsgi_app)
33-
flask.Flask.__init__ = after
55+
flask.Flask.__init__ = _after
3456
except:
3557
pass
3658

37-
except:
59+
except Exception:
3860
pass

0 commit comments

Comments
 (0)