Skip to content

Commit 1cfb99f

Browse files
authored
Merge pull request #170 from matthiaswenz/conditional-import-sqlite
Load sqlite module only if sqlite connection
2 parents fcc68a1 + a3784cc commit 1cfb99f

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@
1818
package_dir={"": "src"},
1919
packages=["cs50"],
2020
url="https://github.com/cs50/python-cs50",
21-
version="9.2.2"
21+
version="9.2.3"
2222
)

src/cs50/sql.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import sys
12
import threading
23

34
# Thread-local data
@@ -51,12 +52,12 @@ def __init__(self, url, **kwargs):
5152
import re
5253
import sqlalchemy
5354
import sqlalchemy.orm
54-
import sqlite3
5555
import threading
5656

5757
# Require that file already exist for SQLite
5858
matches = re.search(r"^sqlite:///(.+)$", url)
5959
if matches:
60+
import sqlite3
6061
if not os.path.exists(matches.group(1)):
6162
raise RuntimeError("does not exist: {}".format(matches.group(1)))
6263
if not os.path.isfile(matches.group(1)):
@@ -74,7 +75,7 @@ def __init__(self, url, **kwargs):
7475
def connect(dbapi_connection, connection_record):
7576

7677
# Enable foreign key constraints
77-
if type(dbapi_connection) is sqlite3.Connection: # If back end is sqlite
78+
if "sqlite3" in sys.modules and type(dbapi_connection) is sqlite3.Connection: # If back end is sqlite
7879
cursor = dbapi_connection.cursor()
7980
cursor.execute("PRAGMA foreign_keys=ON")
8081
cursor.close()

0 commit comments

Comments
 (0)