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(dbmobject *self, PyObject *key, PyObject *default_value);
16
17 static PyObject *
_gdbm_gdbm_get(dbmobject * self,PyObject * const * args,Py_ssize_t nargs)18 _gdbm_gdbm_get(dbmobject *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(dbmobject *self, PyObject *key,
50 PyObject *default_value);
51
52 static PyObject *
_gdbm_gdbm_setdefault(dbmobject * self,PyObject * const * args,Py_ssize_t nargs)53 _gdbm_gdbm_setdefault(dbmobject *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(dbmobject *self);
85
86 static PyObject *
_gdbm_gdbm_close(dbmobject * self,PyObject * Py_UNUSED (ignored))87 _gdbm_gdbm_close(dbmobject *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)_gdbm_gdbm_keys, METH_NOARGS, _gdbm_gdbm_keys__doc__},
100
101 static PyObject *
102 _gdbm_gdbm_keys_impl(dbmobject *self);
103
104 static PyObject *
_gdbm_gdbm_keys(dbmobject * self,PyObject * Py_UNUSED (ignored))105 _gdbm_gdbm_keys(dbmobject *self, PyObject *Py_UNUSED(ignored))
106 {
107 return _gdbm_gdbm_keys_impl(self);
108 }
109
110 PyDoc_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
123 static PyObject *
124 _gdbm_gdbm_firstkey_impl(dbmobject *self);
125
126 static PyObject *
_gdbm_gdbm_firstkey(dbmobject * self,PyObject * Py_UNUSED (ignored))127 _gdbm_gdbm_firstkey(dbmobject *self, PyObject *Py_UNUSED(ignored))
128 {
129 return _gdbm_gdbm_firstkey_impl(self);
130 }
131
132 PyDoc_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
149 static PyObject *
150 _gdbm_gdbm_nextkey_impl(dbmobject *self, const char *key,
151 Py_ssize_clean_t key_length);
152
153 static PyObject *
_gdbm_gdbm_nextkey(dbmobject * self,PyObject * arg)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
160 if (!PyArg_Parse(arg, "s#:nextkey", &key, &key_length)) {
161 goto exit;
162 }
163 return_value = _gdbm_gdbm_nextkey_impl(self, key, key_length);
164
165 exit:
166 return return_value;
167 }
168
169 PyDoc_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
184 static PyObject *
185 _gdbm_gdbm_reorganize_impl(dbmobject *self);
186
187 static PyObject *
_gdbm_gdbm_reorganize(dbmobject * self,PyObject * Py_UNUSED (ignored))188 _gdbm_gdbm_reorganize(dbmobject *self, PyObject *Py_UNUSED(ignored))
189 {
190 return _gdbm_gdbm_reorganize_impl(self);
191 }
192
193 PyDoc_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
205 static PyObject *
206 _gdbm_gdbm_sync_impl(dbmobject *self);
207
208 static PyObject *
_gdbm_gdbm_sync(dbmobject * self,PyObject * Py_UNUSED (ignored))209 _gdbm_gdbm_sync(dbmobject *self, PyObject *Py_UNUSED(ignored))
210 {
211 return _gdbm_gdbm_sync_impl(self);
212 }
213
214 PyDoc_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 \
242 {"open", (PyCFunction)(void(*)(void))dbmopen, METH_FASTCALL, dbmopen__doc__},
243
244 static PyObject *
245 dbmopen_impl(PyObject *module, PyObject *filename, const char *flags,
246 int mode);
247
248 static PyObject *
dbmopen(PyObject * module,PyObject * const * args,Py_ssize_t nargs)249 dbmopen(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
250 {
251 PyObject *return_value = NULL;
252 PyObject *filename;
253 const char *flags = "r";
254 int mode = 438;
255
256 if (!_PyArg_CheckPositional("open", nargs, 1, 3)) {
257 goto exit;
258 }
259 if (!PyUnicode_Check(args[0])) {
260 _PyArg_BadArgument("open", "argument 1", "str", args[0]);
261 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])) {
271 _PyArg_BadArgument("open", "argument 2", "str", args[1]);
272 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 }
295 skip_optional:
296 return_value = dbmopen_impl(module, filename, flags, mode);
297
298 exit:
299 return return_value;
300 }
301 /*[clinic end generated code: output=2766471b2fa1a816 input=a9049054013a1b77]*/
302