diff options
Diffstat (limited to 'Modules/_dbmmodule.c')
-rw-r--r-- | Modules/_dbmmodule.c | 128 |
1 files changed, 67 insertions, 61 deletions
diff --git a/Modules/_dbmmodule.c b/Modules/_dbmmodule.c index b815e97..6c605ea 100644 --- a/Modules/_dbmmodule.c +++ b/Modules/_dbmmodule.c @@ -29,10 +29,10 @@ static char *which_dbm = "Berkeley DB"; #endif /*[clinic input] -module dbm -class dbm.dbm "dbmobject *" "&Dbmtype" +module _dbm +class _dbm.dbm "dbmobject *" "&Dbmtype" [clinic start generated code]*/ -/*[clinic end generated code: output=da39a3ee5e6b4b0d input=92450564684a69a3]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=9b1aa8756d16150e]*/ typedef struct { PyObject_HEAD @@ -51,15 +51,6 @@ static PyTypeObject Dbmtype; static PyObject *DbmError; -/*[python input] -class dbmobject_converter(self_converter): - type = "dbmobject *" - def pre_render(self): - super().pre_render() - self.name = 'dp' -[python start generated code]*/ -/*[python end generated code: output=da39a3ee5e6b4b0d input=6ad536357913879a]*/ - static PyObject * newdbmobject(const char *file, int flags, int mode) { @@ -183,29 +174,43 @@ static PyMappingMethods dbm_as_mapping = { (objobjargproc)dbm_ass_sub, /*mp_ass_subscript*/ }; +/*[clinic input] +_dbm.dbm.close + +Close the database. +[clinic start generated code]*/ + static PyObject * -dbm__close(dbmobject *dp, PyObject *unused) +_dbm_dbm_close_impl(dbmobject *self) +/*[clinic end generated code: output=c8dc5b6709600b86 input=046db72377d51be8]*/ { - if (dp->di_dbm) - dbm_close(dp->di_dbm); - dp->di_dbm = NULL; + if (self->di_dbm) + dbm_close(self->di_dbm); + self->di_dbm = NULL; Py_INCREF(Py_None); return Py_None; } +/*[clinic input] +_dbm.dbm.keys + +Return a list of all keys in the database. +[clinic start generated code]*/ + static PyObject * -dbm_keys(dbmobject *dp, PyObject *unused) +_dbm_dbm_keys_impl(dbmobject *self) +/*[clinic end generated code: output=434549f7c121b33c input=d210ba778cd9c68a]*/ { PyObject *v, *item; datum key; int err; - check_dbmobject_open(dp); + check_dbmobject_open(self); v = PyList_New(0); if (v == NULL) return NULL; - for (key = dbm_firstkey(dp->di_dbm); key.dptr; - key = dbm_nextkey(dp->di_dbm)) { + for (key = dbm_firstkey(self->di_dbm); key.dptr; + key = dbm_nextkey(self->di_dbm)) { item = PyBytes_FromStringAndSize(key.dptr, key.dsize); if (item == NULL) { Py_DECREF(v); @@ -267,29 +272,26 @@ static PySequenceMethods dbm_as_sequence = { }; /*[clinic input] - -dbm.dbm.get - - self: dbmobject +_dbm.dbm.get key: str(types={'str', 'robuffer'}, length=True) - default: object = None + default: object(c_default="NULL") = b'' / Return the value for key if present, otherwise default. [clinic start generated code]*/ static PyObject * -dbm_dbm_get_impl(dbmobject *dp, const char *key, Py_ssize_clean_t key_length, - PyObject *default_value) -/*[clinic end generated code: output=4f5c0e523eaf1251 input=f81478bc211895ef]*/ +_dbm_dbm_get_impl(dbmobject *self, const char *key, + Py_ssize_clean_t key_length, PyObject *default_value) +/*[clinic end generated code: output=b44f95eba8203d93 input=fee97bbe85e84822]*/ { datum dbm_key, val; dbm_key.dptr = (char *)key; dbm_key.dsize = key_length; - check_dbmobject_open(dp); - val = dbm_fetch(dp->di_dbm, dbm_key); + check_dbmobject_open(self); + val = dbm_fetch(self->di_dbm, dbm_key); if (val.dptr != NULL) return PyBytes_FromStringAndSize(val.dptr, val.dsize); @@ -297,46 +299,55 @@ dbm_dbm_get_impl(dbmobject *dp, const char *key, Py_ssize_clean_t key_length, return default_value; } +/*[clinic input] +_dbm.dbm.setdefault + key: str(types={'str', 'robuffer'}, length=True) + default: object(c_default="NULL") = b'' + / + +Return the value for key if present, otherwise default. + +If key is not in the database, it is inserted with default as the value. +[clinic start generated code]*/ + static PyObject * -dbm_setdefault(dbmobject *dp, PyObject *args) +_dbm_dbm_setdefault_impl(dbmobject *self, const char *key, + Py_ssize_clean_t key_length, + PyObject *default_value) +/*[clinic end generated code: output=52545886cf272161 input=6a3b99ae91f20203]*/ { - datum key, val; - PyObject *defvalue = NULL; - char *tmp_ptr; + datum dbm_key, val; Py_ssize_t tmp_size; - if (!PyArg_ParseTuple(args, "s#|O:setdefault", - &tmp_ptr, &tmp_size, &defvalue)) - return NULL; - key.dptr = tmp_ptr; - key.dsize = tmp_size; - check_dbmobject_open(dp); - val = dbm_fetch(dp->di_dbm, key); + dbm_key.dptr = (char *)key; + dbm_key.dsize = key_length; + check_dbmobject_open(self); + val = dbm_fetch(self->di_dbm, dbm_key); if (val.dptr != NULL) return PyBytes_FromStringAndSize(val.dptr, val.dsize); - if (defvalue == NULL) { - defvalue = PyBytes_FromStringAndSize(NULL, 0); - if (defvalue == NULL) + if (default_value == NULL) { + default_value = PyBytes_FromStringAndSize(NULL, 0); + if (default_value == NULL) return NULL; val.dptr = NULL; val.dsize = 0; } else { - if ( !PyArg_Parse(defvalue, "s#", &val.dptr, &tmp_size) ) { + if ( !PyArg_Parse(default_value, "s#", &val.dptr, &tmp_size) ) { PyErr_SetString(PyExc_TypeError, "dbm mappings have byte string elements only"); return NULL; } val.dsize = tmp_size; - Py_INCREF(defvalue); + Py_INCREF(default_value); } - if (dbm_store(dp->di_dbm, key, val, DBM_INSERT) < 0) { - dbm_clearerr(dp->di_dbm); + if (dbm_store(self->di_dbm, dbm_key, val, DBM_INSERT) < 0) { + dbm_clearerr(self->di_dbm); PyErr_SetString(DbmError, "cannot add item to database"); - Py_DECREF(defvalue); + Py_DECREF(default_value); return NULL; } - return defvalue; + return default_value; } static PyObject * @@ -355,15 +366,10 @@ dbm__exit__(PyObject *self, PyObject *args) static PyMethodDef dbm_methods[] = { - {"close", (PyCFunction)dbm__close, METH_NOARGS, - "close()\nClose the database."}, - {"keys", (PyCFunction)dbm_keys, METH_NOARGS, - "keys() -> list\nReturn a list of all keys in the database."}, - DBM_DBM_GET_METHODDEF - {"setdefault", (PyCFunction)dbm_setdefault, METH_VARARGS, - "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."}, + _DBM_DBM_CLOSE_METHODDEF + _DBM_DBM_KEYS_METHODDEF + _DBM_DBM_GET_METHODDEF + _DBM_DBM_SETDEFAULT_METHODDEF {"__enter__", dbm__enter__, METH_NOARGS, NULL}, {"__exit__", dbm__exit__, METH_VARARGS, NULL}, {NULL, NULL} /* sentinel */ @@ -404,7 +410,7 @@ static PyTypeObject Dbmtype = { /*[clinic input] -dbm.open as dbmopen +_dbm.open as dbmopen filename: str The filename to open. @@ -425,7 +431,7 @@ Return a database object. static PyObject * dbmopen_impl(PyModuleDef *module, const char *filename, const char *flags, int mode) -/*[clinic end generated code: output=e8d4b36f25c733fd input=6499ab0fab1333ac]*/ +/*[clinic end generated code: output=e8d4b36f25c733fd input=226334bade5764e6]*/ { int iflags; |