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