Skip to content

Commit 83f6736

Browse files
author
Erlend E. Aasland
committed
bpo-45512: Raise sqlite3.Connection.__init__ is called with bad isolation level
1 parent 82e1f5e commit 83f6736

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

Lib/test/test_sqlite3/test_dbapi.py

Lines changed: 10 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

@@ -547,6 +547,14 @@ def test_connection_bad_reinit(self):
547547
cx.executemany, "insert into t values(?)",
548548
((v,) for v in range(3)))
549549

550+
def test_connection_init_bad_isolation_level(self):
551+
msg = (
552+
"isolation_level string must be '', 'DEFERRED', 'IMMEDIATE', or "
553+
"'EXCLUSIVE'"
554+
)
555+
with self.assertRaisesRegex(ValueError, msg):
556+
memory_database(isolation_level="BOGUS")
557+
550558

551559
class UninitialisedConnectionTests(unittest.TestCase):
552560
def setUp(self):

Modules/_sqlite/connection.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ get_begin_statement(const char *level)
129129
return begin_statements[i];
130130
}
131131
}
132+
PyErr_SetString(PyExc_ValueError,
133+
"isolation_level string must be '', 'DEFERRED', "
134+
"'IMMEDIATE', or 'EXCLUSIVE'");
132135
return NULL;
133136
}
134137

@@ -1399,9 +1402,6 @@ pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* iso
13991402
}
14001403
const char *stmt = get_begin_statement(cstr_level);
14011404
if (stmt == NULL) {
1402-
PyErr_SetString(PyExc_ValueError,
1403-
"isolation_level string must be '', 'DEFERRED', "
1404-
"'IMMEDIATE', or 'EXCLUSIVE'");
14051405
return -1;
14061406
}
14071407
self->begin_statement = stmt;

0 commit comments

Comments
 (0)