| 1 |
/*[clinic input] |
| 2 |
preserve |
| 3 |
[clinic start generated code]*/ |
| 4 |
|
| 5 |
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
| 6 |
# include "pycore_runtime.h" // _Py_SINGLETON() |
| 7 |
#endif |
| 8 |
#include "pycore_modsupport.h" // _PyArg_CheckPositional() |
| 9 |
|
| 10 |
PyDoc_STRVAR(_gdbm_gdbm_get__doc__, |
| 11 |
"get($self, key, default=None, /)\n" |
| 12 |
"--\n" |
| 13 |
"\n" |
| 14 |
"Get the value for key, or default if not present."); |
| 15 |
|
| 16 |
#define _GDBM_GDBM_GET_METHODDEF \ |
| 17 |
{"get", _PyCFunction_CAST(_gdbm_gdbm_get), METH_FASTCALL, _gdbm_gdbm_get__doc__}, |
| 18 |
|
| 19 |
static PyObject * |
| 20 |
_gdbm_gdbm_get_impl(gdbmobject *self, PyObject *key, PyObject *default_value); |
| 21 |
|
| 22 |
static PyObject * |
| 23 |
_gdbm_gdbm_get(gdbmobject *self, PyObject *const *args, Py_ssize_t nargs) |
| 24 |
{ |
| 25 |
PyObject *return_value = NULL; |
| 26 |
PyObject *key; |
| 27 |
PyObject *default_value = Py_None; |
| 28 |
|
| 29 |
if (!_PyArg_CheckPositional("get", nargs, 1, 2)) { |
| 30 |
goto exit; |
| 31 |
} |
| 32 |
key = args[0]; |
| 33 |
if (nargs < 2) { |
| 34 |
goto skip_optional; |
| 35 |
} |
| 36 |
default_value = args[1]; |
| 37 |
skip_optional: |
| 38 |
return_value = _gdbm_gdbm_get_impl(self, key, default_value); |
| 39 |
|
| 40 |
exit: |
| 41 |
return return_value; |
| 42 |
} |
| 43 |
|
| 44 |
PyDoc_STRVAR(_gdbm_gdbm_setdefault__doc__, |
| 45 |
"setdefault($self, key, default=None, /)\n" |
| 46 |
"--\n" |
| 47 |
"\n" |
| 48 |
"Get value for key, or set it to default and return default if not present."); |
| 49 |
|
| 50 |
#define _GDBM_GDBM_SETDEFAULT_METHODDEF \ |
| 51 |
{"setdefault", _PyCFunction_CAST(_gdbm_gdbm_setdefault), METH_FASTCALL, _gdbm_gdbm_setdefault__doc__}, |
| 52 |
|
| 53 |
static PyObject * |
| 54 |
_gdbm_gdbm_setdefault_impl(gdbmobject *self, PyObject *key, |
| 55 |
PyObject *default_value); |
| 56 |
|
| 57 |
static PyObject * |
| 58 |
_gdbm_gdbm_setdefault(gdbmobject *self, PyObject *const *args, Py_ssize_t nargs) |
| 59 |
{ |
| 60 |
PyObject *return_value = NULL; |
| 61 |
PyObject *key; |
| 62 |
PyObject *default_value = Py_None; |
| 63 |
|
| 64 |
if (!_PyArg_CheckPositional("setdefault", nargs, 1, 2)) { |
| 65 |
goto exit; |
| 66 |
} |
| 67 |
key = args[0]; |
| 68 |
if (nargs < 2) { |
| 69 |
goto skip_optional; |
| 70 |
} |
| 71 |
default_value = args[1]; |
| 72 |
skip_optional: |
| 73 |
return_value = _gdbm_gdbm_setdefault_impl(self, key, default_value); |
| 74 |
|
| 75 |
exit: |
| 76 |
return return_value; |
| 77 |
} |
| 78 |
|
| 79 |
PyDoc_STRVAR(_gdbm_gdbm_close__doc__, |
| 80 |
"close($self, /)\n" |
| 81 |
"--\n" |
| 82 |
"\n" |
| 83 |
"Close the database."); |
| 84 |
|
| 85 |
#define _GDBM_GDBM_CLOSE_METHODDEF \ |
| 86 |
{"close", (PyCFunction)_gdbm_gdbm_close, METH_NOARGS, _gdbm_gdbm_close__doc__}, |
| 87 |
|
| 88 |
static PyObject * |
| 89 |
_gdbm_gdbm_close_impl(gdbmobject *self); |
| 90 |
|
| 91 |
static PyObject * |
| 92 |
_gdbm_gdbm_close(gdbmobject *self, PyObject *Py_UNUSED(ignored)) |
| 93 |
{ |
| 94 |
return _gdbm_gdbm_close_impl(self); |
| 95 |
} |
| 96 |
|
| 97 |
PyDoc_STRVAR(_gdbm_gdbm_keys__doc__, |
| 98 |
"keys($self, /)\n" |
| 99 |
"--\n" |
| 100 |
"\n" |
| 101 |
"Get a list of all keys in the database."); |
| 102 |
|
| 103 |
#define _GDBM_GDBM_KEYS_METHODDEF \ |
| 104 |
{"keys", _PyCFunction_CAST(_gdbm_gdbm_keys), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _gdbm_gdbm_keys__doc__}, |
| 105 |
|
| 106 |
static PyObject * |
| 107 |
_gdbm_gdbm_keys_impl(gdbmobject *self, PyTypeObject *cls); |
| 108 |
|
| 109 |
static PyObject * |
| 110 |
_gdbm_gdbm_keys(gdbmobject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
| 111 |
{ |
| 112 |
if (nargs || (kwnames && PyTuple_GET_SIZE(kwnames))) { |
| 113 |
PyErr_SetString(PyExc_TypeError, "keys() takes no arguments"); |
| 114 |
return NULL; |
| 115 |
} |
| 116 |
return _gdbm_gdbm_keys_impl(self, cls); |
| 117 |
} |
| 118 |
|
| 119 |
PyDoc_STRVAR(_gdbm_gdbm_firstkey__doc__, |
| 120 |
"firstkey($self, /)\n" |
| 121 |
"--\n" |
| 122 |
"\n" |
| 123 |
"Return the starting key for the traversal.\n" |
| 124 |
"\n" |
| 125 |
"It\'s possible to loop over every key in the database using this method\n" |
| 126 |
"and the nextkey() method. The traversal is ordered by GDBM\'s internal\n" |
| 127 |
"hash values, and won\'t be sorted by the key values."); |
| 128 |
|
| 129 |
#define _GDBM_GDBM_FIRSTKEY_METHODDEF \ |
| 130 |
{"firstkey", _PyCFunction_CAST(_gdbm_gdbm_firstkey), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _gdbm_gdbm_firstkey__doc__}, |
| 131 |
|
| 132 |
static PyObject * |
| 133 |
_gdbm_gdbm_firstkey_impl(gdbmobject *self, PyTypeObject *cls); |
| 134 |
|
| 135 |
static PyObject * |
| 136 |
_gdbm_gdbm_firstkey(gdbmobject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
| 137 |
{ |
| 138 |
if (nargs || (kwnames && PyTuple_GET_SIZE(kwnames))) { |
| 139 |
PyErr_SetString(PyExc_TypeError, "firstkey() takes no arguments"); |
| 140 |
return NULL; |
| 141 |
} |
| 142 |
return _gdbm_gdbm_firstkey_impl(self, cls); |
| 143 |
} |
| 144 |
|
| 145 |
PyDoc_STRVAR(_gdbm_gdbm_nextkey__doc__, |
| 146 |
"nextkey($self, key, /)\n" |
| 147 |
"--\n" |
| 148 |
"\n" |
| 149 |
"Returns the key that follows key in the traversal.\n" |
| 150 |
"\n" |
| 151 |
"The following code prints every key in the database db, without having\n" |
| 152 |
"to create a list in memory that contains them all:\n" |
| 153 |
"\n" |
| 154 |
" k = db.firstkey()\n" |
| 155 |
" while k is not None:\n" |
| 156 |
" print(k)\n" |
| 157 |
" k = db.nextkey(k)"); |
| 158 |
|
| 159 |
#define _GDBM_GDBM_NEXTKEY_METHODDEF \ |
| 160 |
{"nextkey", _PyCFunction_CAST(_gdbm_gdbm_nextkey), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _gdbm_gdbm_nextkey__doc__}, |
| 161 |
|
| 162 |
static PyObject * |
| 163 |
_gdbm_gdbm_nextkey_impl(gdbmobject *self, PyTypeObject *cls, const char *key, |
| 164 |
Py_ssize_t key_length); |
| 165 |
|
| 166 |
static PyObject * |
| 167 |
_gdbm_gdbm_nextkey(gdbmobject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
| 168 |
{ |
| 169 |
PyObject *return_value = NULL; |
| 170 |
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
| 171 |
# define KWTUPLE (PyObject *)&_Py_SINGLETON(tuple_empty) |
| 172 |
#else |
| 173 |
# define KWTUPLE NULL |
| 174 |
#endif |
| 175 |
|
| 176 |
static const char * const _keywords[] = {"", NULL}; |
| 177 |
static _PyArg_Parser _parser = { |
| 178 |
.keywords = _keywords, |
| 179 |
.format = "s#:nextkey", |
| 180 |
.kwtuple = KWTUPLE, |
| 181 |
}; |
| 182 |
#undef KWTUPLE |
| 183 |
const char *key; |
| 184 |
Py_ssize_t key_length; |
| 185 |
|
| 186 |
if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser, |
| 187 |
&key, &key_length)) { |
| 188 |
goto exit; |
| 189 |
} |
| 190 |
return_value = _gdbm_gdbm_nextkey_impl(self, cls, key, key_length); |
| 191 |
|
| 192 |
exit: |
| 193 |
return return_value; |
| 194 |
} |
| 195 |
|
| 196 |
PyDoc_STRVAR(_gdbm_gdbm_reorganize__doc__, |
| 197 |
"reorganize($self, /)\n" |
| 198 |
"--\n" |
| 199 |
"\n" |
| 200 |
"Reorganize the database.\n" |
| 201 |
"\n" |
| 202 |
"If you have carried out a lot of deletions and would like to shrink\n" |
| 203 |
"the space used by the GDBM file, this routine will reorganize the\n" |
| 204 |
"database. GDBM will not shorten the length of a database file except\n" |
| 205 |
"by using this reorganization; otherwise, deleted file space will be\n" |
| 206 |
"kept and reused as new (key,value) pairs are added."); |
| 207 |
|
| 208 |
#define _GDBM_GDBM_REORGANIZE_METHODDEF \ |
| 209 |
{"reorganize", _PyCFunction_CAST(_gdbm_gdbm_reorganize), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _gdbm_gdbm_reorganize__doc__}, |
| 210 |
|
| 211 |
static PyObject * |
| 212 |
_gdbm_gdbm_reorganize_impl(gdbmobject *self, PyTypeObject *cls); |
| 213 |
|
| 214 |
static PyObject * |
| 215 |
_gdbm_gdbm_reorganize(gdbmobject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
| 216 |
{ |
| 217 |
if (nargs || (kwnames && PyTuple_GET_SIZE(kwnames))) { |
| 218 |
PyErr_SetString(PyExc_TypeError, "reorganize() takes no arguments"); |
| 219 |
return NULL; |
| 220 |
} |
| 221 |
return _gdbm_gdbm_reorganize_impl(self, cls); |
| 222 |
} |
| 223 |
|
| 224 |
PyDoc_STRVAR(_gdbm_gdbm_sync__doc__, |
| 225 |
"sync($self, /)\n" |
| 226 |
"--\n" |
| 227 |
"\n" |
| 228 |
"Flush the database to the disk file.\n" |
| 229 |
"\n" |
| 230 |
"When the database has been opened in fast mode, this method forces\n" |
| 231 |
"any unwritten data to be written to the disk."); |
| 232 |
|
| 233 |
#define _GDBM_GDBM_SYNC_METHODDEF \ |
| 234 |
{"sync", _PyCFunction_CAST(_gdbm_gdbm_sync), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _gdbm_gdbm_sync__doc__}, |
| 235 |
|
| 236 |
static PyObject * |
| 237 |
_gdbm_gdbm_sync_impl(gdbmobject *self, PyTypeObject *cls); |
| 238 |
|
| 239 |
static PyObject * |
| 240 |
_gdbm_gdbm_sync(gdbmobject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
| 241 |
{ |
| 242 |
if (nargs || (kwnames && PyTuple_GET_SIZE(kwnames))) { |
| 243 |
PyErr_SetString(PyExc_TypeError, "sync() takes no arguments"); |
| 244 |
return NULL; |
| 245 |
} |
| 246 |
return _gdbm_gdbm_sync_impl(self, cls); |
| 247 |
} |
| 248 |
|
| 249 |
PyDoc_STRVAR(_gdbm_gdbm_clear__doc__, |
| 250 |
"clear($self, /)\n" |
| 251 |
"--\n" |
| 252 |
"\n" |
| 253 |
"Remove all items from the database."); |
| 254 |
|
| 255 |
#define _GDBM_GDBM_CLEAR_METHODDEF \ |
| 256 |
{"clear", _PyCFunction_CAST(_gdbm_gdbm_clear), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _gdbm_gdbm_clear__doc__}, |
| 257 |
|
| 258 |
static PyObject * |
| 259 |
_gdbm_gdbm_clear_impl(gdbmobject *self, PyTypeObject *cls); |
| 260 |
|
| 261 |
static PyObject * |
| 262 |
_gdbm_gdbm_clear(gdbmobject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
| 263 |
{ |
| 264 |
if (nargs || (kwnames && PyTuple_GET_SIZE(kwnames))) { |
| 265 |
PyErr_SetString(PyExc_TypeError, "clear() takes no arguments"); |
| 266 |
return NULL; |
| 267 |
} |
| 268 |
return _gdbm_gdbm_clear_impl(self, cls); |
| 269 |
} |
| 270 |
|
| 271 |
PyDoc_STRVAR(dbmopen__doc__, |
| 272 |
"open($module, filename, flags=\'r\', mode=0o666, /)\n" |
| 273 |
"--\n" |
| 274 |
"\n" |
| 275 |
"Open a dbm database and return a dbm object.\n" |
| 276 |
"\n" |
| 277 |
"The filename argument is the name of the database file.\n" |
| 278 |
"\n" |
| 279 |
"The optional flags argument can be \'r\' (to open an existing database\n" |
| 280 |
"for reading only -- default), \'w\' (to open an existing database for\n" |
| 281 |
"reading and writing), \'c\' (which creates the database if it doesn\'t\n" |
| 282 |
"exist), or \'n\' (which always creates a new empty database).\n" |
| 283 |
"\n" |
| 284 |
"Some versions of gdbm support additional flags which must be\n" |
| 285 |
"appended to one of the flags described above. The module constant\n" |
| 286 |
"\'open_flags\' is a string of valid additional flags. The \'f\' flag\n" |
| 287 |
"opens the database in fast mode; altered data will not automatically\n" |
| 288 |
"be written to the disk after every change. This results in faster\n" |
| 289 |
"writes to the database, but may result in an inconsistent database\n" |
| 290 |
"if the program crashes while the database is still open. Use the\n" |
| 291 |
"sync() method to force any unwritten data to be written to the disk.\n" |
| 292 |
"The \'s\' flag causes all database operations to be synchronized to\n" |
| 293 |
"disk. The \'u\' flag disables locking of the database file.\n" |
| 294 |
"\n" |
| 295 |
"The optional mode argument is the Unix mode of the file, used only\n" |
| 296 |
"when the database has to be created. It defaults to octal 0o666."); |
| 297 |
|
| 298 |
#define DBMOPEN_METHODDEF \ |
| 299 |
{"open", _PyCFunction_CAST(dbmopen), METH_FASTCALL, dbmopen__doc__}, |
| 300 |
|
| 301 |
static PyObject * |
| 302 |
dbmopen_impl(PyObject *module, PyObject *filename, const char *flags, |
| 303 |
int mode); |
| 304 |
|
| 305 |
static PyObject * |
| 306 |
dbmopen(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
| 307 |
{ |
| 308 |
PyObject *return_value = NULL; |
| 309 |
PyObject *filename; |
| 310 |
const char *flags = "r"; |
| 311 |
int mode = 438; |
| 312 |
|
| 313 |
if (!_PyArg_CheckPositional("open", nargs, 1, 3)) { |
| 314 |
goto exit; |
| 315 |
} |
| 316 |
filename = args[0]; |
| 317 |
if (nargs < 2) { |
| 318 |
goto skip_optional; |
| 319 |
} |
| 320 |
if (!PyUnicode_Check(args[1])) { |
| 321 |
_PyArg_BadArgument("open", "argument 2", "str", args[1]); |
| 322 |
goto exit; |
| 323 |
} |
| 324 |
Py_ssize_t flags_length; |
| 325 |
flags = PyUnicode_AsUTF8AndSize(args[1], &flags_length); |
| 326 |
if (flags == NULL) { |
| 327 |
goto exit; |
| 328 |
} |
| 329 |
if (strlen(flags) != (size_t)flags_length) { |
| 330 |
PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 331 |
goto exit; |
| 332 |
} |
| 333 |
if (nargs < 3) { |
| 334 |
goto skip_optional; |
| 335 |
} |
| 336 |
mode = PyLong_AsInt(args[2]); |
| 337 |
if (mode == -1 && PyErr_Occurred()) { |
| 338 |
goto exit; |
| 339 |
} |
| 340 |
skip_optional: |
| 341 |
return_value = dbmopen_impl(module, filename, flags, mode); |
| 342 |
|
| 343 |
exit: |
| 344 |
return return_value; |
| 345 |
} |
| 346 |
/*[clinic end generated code: output=07bdeb4a8ecb328e input=a9049054013a1b77]*/ |