Skip to content

bpo-45512: Raise exception if sqlite3.Connection.__init__ is called with bad isolation level #29561

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 4 commits into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
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
18 changes: 16 additions & 2 deletions Lib/test/test_sqlite3/test_dbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ def managed_connect(*args, in_mem=False, **kwargs):


# Helper for temporary memory databases
def memory_database():
cx = sqlite.connect(":memory:")
def memory_database(*args, **kwargs):
cx = sqlite.connect(":memory:", *args, **kwargs)
return contextlib.closing(cx)


Expand Down Expand Up @@ -509,6 +509,20 @@ def test_connection_bad_limit_category(self):
self.assertRaisesRegex(sqlite.ProgrammingError, msg,
self.cx.setlimit, cat, 0)

def test_connection_init_bad_isolation_level(self):
msg = (
"isolation_level string must be '', 'DEFERRED', 'IMMEDIATE', or "
"'EXCLUSIVE'"
)
with self.assertRaisesRegex(ValueError, msg):
memory_database(isolation_level="BOGUS")

def test_connection_init_good_isolation_levels(self):
for level in ("", "DEFERRED", "IMMEDIATE", "EXCLUSIVE", None):
with self.subTest(level=level):
with memory_database(isolation_level=level) as cx:
cx.execute("select 'ok'")


class UninitialisedConnectionTests(unittest.TestCase):
def setUp(self):
Expand Down
6 changes: 3 additions & 3 deletions Modules/_sqlite/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ get_begin_statement(const char *level)
return begin_statements[i];
}
}
PyErr_SetString(PyExc_ValueError,
"isolation_level string must be '', 'DEFERRED', "
"'IMMEDIATE', or 'EXCLUSIVE'");
return NULL;
}

Expand Down Expand Up @@ -1389,9 +1392,6 @@ pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* iso
}
const char *stmt = get_begin_statement(cstr_level);
if (stmt == NULL) {
PyErr_SetString(PyExc_ValueError,
"isolation_level string must be '', 'DEFERRED', "
"'IMMEDIATE', or 'EXCLUSIVE'");
return -1;
}
self->begin_statement = stmt;
Expand Down