Skip to content

Commit 822c3dc

Browse files
Erlend Egeberg Aaslandcorona10
Erlend Egeberg Aasland
andauthored
bpo-45512: Raise exception if sqlite3.Connection.__init__ is called with bad isolation level (#29561)
* bpo-45512: Raise sqlite3.Connection.__init__ is called with bad isolation level * Also explicitly test allowed isolation levels * Use subTest for better error messages if something goes wrong * Update Lib/test/test_sqlite3/test_dbapi.py Co-authored-by: Dong-hee Na <[email protected]> Co-authored-by: Dong-hee Na <[email protected]>
1 parent b567b9d commit 822c3dc

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

Lib/test/test_sqlite3/test_dbapi.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ def managed_connect(*args, in_mem=False, **kwargs):
4848

4949

5050
# Helper for temporary memory databases
51-
def memory_database():
52-
cx = sqlite.connect(":memory:")
51+
def memory_database(*args, **kwargs):
52+
cx = sqlite.connect(":memory:", *args, **kwargs)
5353
return contextlib.closing(cx)
5454

5555

@@ -509,6 +509,20 @@ def test_connection_bad_limit_category(self):
509509
self.assertRaisesRegex(sqlite.ProgrammingError, msg,
510510
self.cx.setlimit, cat, 0)
511511

512+
def test_connection_init_bad_isolation_level(self):
513+
msg = (
514+
"isolation_level string must be '', 'DEFERRED', 'IMMEDIATE', or "
515+
"'EXCLUSIVE'"
516+
)
517+
with self.assertRaisesRegex(ValueError, msg):
518+
memory_database(isolation_level="BOGUS")
519+
520+
def test_connection_init_good_isolation_levels(self):
521+
for level in ("", "DEFERRED", "IMMEDIATE", "EXCLUSIVE", None):
522+
with self.subTest(level=level):
523+
with memory_database(isolation_level=level) as cx:
524+
cx.execute("select 'ok'")
525+
512526

513527
class UninitialisedConnectionTests(unittest.TestCase):
514528
def setUp(self):

Modules/_sqlite/connection.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ get_begin_statement(const char *level)
127127
return begin_statements[i];
128128
}
129129
}
130+
PyErr_SetString(PyExc_ValueError,
131+
"isolation_level string must be '', 'DEFERRED', "
132+
"'IMMEDIATE', or 'EXCLUSIVE'");
130133
return NULL;
131134
}
132135

@@ -1389,9 +1392,6 @@ pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* iso
13891392
}
13901393
const char *stmt = get_begin_statement(cstr_level);
13911394
if (stmt == NULL) {
1392-
PyErr_SetString(PyExc_ValueError,
1393-
"isolation_level string must be '', 'DEFERRED', "
1394-
"'IMMEDIATE', or 'EXCLUSIVE'");
13951395
return -1;
13961396
}
13971397
self->begin_statement = stmt;

0 commit comments

Comments
 (0)