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