summaryrefslogtreecommitdiffstats
path: root/Modules
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 /Modules
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 'Modules')
-rw-r--r--Modules/_sqlite/blob.c43
-rw-r--r--Modules/_sqlite/clinic/blob.c.h53
2 files changed, 95 insertions, 1 deletions
diff --git a/Modules/_sqlite/blob.c b/Modules/_sqlite/blob.c
index c4f8be4..3f76630 100644
--- a/Modules/_sqlite/blob.c
+++ b/Modules/_sqlite/blob.c
@@ -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
diff --git a/Modules/_sqlite/clinic/blob.c.h b/Modules/_sqlite/clinic/blob.c.h
index 30b3e3c..237877a 100644
--- a/Modules/_sqlite/clinic/blob.c.h
+++ b/Modules/_sqlite/clinic/blob.c.h
@@ -162,4 +162,55 @@ blob_tell(pysqlite_Blob *self, PyObject *Py_UNUSED(ignored))
{
return blob_tell_impl(self);
}
-/*[clinic end generated code: output=d3a02b127f2cfa58 input=a9049054013a1b77]*/
+
+PyDoc_STRVAR(blob_enter__doc__,
+"__enter__($self, /)\n"
+"--\n"
+"\n"
+"Blob context manager enter.");
+
+#define BLOB_ENTER_METHODDEF \
+ {"__enter__", (PyCFunction)blob_enter, METH_NOARGS, blob_enter__doc__},
+
+static PyObject *
+blob_enter_impl(pysqlite_Blob *self);
+
+static PyObject *
+blob_enter(pysqlite_Blob *self, PyObject *Py_UNUSED(ignored))
+{
+ return blob_enter_impl(self);
+}
+
+PyDoc_STRVAR(blob_exit__doc__,
+"__exit__($self, type, val, tb, /)\n"
+"--\n"
+"\n"
+"Blob context manager exit.");
+
+#define BLOB_EXIT_METHODDEF \
+ {"__exit__", (PyCFunction)(void(*)(void))blob_exit, METH_FASTCALL, blob_exit__doc__},
+
+static PyObject *
+blob_exit_impl(pysqlite_Blob *self, PyObject *type, PyObject *val,
+ PyObject *tb);
+
+static PyObject *
+blob_exit(pysqlite_Blob *self, PyObject *const *args, Py_ssize_t nargs)
+{
+ PyObject *return_value = NULL;
+ PyObject *type;
+ PyObject *val;
+ PyObject *tb;
+
+ if (!_PyArg_CheckPositional("__exit__", nargs, 3, 3)) {
+ goto exit;
+ }
+ type = args[0];
+ val = args[1];
+ tb = args[2];
+ return_value = blob_exit_impl(self, type, val, tb);
+
+exit:
+ return return_value;
+}
+/*[clinic end generated code: output=ca2400862c18dadb input=a9049054013a1b77]*/