summaryrefslogtreecommitdiffstats
path: root/Modules/_dbmmodule.c
diff options
context:
space:
mode:
authorNick Coghlan <ncoghlan@gmail.com>2013-11-17 05:59:51 (GMT)
committerNick Coghlan <ncoghlan@gmail.com>2013-11-17 05:59:51 (GMT)
commitc610aba1ed57a30104a254ccd1f9fe07d02b1334 (patch)
treede690ad2f0b0f9cda831d66f026e5494a77882c5 /Modules/_dbmmodule.c
parenteb8ea265bac16844efebe4f42d3c674527a66988 (diff)
downloadcpython-c610aba1ed57a30104a254ccd1f9fe07d02b1334.zip
cpython-c610aba1ed57a30104a254ccd1f9fe07d02b1334.tar.gz
cpython-c610aba1ed57a30104a254ccd1f9fe07d02b1334.tar.bz2
Close #19282: Native context management in dbm
Diffstat (limited to 'Modules/_dbmmodule.c')
-rw-r--r--Modules/_dbmmodule.c17
1 files changed, 17 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 */
};