| 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_UnpackKeywords() |
| 10 |
|
| 11 |
PyDoc_STRVAR(_dbm_dbm_close__doc__, |
| 12 |
"close($self, /)\n" |
| 13 |
"--\n" |
| 14 |
"\n" |
| 15 |
"Close the database."); |
| 16 |
|
| 17 |
#define _DBM_DBM_CLOSE_METHODDEF \ |
| 18 |
{"close", (PyCFunction)_dbm_dbm_close, METH_NOARGS, _dbm_dbm_close__doc__}, |
| 19 |
|
| 20 |
static PyObject * |
| 21 |
_dbm_dbm_close_impl(dbmobject *self); |
| 22 |
|
| 23 |
static PyObject * |
| 24 |
_dbm_dbm_close(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 25 |
{ |
| 26 |
PyObject *return_value = NULL; |
| 27 |
|
| 28 |
Py_BEGIN_CRITICAL_SECTION(self); |
| 29 |
return_value = _dbm_dbm_close_impl((dbmobject *)self); |
| 30 |
Py_END_CRITICAL_SECTION(); |
| 31 |
|
| 32 |
return return_value; |
| 33 |
} |
| 34 |
|
| 35 |
PyDoc_STRVAR(_dbm_dbm_keys__doc__, |
| 36 |
"keys($self, /)\n" |
| 37 |
"--\n" |
| 38 |
"\n" |
| 39 |
"Return a list of all keys in the database."); |
| 40 |
|
| 41 |
#define _DBM_DBM_KEYS_METHODDEF \ |
| 42 |
{"keys", _PyCFunction_CAST(_dbm_dbm_keys), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _dbm_dbm_keys__doc__}, |
| 43 |
|
| 44 |
static PyObject * |
| 45 |
_dbm_dbm_keys_impl(dbmobject *self, PyTypeObject *cls); |
| 46 |
|
| 47 |
static PyObject * |
| 48 |
_dbm_dbm_keys(PyObject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
| 49 |
{ |
| 50 |
PyObject *return_value = NULL; |
| 51 |
|
| 52 |
if (nargs || (kwnames && PyTuple_GET_SIZE(kwnames))) { |
| 53 |
PyErr_SetString(PyExc_TypeError, "keys() takes no arguments"); |
| 54 |
goto exit; |
| 55 |
} |
| 56 |
Py_BEGIN_CRITICAL_SECTION(self); |
| 57 |
return_value = _dbm_dbm_keys_impl((dbmobject *)self, cls); |
| 58 |
Py_END_CRITICAL_SECTION(); |
| 59 |
|
| 60 |
exit: |
| 61 |
return return_value; |
| 62 |
} |
| 63 |
|
| 64 |
PyDoc_STRVAR(_dbm_dbm_get__doc__, |
| 65 |
"get($self, key, default=None, /)\n" |
| 66 |
"--\n" |
| 67 |
"\n" |
| 68 |
"Return the value for key if present, otherwise default."); |
| 69 |
|
| 70 |
#define _DBM_DBM_GET_METHODDEF \ |
| 71 |
{"get", _PyCFunction_CAST(_dbm_dbm_get), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _dbm_dbm_get__doc__}, |
| 72 |
|
| 73 |
static PyObject * |
| 74 |
_dbm_dbm_get_impl(dbmobject *self, PyTypeObject *cls, const char *key, |
| 75 |
Py_ssize_t key_length, PyObject *default_value); |
| 76 |
|
| 77 |
static PyObject * |
| 78 |
_dbm_dbm_get(PyObject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
| 79 |
{ |
| 80 |
PyObject *return_value = NULL; |
| 81 |
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
| 82 |
# define KWTUPLE (PyObject *)&_Py_SINGLETON(tuple_empty) |
| 83 |
#else |
| 84 |
# define KWTUPLE NULL |
| 85 |
#endif |
| 86 |
|
| 87 |
static const char * const _keywords[] = {"", "", NULL}; |
| 88 |
static _PyArg_Parser _parser = { |
| 89 |
.keywords = _keywords, |
| 90 |
.format = "s#|O:get", |
| 91 |
.kwtuple = KWTUPLE, |
| 92 |
}; |
| 93 |
#undef KWTUPLE |
| 94 |
const char *key; |
| 95 |
Py_ssize_t key_length; |
| 96 |
PyObject *default_value = Py_None; |
| 97 |
|
| 98 |
if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser, |
| 99 |
&key, &key_length, &default_value)) { |
| 100 |
goto exit; |
| 101 |
} |
| 102 |
Py_BEGIN_CRITICAL_SECTION(self); |
| 103 |
return_value = _dbm_dbm_get_impl((dbmobject *)self, cls, key, key_length, default_value); |
| 104 |
Py_END_CRITICAL_SECTION(); |
| 105 |
|
| 106 |
exit: |
| 107 |
return return_value; |
| 108 |
} |
| 109 |
|
| 110 |
PyDoc_STRVAR(_dbm_dbm_setdefault__doc__, |
| 111 |
"setdefault($self, key, default=b\'\', /)\n" |
| 112 |
"--\n" |
| 113 |
"\n" |
| 114 |
"Return the value for key if present, otherwise default.\n" |
| 115 |
"\n" |
| 116 |
"If key is not in the database, it is inserted with default as the value."); |
| 117 |
|
| 118 |
#define _DBM_DBM_SETDEFAULT_METHODDEF \ |
| 119 |
{"setdefault", _PyCFunction_CAST(_dbm_dbm_setdefault), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _dbm_dbm_setdefault__doc__}, |
| 120 |
|
| 121 |
static PyObject * |
| 122 |
_dbm_dbm_setdefault_impl(dbmobject *self, PyTypeObject *cls, const char *key, |
| 123 |
Py_ssize_t key_length, PyObject *default_value); |
| 124 |
|
| 125 |
static PyObject * |
| 126 |
_dbm_dbm_setdefault(PyObject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
| 127 |
{ |
| 128 |
PyObject *return_value = NULL; |
| 129 |
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
| 130 |
# define KWTUPLE (PyObject *)&_Py_SINGLETON(tuple_empty) |
| 131 |
#else |
| 132 |
# define KWTUPLE NULL |
| 133 |
#endif |
| 134 |
|
| 135 |
static const char * const _keywords[] = {"", "", NULL}; |
| 136 |
static _PyArg_Parser _parser = { |
| 137 |
.keywords = _keywords, |
| 138 |
.format = "s#|O:setdefault", |
| 139 |
.kwtuple = KWTUPLE, |
| 140 |
}; |
| 141 |
#undef KWTUPLE |
| 142 |
const char *key; |
| 143 |
Py_ssize_t key_length; |
| 144 |
PyObject *default_value = NULL; |
| 145 |
|
| 146 |
if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser, |
| 147 |
&key, &key_length, &default_value)) { |
| 148 |
goto exit; |
| 149 |
} |
| 150 |
Py_BEGIN_CRITICAL_SECTION(self); |
| 151 |
return_value = _dbm_dbm_setdefault_impl((dbmobject *)self, cls, key, key_length, default_value); |
| 152 |
Py_END_CRITICAL_SECTION(); |
| 153 |
|
| 154 |
exit: |
| 155 |
return return_value; |
| 156 |
} |
| 157 |
|
| 158 |
PyDoc_STRVAR(_dbm_dbm_clear__doc__, |
| 159 |
"clear($self, /)\n" |
| 160 |
"--\n" |
| 161 |
"\n" |
| 162 |
"Remove all items from the database."); |
| 163 |
|
| 164 |
#define _DBM_DBM_CLEAR_METHODDEF \ |
| 165 |
{"clear", _PyCFunction_CAST(_dbm_dbm_clear), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _dbm_dbm_clear__doc__}, |
| 166 |
|
| 167 |
static PyObject * |
| 168 |
_dbm_dbm_clear_impl(dbmobject *self, PyTypeObject *cls); |
| 169 |
|
| 170 |
static PyObject * |
| 171 |
_dbm_dbm_clear(PyObject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
| 172 |
{ |
| 173 |
PyObject *return_value = NULL; |
| 174 |
|
| 175 |
if (nargs || (kwnames && PyTuple_GET_SIZE(kwnames))) { |
| 176 |
PyErr_SetString(PyExc_TypeError, "clear() takes no arguments"); |
| 177 |
goto exit; |
| 178 |
} |
| 179 |
Py_BEGIN_CRITICAL_SECTION(self); |
| 180 |
return_value = _dbm_dbm_clear_impl((dbmobject *)self, cls); |
| 181 |
Py_END_CRITICAL_SECTION(); |
| 182 |
|
| 183 |
exit: |
| 184 |
return return_value; |
| 185 |
} |
| 186 |
|
| 187 |
PyDoc_STRVAR(dbmopen__doc__, |
| 188 |
"open($module, filename, flags=\'r\', mode=0o666, /)\n" |
| 189 |
"--\n" |
| 190 |
"\n" |
| 191 |
"Return a database object.\n" |
| 192 |
"\n" |
| 193 |
" filename\n" |
| 194 |
" The filename to open.\n" |
| 195 |
" flags\n" |
| 196 |
" How to open the file. \"r\" for reading, \"w\" for writing, etc.\n" |
| 197 |
" mode\n" |
| 198 |
" If creating a new file, the mode bits for the new file\n" |
| 199 |
" (e.g. os.O_RDWR)."); |
| 200 |
|
| 201 |
#define DBMOPEN_METHODDEF \ |
| 202 |
{"open", _PyCFunction_CAST(dbmopen), METH_FASTCALL, dbmopen__doc__}, |
| 203 |
|
| 204 |
static PyObject * |
| 205 |
dbmopen_impl(PyObject *module, PyObject *filename, const char *flags, |
| 206 |
int mode); |
| 207 |
|
| 208 |
static PyObject * |
| 209 |
dbmopen(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
| 210 |
{ |
| 211 |
PyObject *return_value = NULL; |
| 212 |
PyObject *filename; |
| 213 |
const char *flags = "r"; |
| 214 |
int mode = 438; |
| 215 |
|
| 216 |
if (!_PyArg_CheckPositional("open", nargs, 1, 3)) { |
| 217 |
goto exit; |
| 218 |
} |
| 219 |
filename = args[0]; |
| 220 |
if (nargs < 2) { |
| 221 |
goto skip_optional; |
| 222 |
} |
| 223 |
if (!PyUnicode_Check(args[1])) { |
| 224 |
_PyArg_BadArgument("open", "argument 2", "str", args[1]); |
| 225 |
goto exit; |
| 226 |
} |
| 227 |
Py_ssize_t flags_length; |
| 228 |
flags = PyUnicode_AsUTF8AndSize(args[1], &flags_length); |
| 229 |
if (flags == NULL) { |
| 230 |
goto exit; |
| 231 |
} |
| 232 |
if (strlen(flags) != (size_t)flags_length) { |
| 233 |
PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 234 |
goto exit; |
| 235 |
} |
| 236 |
if (nargs < 3) { |
| 237 |
goto skip_optional; |
| 238 |
} |
| 239 |
mode = PyLong_AsInt(args[2]); |
| 240 |
if (mode == -1 && PyErr_Occurred()) { |
| 241 |
goto exit; |
| 242 |
} |
| 243 |
skip_optional: |
| 244 |
return_value = dbmopen_impl(module, filename, flags, mode); |
| 245 |
|
| 246 |
exit: |
| 247 |
return return_value; |
| 248 |
} |
| 249 |
/*[clinic end generated code: output=279511ea7cda38dd input=a9049054013a1b77]*/ |