8000 gh-69093: Add context manager support to sqlite3.Blob by erlend-aasland · Pull Request #91562 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-69093: Add context manager support to sqlite3.Blob #91562

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
gh-69093: Add context manager support to sqlite3.Blob
  • Loading branch information
Erlend E. Aasland committed Apr 15, 2022
commit b6cd651ef489ed012479740f96076638fc25d9f1
12 changes: 12 additions & 0 deletions Doc/includes/sqlite3/blob_with.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import sqlite3

con = sqlite3.connect(":memory:")

con.execute("create table test(blob_col blob)")
con.execute("insert into test(blob_col) values (zeroblob(10))")

with con.blobopen("test", "blob_col", 1) as blob:
blob.write(b"Hello")
blob.write(b"World")
blob.seek(0)
print(blob.read()) # will print b"HelloWorld"
4 changes: 4 additions & 0 deletions Doc/library/sqlite3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,10 @@ Blob Objects

.. literalinclude:: ../includes/sqlite3/blob.py

A :class:`Blob` can also be used as a :term:`context manager`:

.. literalinclude:: ../includes/sqlite3/blob_with.py


.. _sqlite3-types:

Expand Down
23 changes: 23 additions & 0 deletions Lib/test/test_sqlite3/test_dbapi.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add :term:`context manager` support to :class:`sqlite3.Blob`.
Patch by Aviv Palivoda and Erlend E. Aasland.
43 changes: 43 additions & 0 deletions Modules/_sqlite/blob.c
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,51 @@ blob_tell_impl(pysqlite_Blob *self)
}


/*[clinic input]
_sqlite3.Blob.__enter__ as blob_enter

Blob context manager enter.
[clinic start generated code]*/

static PyObject *
blob_enter_impl(pysqlite_Blob *self)
/*[clinic end generated code: output=4fd32484b071a6cd input=fe4842c3c582d5a7]*/
{
if (!check_blob(self)) {
return NULL;
}
return Py_NewRef(self);
}


/*[clinic input]
_sqlite3.Blob.__exit__ as blob_exit

type: object
val: object
tb: object
/

Blob context manager exit.
[clinic start generated code]*/

static PyObject *
blob_exit_impl(pysqlite_Blob *self, PyObject *type, PyObject *val,
PyObject *tb)
/*[clinic end generated code: output=fc86ceeb2b68c7b2 input=575d9ecea205f35f]*/
{
if (!check_blob(self)) {
return NULL;
}
close_blob(self);
Py_RETURN_FALSE;
}


static PyMethodDef blob_methods[] = {
BLOB_CLOSE_METHODDEF
BLOB_ENTER_METHODDEF
BLOB_EXIT_METHODDEF
BLOB_READ_METHODDEF
BLOB_SEEK_METHODDEF
BLOB_TELL_METHODDEF
Expand Down
53 changes: 52 additions & 1 deletion Modules/_sqlite/clinic/blob.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0