summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_dbmmodule.c17
-rw-r--r--Modules/_gdbmmodule.c16
2 files changed, 33 insertions, 0 deletions
diff --git a/Modules/_dbmmodule.c b/Modules/_dbmmodule.c
index 60802b6..370f670 100644
--- a/Modules/_dbmmodule.c
+++ b/Modules/_dbmmodule.c
@@ -313,6 +313,21 @@ dbm_setdefault(dbmobject *dp, PyObject *args)
return defvalue;
}
+static PyObject *
+dbm__enter__(PyObject *self, PyObject *args)
+{
+ Py_INCREF(self);
+ return self;
+}
+
+static PyObject *
+dbm__exit__(PyObject *self, PyObject *args)
+{
+ _Py_IDENTIFIER(close);
+ return _PyObject_CallMethodId(self, &PyId_close, NULL);
+}
+
+
static PyMethodDef dbm_methods[] = {
{"close", (PyCFunction)dbm__close, METH_NOARGS,
"close()\nClose the database."},
@@ -325,6 +340,8 @@ static PyMethodDef dbm_methods[] = {
"setdefault(key[, default]) -> value\n"
"Return the value for key if present, otherwise default. If key\n"
"is not in the database, it is inserted with default as the value."},
+ {"__enter__", dbm__enter__, METH_NOARGS, NULL},
+ {"__exit__", dbm__exit__, METH_VARARGS, NULL},
{NULL, NULL} /* sentinel */
};
diff --git a/Modules/_gdbmmodule.c b/Modules/_gdbmmodule.c
index 36c06d1..229e16e 100644
--- a/Modules/_gdbmmodule.c
+++ b/Modules/_gdbmmodule.c
@@ -425,6 +425,20 @@ dbm_sync(dbmobject *dp, PyObject *unused)
return Py_None;
}
+static PyObject *
+dbm__enter__(PyObject *self, PyObject *args)
+{
+ Py_INCREF(self);
+ return self;
+}
+
+static PyObject *
+dbm__exit__(PyObject *self, PyObject *args)
+{
+ _Py_IDENTIFIER(close);
+ return _PyObject_CallMethodId(self, &PyId_close, NULL);
+}
+
static PyMethodDef dbm_methods[] = {
{"close", (PyCFunction)dbm_close, METH_NOARGS, dbm_close__doc__},
{"keys", (PyCFunction)dbm_keys, METH_NOARGS, dbm_keys__doc__},
@@ -434,6 +448,8 @@ static PyMethodDef dbm_methods[] = {
{"sync", (PyCFunction)dbm_sync, METH_NOARGS, dbm_sync__doc__},
{"get", (PyCFunction)dbm_get, METH_VARARGS, dbm_get__doc__},
{"setdefault",(PyCFunction)dbm_setdefault,METH_VARARGS, dbm_setdefault__doc__},
+ {"__enter__", dbm__enter__, METH_NOARGS, NULL},
+ {"__exit__", dbm__exit__, METH_VARARGS, NULL},
{NULL, NULL} /* sentinel */
};