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