Skip to content

Commit

Permalink
Merge pull request #135 from cs50/develop
Browse files Browse the repository at this point in the history
Fixes support for transactions, drops support for unpacking of args to execute
  • Loading branch information
Kareem Zidane authored Nov 26, 2020
2 parents 2d40229 + 4887b5f commit a3b3cc5
Show file tree
Hide file tree
Showing 9 changed files with 248 additions and 251 deletions.
71 changes: 71 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Loading

0 comments on commit a3b3cc5

Please sign in to comment.