summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_sqlite3
diff options
context:
space:
mode:
authorErlend Egeberg Aasland <erlend.aasland@innova.no>2022-04-16 04:21:12 (GMT)
committerGitHub <noreply@github.com>2022-04-16 04:21:12 (GMT)
commita8617566759a07c67d14c9b6ed663e32d3b3f5e7 (patch)
tree531993ba1515ca7050375c7d840579423dcc4d7f /Lib/test/test_sqlite3
parent4e661cd69164318c1f871faa476c68a04092ddc4 (diff)
downloadcpython-a8617566759a07c67d14c9b6ed663e32d3b3f5e7.zip
cpython-a8617566759a07c67d14c9b6ed663e32d3b3f5e7.tar.gz
cpython-a8617566759a07c67d14c9b6ed663e32d3b3f5e7.tar.bz2
gh-69093: Add context manager support to sqlite3.Blob (GH-91562)
Diffstat (limited to 'Lib/test/test_sqlite3')
-rw-r--r--Lib/test/test_sqlite3/test_dbapi.py23
1 files changed, 23 insertions, 0 deletions
diff --git a/Lib/test/test_sqlite3/test_dbapi.py b/Lib/test/test_sqlite3/test_dbapi.py
index b010813..79dcb3e 100644
--- a/Lib/test/test_sqlite3/test_dbapi.py
+++ b/Lib/test/test_sqlite3/test_dbapi.py
@@ -1170,6 +1170,25 @@ class BlobTests(unittest.TestCase):
with self.assertRaises(TypeError):
b"a" in self.blob
+ def test_blob_context_manager(self):
+ data = b"a" * 50
+ with self.cx.blobopen("test", "b", 1) as blob:
+ blob.write(data)
+ actual = self.cx.execute("select b from test").fetchone()[0]
+ self.assertEqual(actual, data)
+
+ # Check that __exit__ closed the blob
+ with self.assertRaisesRegex(sqlite.ProgrammingError, "closed blob"):
+ blob.read()
+
+ def test_blob_context_manager_reraise_exceptions(self):
+ class DummyException(Exception):
+ pass
+ with self.assertRaisesRegex(DummyException, "reraised"):
+ with self.cx.blobopen("test", "b", 1) as blob:
+ raise DummyException("reraised")
+
+
def test_blob_closed(self):
with memory_database() as cx:
cx.execute("create table test(b blob)")
@@ -1186,6 +1205,10 @@ class BlobTests(unittest.TestCase):
blob.seek(0)
with self.assertRaisesRegex(sqlite.ProgrammingError, msg):
blob.tell()
+ with self.assertRaisesRegex(sqlite.ProgrammingError, msg):
+ blob.__enter__()
+ with self.assertRaisesRegex(sqlite.ProgrammingError, msg):
+ blob.__exit__(None, None, None)
def test_blob_closed_db_read(self):
with memory_database() as cx: