mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
bpo-42213: Check connection in sqlite3.Connection.__enter__ (GH-26512)
Try to harden connection close: - add tests that exercise stuff against a closed database - add wrapper for sqlite3_close_v2() - check connection on __enter__ - explicitly free pending statements before close() - sqlite3_close_v2() always returns SQLITE_OK
This commit is contained in:
parent
937cebc93b
commit
82ad22a97d
2 changed files with 35 additions and 16 deletions
|
@ -135,6 +135,26 @@ class ConnectionTests(unittest.TestCase):
|
|||
def test_close(self):
|
||||
self.cx.close()
|
||||
|
||||
def test_use_after_close(self):
|
||||
sql = "select 1"
|
||||
cu = self.cx.cursor()
|
||||
res = cu.execute(sql)
|
||||
self.cx.close()
|
||||
self.assertRaises(sqlite.ProgrammingError, res.fetchall)
|
||||
self.assertRaises(sqlite.ProgrammingError, cu.execute, sql)
|
||||
self.assertRaises(sqlite.ProgrammingError, cu.executemany, sql, [])
|
||||
self.assertRaises(sqlite.ProgrammingError, cu.executescript, sql)
|
||||
self.assertRaises(sqlite.ProgrammingError, self.cx.execute, sql)
|
||||
self.assertRaises(sqlite.ProgrammingError,
|
||||
self.cx.executemany, sql, [])
|
||||
self.assertRaises(sqlite.ProgrammingError, self.cx.executescript, sql)
|
||||
self.assertRaises(sqlite.ProgrammingError,
|
||||
self.cx.create_function, "t", 1, lambda x: x)
|
||||
self.assertRaises(sqlite.ProgrammingError, self.cx.cursor)
|
||||
with self.assertRaises(sqlite.ProgrammingError):
|
||||
with self.cx:
|
||||
pass
|
||||
|
||||
def test_exceptions(self):
|
||||
# Optional DB-API extension.
|
||||
self.assertEqual(self.cx.Warning, sqlite.Warning)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue