blob: aa37a24d3b211505542c8325e987c0cb95f31065 [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 *
15_gdbm_gdbm_get_impl(dbmobject *self, PyObject *key, PyObject *default_value);
16
17static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +020018_gdbm_gdbm_get(dbmobject *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 *
49_gdbm_gdbm_setdefault_impl(dbmobject *self, PyObject *key,
50 PyObject *default_value);
51
52static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +020053_gdbm_gdbm_setdefault(dbmobject *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 *
84_gdbm_gdbm_close_impl(dbmobject *self);
85
86static PyObject *
87_gdbm_gdbm_close(dbmobject *self, PyObject *Py_UNUSED(ignored))
88{
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 \
99 {"keys", (PyCFunction)_gdbm_gdbm_keys, METH_NOARGS, _gdbm_gdbm_keys__doc__},
100
101static PyObject *
102_gdbm_gdbm_keys_impl(dbmobject *self);
103
104static PyObject *
105_gdbm_gdbm_keys(dbmobject *self, PyObject *Py_UNUSED(ignored))
106{
107 return _gdbm_gdbm_keys_impl(self);
108}
109
110PyDoc_STRVAR(_gdbm_gdbm_firstkey__doc__,
111"firstkey($self, /)\n"
112"--\n"
113"\n"
114"Return the starting key for the traversal.\n"
115"\n"
116"It\'s possible to loop over every key in the database using this method\n"
117"and the nextkey() method. The traversal is ordered by GDBM\'s internal\n"
118"hash values, and won\'t be sorted by the key values.");
119
120#define _GDBM_GDBM_FIRSTKEY_METHODDEF \
121 {"firstkey", (PyCFunction)_gdbm_gdbm_firstkey, METH_NOARGS, _gdbm_gdbm_firstkey__doc__},
122
123static PyObject *
124_gdbm_gdbm_firstkey_impl(dbmobject *self);
125
126static PyObject *
127_gdbm_gdbm_firstkey(dbmobject *self, PyObject *Py_UNUSED(ignored))
128{
129 return _gdbm_gdbm_firstkey_impl(self);
130}
131
132PyDoc_STRVAR(_gdbm_gdbm_nextkey__doc__,
133"nextkey($self, key, /)\n"
134"--\n"
135"\n"
136"Returns the key that follows key in the traversal.\n"
137"\n"
138"The following code prints every key in the database db, without having\n"
139"to create a list in memory that contains them all:\n"
140"\n"
141" k = db.firstkey()\n"
142" while k != None:\n"
143" print(k)\n"
144" k = db.nextkey(k)");
145
146#define _GDBM_GDBM_NEXTKEY_METHODDEF \
147 {"nextkey", (PyCFunction)_gdbm_gdbm_nextkey, METH_O, _gdbm_gdbm_nextkey__doc__},
148
149static PyObject *
150_gdbm_gdbm_nextkey_impl(dbmobject *self, const char *key,
151 Py_ssize_clean_t key_length);
152
153static PyObject *
154_gdbm_gdbm_nextkey(dbmobject *self, PyObject *arg)
155{
156 PyObject *return_value = NULL;
157 const char *key;
158 Py_ssize_clean_t key_length;
159
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300160 if (!PyArg_Parse(arg, "s#:nextkey", &key, &key_length)) {
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300161 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300162 }
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300163 return_value = _gdbm_gdbm_nextkey_impl(self, key, key_length);
164
165exit:
166 return return_value;
167}
168
169PyDoc_STRVAR(_gdbm_gdbm_reorganize__doc__,
170"reorganize($self, /)\n"
171"--\n"
172"\n"
173"Reorganize the database.\n"
174"\n"
175"If you have carried out a lot of deletions and would like to shrink\n"
176"the space used by the GDBM file, this routine will reorganize the\n"
177"database. GDBM will not shorten the length of a database file except\n"
178"by using this reorganization; otherwise, deleted file space will be\n"
179"kept and reused as new (key,value) pairs are added.");
180
181#define _GDBM_GDBM_REORGANIZE_METHODDEF \
182 {"reorganize", (PyCFunction)_gdbm_gdbm_reorganize, METH_NOARGS, _gdbm_gdbm_reorganize__doc__},
183
184static PyObject *
185_gdbm_gdbm_reorganize_impl(dbmobject *self);
186
187static PyObject *
188_gdbm_gdbm_reorganize(dbmobject *self, PyObject *Py_UNUSED(ignored))
189{
190 return _gdbm_gdbm_reorganize_impl(self);
191}
192
193PyDoc_STRVAR(_gdbm_gdbm_sync__doc__,
194"sync($self, /)\n"
195"--\n"
196"\n"
197"Flush the database to the disk file.\n"
198"\n"
199"When the database has been opened in fast mode, this method forces\n"
200"any unwritten data to be written to the disk.");
201
202#define _GDBM_GDBM_SYNC_METHODDEF \
203 {"sync", (PyCFunction)_gdbm_gdbm_sync, METH_NOARGS, _gdbm_gdbm_sync__doc__},
204
205static PyObject *
206_gdbm_gdbm_sync_impl(dbmobject *self);
207
208static PyObject *
209_gdbm_gdbm_sync(dbmobject *self, PyObject *Py_UNUSED(ignored))
210{
211 return _gdbm_gdbm_sync_impl(self);
212}
213
214PyDoc_STRVAR(dbmopen__doc__,
215"open($module, filename, flags=\'r\', mode=0o666, /)\n"
216"--\n"
217"\n"
218"Open a dbm database and return a dbm object.\n"
219"\n"
220"The filename argument is the name of the database file.\n"
221"\n"
222"The optional flags argument can be \'r\' (to open an existing database\n"
223"for reading only -- default), \'w\' (to open an existing database for\n"
224"reading and writing), \'c\' (which creates the database if it doesn\'t\n"
225"exist), or \'n\' (which always creates a new empty database).\n"
226"\n"
227"Some versions of gdbm support additional flags which must be\n"
228"appended to one of the flags described above. The module constant\n"
229"\'open_flags\' is a string of valid additional flags. The \'f\' flag\n"
230"opens the database in fast mode; altered data will not automatically\n"
231"be written to the disk after every change. This results in faster\n"
232"writes to the database, but may result in an inconsistent database\n"
233"if the program crashes while the database is still open. Use the\n"
234"sync() method to force any unwritten data to be written to the disk.\n"
235"The \'s\' flag causes all database operations to be synchronized to\n"
236"disk. The \'u\' flag disables locking of the database file.\n"
237"\n"
238"The optional mode argument is the Unix mode of the file, used only\n"
239"when the database has to be created. It defaults to octal 0o666.");
240
241#define DBMOPEN_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200242 {"open", (PyCFunction)(void(*)(void))dbmopen, METH_FASTCALL, dbmopen__doc__},
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300243
244static PyObject *
Serhiy Storchaka6f600ff2018-02-26 16:02:22 +0200245dbmopen_impl(PyObject *module, PyObject *filename, const char *flags,
246 int mode);
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300247
248static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200249dbmopen(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300250{
251 PyObject *return_value = NULL;
Serhiy Storchaka6f600ff2018-02-26 16:02:22 +0200252 PyObject *filename;
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300253 const char *flags = "r";
254 int mode = 438;
255
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200256 if (!_PyArg_CheckPositional("open", nargs, 1, 3)) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100257 goto exit;
258 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200259 if (!PyUnicode_Check(args[0])) {
Serhiy Storchaka96631dc2019-08-29 18:29:59 +0300260 _PyArg_BadArgument("open", "argument 1", "str", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200261 goto exit;
262 }
263 if (PyUnicode_READY(args[0]) == -1) {
264 goto exit;
265 }
266 filename = args[0];
267 if (nargs < 2) {
268 goto skip_optional;
269 }
270 if (!PyUnicode_Check(args[1])) {
Serhiy Storchaka96631dc2019-08-29 18:29:59 +0300271 _PyArg_BadArgument("open", "argument 2", "str", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200272 goto exit;
273 }
274 Py_ssize_t flags_length;
275 flags = PyUnicode_AsUTF8AndSize(args[1], &flags_length);
276 if (flags == NULL) {
277 goto exit;
278 }
279 if (strlen(flags) != (size_t)flags_length) {
280 PyErr_SetString(PyExc_ValueError, "embedded null character");
281 goto exit;
282 }
283 if (nargs < 3) {
284 goto skip_optional;
285 }
286 if (PyFloat_Check(args[2])) {
287 PyErr_SetString(PyExc_TypeError,
288 "integer argument expected, got float" );
289 goto exit;
290 }
291 mode = _PyLong_AsInt(args[2]);
292 if (mode == -1 && PyErr_Occurred()) {
293 goto exit;
294 }
295skip_optional:
Serhiy Storchaka6f600ff2018-02-26 16:02:22 +0200296 return_value = dbmopen_impl(module, filename, flags, mode);
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300297
298exit:
299 return return_value;
300}
Serhiy Storchaka96631dc2019-08-29 18:29:59 +0300301/*[clinic end generated code: output=2766471b2fa1a816 input=a9049054013a1b77]*/