◐ Shell
clean mode source ↗

gh-69093: Add context manager support to sqlite3.Blob by erlend-aasland · Pull Request #91562 · python/cpython

Expand Up @@ -1166,6 +1166,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)") Expand All @@ -1182,6 +1201,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: Expand Down