blob: ffd2179f36970adcd6bd94f8fdb3be1e786c56a8 [file] [log] [blame]
Serhiy Storchaka9260e772015-04-17 21:05:18 +03001/*[clinic input]
2preserve
3[clinic start generated code]*/
4
5PyDoc_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 \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +020012 {"get", (PyCFunction)(void(*)(void))_gdbm_gdbm_get, METH_FASTCALL, _gdbm_gdbm_get__doc__},
Serhiy Storchaka9260e772015-04-17 21:05:18 +030013
14static PyObject *
Dong-hee Nac4862e32020-06-17 01:41:23 +090015_gdbm_gdbm_get_impl(gdbmobject *self, PyObject *key, PyObject *default_value);
Serhiy Storchaka9260e772015-04-17 21:05:18 +030016
17static PyObject *
Dong-hee Nac4862e32020-06-17 01:41:23 +090018_gdbm_gdbm_get(gdbmobject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka9260e772015-04-17 21:05:18 +030019{
20 PyObject *return_value = NULL;
21 PyObject *key;
22 PyObject *default_value = Py_None;
23
Serhiy Storchaka2a39d252019-01-11 18:01:42 +020024 if (!_PyArg_CheckPositional("get", nargs, 1, 2)) {
Victor Stinner0c4a8282017-01-17 02:21:47 +010025 goto exit;
26 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +020027 key = args[0];
28 if (nargs < 2) {
29 goto skip_optional;
30 }
31 default_value = args[1];
32skip_optional:
Serhiy Storchaka9260e772015-04-17 21:05:18 +030033 return_value = _gdbm_gdbm_get_impl(self, key, default_value);
34
35exit:
36 return return_value;
37}
38
39PyDoc_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 \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +020046 {"setdefault", (PyCFunction)(void(*)(void))_gdbm_gdbm_setdefault, METH_FASTCALL, _gdbm_gdbm_setdefault__doc__},
Serhiy Storchaka9260e772015-04-17 21:05:18 +030047
48static PyObject *
Dong-hee Nac4862e32020-06-17 01:41:23 +090049_gdbm_gdbm_setdefault_impl(gdbmobject *self, PyObject *key,
Serhiy Storchaka9260e772015-04-17 21:05:18 +030050 PyObject *default_value);
51
52static PyObject *
Dong-hee Nac4862e32020-06-17 01:41:23 +090053_gdbm_gdbm_setdefault(gdbmobject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka9260e772015-04-17 21:05:18 +030054{
55 PyObject *return_value = NULL;
56 PyObject *key;
57 PyObject *default_value = Py_None;
58
Serhiy Storchaka2a39d252019-01-11 18:01:42 +020059 if (!_PyArg_CheckPositional("setdefault", nargs, 1, 2)) {
Victor Stinner0c4a8282017-01-17 02:21:47 +010060 goto exit;
61 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +020062 key = args[0];
63 if (nargs < 2) {
64 goto skip_optional;
65 }
66 default_value = args[1];
67skip_optional:
Serhiy Storchaka9260e772015-04-17 21:05:18 +030068 return_value = _gdbm_gdbm_setdefault_impl(self, key, default_value);
69
70exit:
71 return return_value;
72}
73
74PyDoc_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
83static PyObject *
Dong-hee Nac4862e32020-06-17 01:41:23 +090084_gdbm_gdbm_close_impl(gdbmobject *self);
Serhiy Storchaka9260e772015-04-17 21:05:18 +030085
86static PyObject *
Dong-hee Nac4862e32020-06-17 01:41:23 +090087_gdbm_gdbm_close(gdbmobject *self, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka9260e772015-04-17 21:05:18 +030088{
89 return _gdbm_gdbm_close_impl(self);
90}
91
92PyDoc_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 \
Dong-hee Nac4862e32020-06-17 01:41:23 +090099 {"keys", (PyCFunction)(void(*)(void))_gdbm_gdbm_keys, METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _gdbm_gdbm_keys__doc__},
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300100
101static PyObject *
Dong-hee Nac4862e32020-06-17 01:41:23 +0900102_gdbm_gdbm_keys_impl(gdbmobject *self, PyTypeObject *cls);
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300103
104static PyObject *
Dong-hee Nac4862e32020-06-17 01:41:23 +0900105_gdbm_gdbm_keys(gdbmobject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300106{
Dong-hee Nac4862e32020-06-17 01:41:23 +0900107 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
117exit:
118 return return_value;
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300119}
120
121PyDoc_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 \
Dong-hee Nac4862e32020-06-17 01:41:23 +0900132 {"firstkey", (PyCFunction)(void(*)(void))_gdbm_gdbm_firstkey, METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _gdbm_gdbm_firstkey__doc__},
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300133
134static PyObject *
Dong-hee Nac4862e32020-06-17 01:41:23 +0900135_gdbm_gdbm_firstkey_impl(gdbmobject *self, PyTypeObject *cls);
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300136
137static PyObject *
Dong-hee Nac4862e32020-06-17 01:41:23 +0900138_gdbm_gdbm_firstkey(gdbmobject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300139{
Dong-hee Nac4862e32020-06-17 01:41:23 +0900140 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
150exit:
151 return return_value;
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300152}
153
154PyDoc_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 != None:\n"
165" print(k)\n"
166" k = db.nextkey(k)");
167
168#define _GDBM_GDBM_NEXTKEY_METHODDEF \
Dong-hee Nac4862e32020-06-17 01:41:23 +0900169 {"nextkey", (PyCFunction)(void(*)(void))_gdbm_gdbm_nextkey, METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _gdbm_gdbm_nextkey__doc__},
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300170
171static PyObject *
Dong-hee Nac4862e32020-06-17 01:41:23 +0900172_gdbm_gdbm_nextkey_impl(gdbmobject *self, PyTypeObject *cls, const char *key,
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300173 Py_ssize_clean_t key_length);
174
175static PyObject *
Dong-hee Nac4862e32020-06-17 01:41:23 +0900176_gdbm_gdbm_nextkey(gdbmobject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300177{
178 PyObject *return_value = NULL;
Dong-hee Nac4862e32020-06-17 01:41:23 +0900179 static const char * const _keywords[] = {"", NULL};
180 static _PyArg_Parser _parser = {"s#:nextkey", _keywords, 0};
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300181 const char *key;
182 Py_ssize_clean_t key_length;
183
Dong-hee Nac4862e32020-06-17 01:41:23 +0900184 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
185 &key, &key_length)) {
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300186 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300187 }
Dong-hee Nac4862e32020-06-17 01:41:23 +0900188 return_value = _gdbm_gdbm_nextkey_impl(self, cls, key, key_length);
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300189
190exit:
191 return return_value;
192}
193
194PyDoc_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 \
Dong-hee Nac4862e32020-06-17 01:41:23 +0900207 {"reorganize", (PyCFunction)(void(*)(void))_gdbm_gdbm_reorganize, METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _gdbm_gdbm_reorganize__doc__},
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300208
209static PyObject *
Dong-hee Nac4862e32020-06-17 01:41:23 +0900210_gdbm_gdbm_reorganize_impl(gdbmobject *self, PyTypeObject *cls);
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300211
212static PyObject *
Dong-hee Nac4862e32020-06-17 01:41:23 +0900213_gdbm_gdbm_reorganize(gdbmobject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300214{
Dong-hee Nac4862e32020-06-17 01:41:23 +0900215 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
225exit:
226 return return_value;
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300227}
228
229PyDoc_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 \
Dong-hee Nac4862e32020-06-17 01:41:23 +0900239 {"sync", (PyCFunction)(void(*)(void))_gdbm_gdbm_sync, METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _gdbm_gdbm_sync__doc__},
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300240
241static PyObject *
Dong-hee Nac4862e32020-06-17 01:41:23 +0900242_gdbm_gdbm_sync_impl(gdbmobject *self, PyTypeObject *cls);
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300243
244static PyObject *
Dong-hee Nac4862e32020-06-17 01:41:23 +0900245_gdbm_gdbm_sync(gdbmobject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300246{
Dong-hee Nac4862e32020-06-17 01:41:23 +0900247 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
257exit:
258 return return_value;
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300259}
260
261PyDoc_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 \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200289 {"open", (PyCFunction)(void(*)(void))dbmopen, METH_FASTCALL, dbmopen__doc__},
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300290
291static PyObject *
Serhiy Storchaka6f600ff2018-02-26 16:02:22 +0200292dbmopen_impl(PyObject *module, PyObject *filename, const char *flags,
293 int mode);
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300294
295static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200296dbmopen(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300297{
298 PyObject *return_value = NULL;
Serhiy Storchaka6f600ff2018-02-26 16:02:22 +0200299 PyObject *filename;
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300300 const char *flags = "r";
301 int mode = 438;
302
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200303 if (!_PyArg_CheckPositional("open", nargs, 1, 3)) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100304 goto exit;
305 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200306 if (!PyUnicode_Check(args[0])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200307 _PyArg_BadArgument("open", "argument 1", "str", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200308 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])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200318 _PyArg_BadArgument("open", "argument 2", "str", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200319 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 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200333 mode = _PyLong_AsInt(args[2]);
334 if (mode == -1 && PyErr_Occurred()) {
335 goto exit;
336 }
337skip_optional:
Serhiy Storchaka6f600ff2018-02-26 16:02:22 +0200338 return_value = dbmopen_impl(module, filename, flags, mode);
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300339
340exit:
341 return return_value;
342}
Dong-hee Nac4862e32020-06-17 01:41:23 +0900343/*[clinic end generated code: output=e84bc6ac82fcb6d4 input=a9049054013a1b77]*/