gh-69093: Add context manager support to sqlite3.Blob (GH-91562)

This commit is contained in:
Erlend Egeberg Aasland 2022-04-16 06:21:12 +02:00 committed by GitHub
parent 4e661cd691
commit a861756675
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 135 additions and 11 deletions

View file

@ -1170,6 +1170,25 @@ def test_blob_sequence_not_supported(self):
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 @@ def test_blob_closed(self):
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: