Skip to content
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

Fixes support for transactions, drops support for unpacking of args to execute #135

Merged
merged 33 commits into from Nov 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
74c74cc
Update README.md
cbscribe Aug 28, 2020
41166b3
Merge pull request #129 from cbscribe/patch-1
dmalan Aug 28, 2020
e7c0df8
added tests for IN
dmalan Nov 22, 2020
cf34af7
reverting to 5.0.5
dmalan Nov 22, 2020
2be1e24
adds tests for IN
dmalan Nov 22, 2020
a598334
updated .travis.yml
dmalan Nov 22, 2020
b2fc969
working on transactions
dmalan Nov 22, 2020
9d007a4
fixed support for transactions
dmalan Nov 22, 2020
f44c562
updated README
dmalan Nov 22, 2020
87dd161
added sample tests
dmalan Nov 22, 2020
a14e549
tidied code
dmalan Nov 22, 2020
c68324d
fixed README
dmalan Nov 23, 2020
083bf5e
corrected syntax in README
dmalan Nov 23, 2020
a11c9b9
detecting PostgreSQL syntax errors
dmalan Nov 23, 2020
727df50
only calling teardown_appcontext once
dmalan Nov 23, 2020
31305e1
raising ValueError, no longer disconnecting on IntegrityError
dmalan Nov 23, 2020
e8cca8d
updated tests for ValueError
dmalan Nov 23, 2020
16e52bf
updated test for ValueError
dmalan Nov 23, 2020
e9464d8
Merge pull request #133 from cs50/transactions
dmalan Nov 23, 2020
5857d3f
removed support for unpacking dicts and lists in execute
dmalan Nov 23, 2020
0a63c89
version++
dmalan Nov 23, 2020
b9b196d
Merge pull request #134 from cs50/unpacking
dmalan Nov 23, 2020
ead20a7
removed TEMPLATES_AUTO_RELOAD
dmalan Nov 23, 2020
87394fd
fix typo
Nov 24, 2020
a80dfeb
configure root logger
Nov 25, 2020
f3750c7
handle no root logger handlers
Nov 25, 2020
db0d14a
use separate logger for cs50.sql
Nov 25, 2020
aea0dfe
move logger to module level
Nov 25, 2020
74f03f0
s/ch/handler/
Nov 25, 2020
9cd038c
rename logger to cs50
Nov 25, 2020
f2ba330
configure cs50 logger in cs50 module
Nov 25, 2020
7dd19ad
forcibly enable cs50 logger in flask apps
Nov 25, 2020
4887b5f
Merge pull request #136 from cs50/logger
Nov 26, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
71 changes: 71 additions & 0 deletions README.md
Expand Up @@ -19,3 +19,74 @@ f = cs50.get_float();
i = cs50.get_int();
s = cs50.get_string();
```

## Testing

1. Run `cli50` in `python-cs50`.
1. Run `sudo su -`.
1. Run `apt install -y libmysqlclient-dev mysql-server postgresql`.
1. Run `pip3 install mysqlclient psycopg2-binary`.
1. In `/etc/mysql/mysql.conf.d/mysqld.cnf`, add `skip-grant-tables` under `[mysqld]`.
1. In `/etc/profile.d/cli.sh`, remove `valgrind` function for now.
1. Run `service mysql start`.
1. Run `mysql -e 'CREATE DATABASE IF NOT EXISTS test;'`.
1. In `/etc/postgresql/10/main/pg_hba.conf, change:
```
local all postgres peer
host all all 127.0.0.1/32 md5
```
to:
```
local all postgres trust
host all all 127.0.0.1/32 trust
```
1. Run `service postgresql start`.
1. Run `psql -c 'create database test;' -U postgres`.
1. Run `touch test.db`.

### Sample Tests

```
import cs50
db = cs50.SQL("sqlite:///foo.db")
db.execute("CREATE TABLE IF NOT EXISTS cs50 (id INTEGER PRIMARY KEY, val TEXT, bin BLOB)")
db.execute("INSERT INTO cs50 (val) VALUES('a')")
db.execute("INSERT INTO cs50 (val) VALUES('b')")
db.execute("BEGIN")
db.execute("INSERT INTO cs50 (val) VALUES('c')")
db.execute("INSERT INTO cs50 (val) VALUES('x')")
db.execute("INSERT INTO cs50 (val) VALUES('y')")
db.execute("ROLLBACK")
db.execute("INSERT INTO cs50 (val) VALUES('z')")
db.execute("COMMIT")

---

import cs50
db = cs50.SQL("mysql://root@localhost/test")
db.execute("CREATE TABLE IF NOT EXISTS cs50 (id INTEGER PRIMARY KEY, val TEXT, bin BLOB)")
db.execute("INSERT INTO cs50 (val) VALUES('a')")
db.execute("INSERT INTO cs50 (val) VALUES('b')")
db.execute("BEGIN")
db.execute("INSERT INTO cs50 (val) VALUES('c')")
db.execute("INSERT INTO cs50 (val) VALUES('x')")
db.execute("INSERT INTO cs50 (val) VALUES('y')")
db.execute("ROLLBACK")
db.execute("INSERT INTO cs50 (val) VALUES('z')")
db.execute("COMMIT")

---

import cs50
db = cs50.SQL("postgresql://postgres@localhost/test")
db.execute("CREATE TABLE IF NOT EXISTS cs50 (id SERIAL PRIMARY KEY, val VARCHAR(16), bin BYTEA)")
db.execute("INSERT INTO cs50 (val) VALUES('a')")
db.execute("INSERT INTO cs50 (val) VALUES('b')")
db.execute("BEGIN")
db.execute("INSERT INTO cs50 (val) VALUES('c')")
db.execute("INSERT INTO cs50 (val) VALUES('x')")
db.execute("INSERT INTO cs50 (val) VALUES('y')")
db.execute("ROLLBACK")
db.execute("INSERT INTO cs50 (val) VALUES('z')")
db.execute("COMMIT")
```
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -16,5 +16,5 @@
package_dir={"": "src"},
packages=["cs50"],
url="https://github.com/cs50/python-cs50",
version="5.1.0"
version="6.0.0"
)
27 changes: 27 additions & 0 deletions src/cs50/cs50.py
@@ -1,6 +1,7 @@
from __future__ import print_function

import inspect
import logging
import os
import re
import sys
Expand All @@ -11,6 +12,32 @@
from traceback import format_exception


# Configure default logging handler and formatter
# Prevent flask, werkzeug, etc from adding default handler
logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.DEBUG)

try:
# Patch formatException
logging.root.handlers[0].formatter.formatException = lambda exc_info: _formatException(*exc_info)
except IndexError:
pass

# Configure cs50 logger
_logger = logging.getLogger("cs50")
_logger.setLevel(logging.DEBUG)

# Log messages once
_logger.propagate = False

handler = logging.StreamHandler()
handler.setLevel(logging.DEBUG)

formatter = logging.Formatter("%(levelname)s: %(message)s")
formatter.formatException = lambda exc_info: _formatException(*exc_info)
handler.setFormatter(formatter)
_logger.addHandler(handler)


class _flushfile():
"""
Disable buffering for standard output and standard error.
Expand Down
8 changes: 3 additions & 5 deletions src/cs50/flask.py
Expand Up @@ -12,22 +12,20 @@ def _wrap_flask(f):
if f.__version__ < StrictVersion("1.0"):
return

f.logging.default_handler.formatter.formatException = lambda exc_info: _formatException(*exc_info)

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)
self.wsgi_app = ProxyFix(self.wsgi_app, x_proto=1) # For HTTPS-to-HTTP proxy
f.Flask.__init__ = _flask_init_after


# Flask was imported before cs50
# If Flask was imported before cs50
if "flask" in sys.modules:
_wrap_flask(sys.modules["flask"])

# Flask wasn't imported
# If Flask wasn't imported
else:
flask_loader = pkgutil.get_loader('flask')
if flask_loader:
Expand Down