1 | n/a | |
---|
2 | n/a | /* DBM module using dictionary interface */ |
---|
3 | n/a | /* Author: Anthony Baxter, after dbmmodule.c */ |
---|
4 | n/a | /* Doc strings: Mitch Chapman */ |
---|
5 | n/a | |
---|
6 | n/a | |
---|
7 | n/a | #include "Python.h" |
---|
8 | n/a | |
---|
9 | n/a | #include <sys/types.h> |
---|
10 | n/a | #include <sys/stat.h> |
---|
11 | n/a | #include <fcntl.h> |
---|
12 | n/a | #include "gdbm.h" |
---|
13 | n/a | |
---|
14 | n/a | #if defined(WIN32) && !defined(__CYGWIN__) |
---|
15 | n/a | #include "gdbmerrno.h" |
---|
16 | n/a | extern const char * gdbm_strerror(gdbm_error); |
---|
17 | n/a | #endif |
---|
18 | n/a | |
---|
19 | n/a | /*[clinic input] |
---|
20 | n/a | module _gdbm |
---|
21 | n/a | class _gdbm.gdbm "dbmobject *" "&Dbmtype" |
---|
22 | n/a | [clinic start generated code]*/ |
---|
23 | n/a | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=113927c6170729b2]*/ |
---|
24 | n/a | |
---|
25 | n/a | PyDoc_STRVAR(gdbmmodule__doc__, |
---|
26 | n/a | "This module provides an interface to the GNU DBM (GDBM) library.\n\ |
---|
27 | n/a | \n\ |
---|
28 | n/a | This module is quite similar to the dbm module, but uses GDBM instead to\n\ |
---|
29 | n/a | provide some additional functionality. Please note that the file formats\n\ |
---|
30 | n/a | created by GDBM and dbm are incompatible.\n\ |
---|
31 | n/a | \n\ |
---|
32 | n/a | GDBM objects behave like mappings (dictionaries), except that keys and\n\ |
---|
33 | n/a | values are always immutable bytes-like objects or strings. Printing\n\ |
---|
34 | n/a | a GDBM object doesn't print the keys and values, and the items() and\n\ |
---|
35 | n/a | values() methods are not supported."); |
---|
36 | n/a | |
---|
37 | n/a | typedef struct { |
---|
38 | n/a | PyObject_HEAD |
---|
39 | n/a | int di_size; /* -1 means recompute */ |
---|
40 | n/a | GDBM_FILE di_dbm; |
---|
41 | n/a | } dbmobject; |
---|
42 | n/a | |
---|
43 | n/a | static PyTypeObject Dbmtype; |
---|
44 | n/a | |
---|
45 | n/a | #include "clinic/_gdbmmodule.c.h" |
---|
46 | n/a | |
---|
47 | n/a | #define is_dbmobject(v) (Py_TYPE(v) == &Dbmtype) |
---|
48 | n/a | #define check_dbmobject_open(v) if ((v)->di_dbm == NULL) \ |
---|
49 | n/a | { PyErr_SetString(DbmError, "GDBM object has already been closed"); \ |
---|
50 | n/a | return NULL; } |
---|
51 | n/a | |
---|
52 | n/a | |
---|
53 | n/a | |
---|
54 | n/a | static PyObject *DbmError; |
---|
55 | n/a | |
---|
56 | n/a | PyDoc_STRVAR(gdbm_object__doc__, |
---|
57 | n/a | "This object represents a GDBM database.\n\ |
---|
58 | n/a | GDBM objects behave like mappings (dictionaries), except that keys and\n\ |
---|
59 | n/a | values are always immutable bytes-like objects or strings. Printing\n\ |
---|
60 | n/a | a GDBM object doesn't print the keys and values, and the items() and\n\ |
---|
61 | n/a | values() methods are not supported.\n\ |
---|
62 | n/a | \n\ |
---|
63 | n/a | GDBM objects also support additional operations such as firstkey,\n\ |
---|
64 | n/a | nextkey, reorganize, and sync."); |
---|
65 | n/a | |
---|
66 | n/a | static PyObject * |
---|
67 | n/a | newdbmobject(const char *file, int flags, int mode) |
---|
68 | n/a | { |
---|
69 | n/a | dbmobject *dp; |
---|
70 | n/a | |
---|
71 | n/a | dp = PyObject_New(dbmobject, &Dbmtype); |
---|
72 | n/a | if (dp == NULL) |
---|
73 | n/a | return NULL; |
---|
74 | n/a | dp->di_size = -1; |
---|
75 | n/a | errno = 0; |
---|
76 | n/a | if ((dp->di_dbm = gdbm_open((char *)file, 0, flags, mode, NULL)) == 0) { |
---|
77 | n/a | if (errno != 0) |
---|
78 | n/a | PyErr_SetFromErrno(DbmError); |
---|
79 | n/a | else |
---|
80 | n/a | PyErr_SetString(DbmError, gdbm_strerror(gdbm_errno)); |
---|
81 | n/a | Py_DECREF(dp); |
---|
82 | n/a | return NULL; |
---|
83 | n/a | } |
---|
84 | n/a | return (PyObject *)dp; |
---|
85 | n/a | } |
---|
86 | n/a | |
---|
87 | n/a | /* Methods */ |
---|
88 | n/a | |
---|
89 | n/a | static void |
---|
90 | n/a | dbm_dealloc(dbmobject *dp) |
---|
91 | n/a | { |
---|
92 | n/a | if (dp->di_dbm) |
---|
93 | n/a | gdbm_close(dp->di_dbm); |
---|
94 | n/a | PyObject_Del(dp); |
---|
95 | n/a | } |
---|
96 | n/a | |
---|
97 | n/a | static Py_ssize_t |
---|
98 | n/a | dbm_length(dbmobject *dp) |
---|
99 | n/a | { |
---|
100 | n/a | if (dp->di_dbm == NULL) { |
---|
101 | n/a | PyErr_SetString(DbmError, "GDBM object has already been closed"); |
---|
102 | n/a | return -1; |
---|
103 | n/a | } |
---|
104 | n/a | if (dp->di_size < 0) { |
---|
105 | n/a | datum key,okey; |
---|
106 | n/a | int size; |
---|
107 | n/a | okey.dsize=0; |
---|
108 | n/a | okey.dptr=NULL; |
---|
109 | n/a | |
---|
110 | n/a | size = 0; |
---|
111 | n/a | for (key=gdbm_firstkey(dp->di_dbm); key.dptr; |
---|
112 | n/a | key = gdbm_nextkey(dp->di_dbm,okey)) { |
---|
113 | n/a | size++; |
---|
114 | n/a | if(okey.dsize) free(okey.dptr); |
---|
115 | n/a | okey=key; |
---|
116 | n/a | } |
---|
117 | n/a | dp->di_size = size; |
---|
118 | n/a | } |
---|
119 | n/a | return dp->di_size; |
---|
120 | n/a | } |
---|
121 | n/a | |
---|
122 | n/a | static PyObject * |
---|
123 | n/a | dbm_subscript(dbmobject *dp, PyObject *key) |
---|
124 | n/a | { |
---|
125 | n/a | PyObject *v; |
---|
126 | n/a | datum drec, krec; |
---|
127 | n/a | |
---|
128 | n/a | if (!PyArg_Parse(key, "s#", &krec.dptr, &krec.dsize) ) |
---|
129 | n/a | return NULL; |
---|
130 | n/a | |
---|
131 | n/a | if (dp->di_dbm == NULL) { |
---|
132 | n/a | PyErr_SetString(DbmError, |
---|
133 | n/a | "GDBM object has already been closed"); |
---|
134 | n/a | return NULL; |
---|
135 | n/a | } |
---|
136 | n/a | drec = gdbm_fetch(dp->di_dbm, krec); |
---|
137 | n/a | if (drec.dptr == 0) { |
---|
138 | n/a | PyErr_SetObject(PyExc_KeyError, key); |
---|
139 | n/a | return NULL; |
---|
140 | n/a | } |
---|
141 | n/a | v = PyBytes_FromStringAndSize(drec.dptr, drec.dsize); |
---|
142 | n/a | free(drec.dptr); |
---|
143 | n/a | return v; |
---|
144 | n/a | } |
---|
145 | n/a | |
---|
146 | n/a | /*[clinic input] |
---|
147 | n/a | _gdbm.gdbm.get |
---|
148 | n/a | |
---|
149 | n/a | key: object |
---|
150 | n/a | default: object = None |
---|
151 | n/a | / |
---|
152 | n/a | |
---|
153 | n/a | Get the value for key, or default if not present. |
---|
154 | n/a | [clinic start generated code]*/ |
---|
155 | n/a | |
---|
156 | n/a | static PyObject * |
---|
157 | n/a | _gdbm_gdbm_get_impl(dbmobject *self, PyObject *key, PyObject *default_value) |
---|
158 | n/a | /*[clinic end generated code: output=19b7c585ad4f554a input=a9c20423f34c17b6]*/ |
---|
159 | n/a | { |
---|
160 | n/a | PyObject *res; |
---|
161 | n/a | |
---|
162 | n/a | res = dbm_subscript(self, key); |
---|
163 | n/a | if (res == NULL && PyErr_ExceptionMatches(PyExc_KeyError)) { |
---|
164 | n/a | PyErr_Clear(); |
---|
165 | n/a | Py_INCREF(default_value); |
---|
166 | n/a | return default_value; |
---|
167 | n/a | } |
---|
168 | n/a | return res; |
---|
169 | n/a | } |
---|
170 | n/a | |
---|
171 | n/a | static int |
---|
172 | n/a | dbm_ass_sub(dbmobject *dp, PyObject *v, PyObject *w) |
---|
173 | n/a | { |
---|
174 | n/a | datum krec, drec; |
---|
175 | n/a | |
---|
176 | n/a | if (!PyArg_Parse(v, "s#", &krec.dptr, &krec.dsize) ) { |
---|
177 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
178 | n/a | "gdbm mappings have bytes or string indices only"); |
---|
179 | n/a | return -1; |
---|
180 | n/a | } |
---|
181 | n/a | if (dp->di_dbm == NULL) { |
---|
182 | n/a | PyErr_SetString(DbmError, |
---|
183 | n/a | "GDBM object has already been closed"); |
---|
184 | n/a | return -1; |
---|
185 | n/a | } |
---|
186 | n/a | dp->di_size = -1; |
---|
187 | n/a | if (w == NULL) { |
---|
188 | n/a | if (gdbm_delete(dp->di_dbm, krec) < 0) { |
---|
189 | n/a | PyErr_SetObject(PyExc_KeyError, v); |
---|
190 | n/a | return -1; |
---|
191 | n/a | } |
---|
192 | n/a | } |
---|
193 | n/a | else { |
---|
194 | n/a | if (!PyArg_Parse(w, "s#", &drec.dptr, &drec.dsize)) { |
---|
195 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
196 | n/a | "gdbm mappings have byte or string elements only"); |
---|
197 | n/a | return -1; |
---|
198 | n/a | } |
---|
199 | n/a | errno = 0; |
---|
200 | n/a | if (gdbm_store(dp->di_dbm, krec, drec, GDBM_REPLACE) < 0) { |
---|
201 | n/a | if (errno != 0) |
---|
202 | n/a | PyErr_SetFromErrno(DbmError); |
---|
203 | n/a | else |
---|
204 | n/a | PyErr_SetString(DbmError, |
---|
205 | n/a | gdbm_strerror(gdbm_errno)); |
---|
206 | n/a | return -1; |
---|
207 | n/a | } |
---|
208 | n/a | } |
---|
209 | n/a | return 0; |
---|
210 | n/a | } |
---|
211 | n/a | |
---|
212 | n/a | /*[clinic input] |
---|
213 | n/a | _gdbm.gdbm.setdefault |
---|
214 | n/a | |
---|
215 | n/a | key: object |
---|
216 | n/a | default: object = None |
---|
217 | n/a | / |
---|
218 | n/a | |
---|
219 | n/a | Get value for key, or set it to default and return default if not present. |
---|
220 | n/a | [clinic start generated code]*/ |
---|
221 | n/a | |
---|
222 | n/a | static PyObject * |
---|
223 | n/a | _gdbm_gdbm_setdefault_impl(dbmobject *self, PyObject *key, |
---|
224 | n/a | PyObject *default_value) |
---|
225 | n/a | /*[clinic end generated code: output=88760ee520329012 input=0db46b69e9680171]*/ |
---|
226 | n/a | { |
---|
227 | n/a | PyObject *res; |
---|
228 | n/a | |
---|
229 | n/a | res = dbm_subscript(self, key); |
---|
230 | n/a | if (res == NULL && PyErr_ExceptionMatches(PyExc_KeyError)) { |
---|
231 | n/a | PyErr_Clear(); |
---|
232 | n/a | if (dbm_ass_sub(self, key, default_value) < 0) |
---|
233 | n/a | return NULL; |
---|
234 | n/a | return dbm_subscript(self, key); |
---|
235 | n/a | } |
---|
236 | n/a | return res; |
---|
237 | n/a | } |
---|
238 | n/a | |
---|
239 | n/a | static PyMappingMethods dbm_as_mapping = { |
---|
240 | n/a | (lenfunc)dbm_length, /*mp_length*/ |
---|
241 | n/a | (binaryfunc)dbm_subscript, /*mp_subscript*/ |
---|
242 | n/a | (objobjargproc)dbm_ass_sub, /*mp_ass_subscript*/ |
---|
243 | n/a | }; |
---|
244 | n/a | |
---|
245 | n/a | /*[clinic input] |
---|
246 | n/a | _gdbm.gdbm.close |
---|
247 | n/a | |
---|
248 | n/a | Close the database. |
---|
249 | n/a | [clinic start generated code]*/ |
---|
250 | n/a | |
---|
251 | n/a | static PyObject * |
---|
252 | n/a | _gdbm_gdbm_close_impl(dbmobject *self) |
---|
253 | n/a | /*[clinic end generated code: output=23512a594598b563 input=0a203447379b45fd]*/ |
---|
254 | n/a | { |
---|
255 | n/a | if (self->di_dbm) |
---|
256 | n/a | gdbm_close(self->di_dbm); |
---|
257 | n/a | self->di_dbm = NULL; |
---|
258 | n/a | Py_RETURN_NONE; |
---|
259 | n/a | } |
---|
260 | n/a | |
---|
261 | n/a | /* XXX Should return a set or a set view */ |
---|
262 | n/a | /*[clinic input] |
---|
263 | n/a | _gdbm.gdbm.keys |
---|
264 | n/a | |
---|
265 | n/a | Get a list of all keys in the database. |
---|
266 | n/a | [clinic start generated code]*/ |
---|
267 | n/a | |
---|
268 | n/a | static PyObject * |
---|
269 | n/a | _gdbm_gdbm_keys_impl(dbmobject *self) |
---|
270 | n/a | /*[clinic end generated code: output=cb4b1776c3645dcc input=1832ee0a3132cfaf]*/ |
---|
271 | n/a | { |
---|
272 | n/a | PyObject *v, *item; |
---|
273 | n/a | datum key, nextkey; |
---|
274 | n/a | int err; |
---|
275 | n/a | |
---|
276 | n/a | if (self == NULL || !is_dbmobject(self)) { |
---|
277 | n/a | PyErr_BadInternalCall(); |
---|
278 | n/a | return NULL; |
---|
279 | n/a | } |
---|
280 | n/a | check_dbmobject_open(self); |
---|
281 | n/a | |
---|
282 | n/a | v = PyList_New(0); |
---|
283 | n/a | if (v == NULL) |
---|
284 | n/a | return NULL; |
---|
285 | n/a | |
---|
286 | n/a | key = gdbm_firstkey(self->di_dbm); |
---|
287 | n/a | while (key.dptr) { |
---|
288 | n/a | item = PyBytes_FromStringAndSize(key.dptr, key.dsize); |
---|
289 | n/a | if (item == NULL) { |
---|
290 | n/a | free(key.dptr); |
---|
291 | n/a | Py_DECREF(v); |
---|
292 | n/a | return NULL; |
---|
293 | n/a | } |
---|
294 | n/a | err = PyList_Append(v, item); |
---|
295 | n/a | Py_DECREF(item); |
---|
296 | n/a | if (err != 0) { |
---|
297 | n/a | free(key.dptr); |
---|
298 | n/a | Py_DECREF(v); |
---|
299 | n/a | return NULL; |
---|
300 | n/a | } |
---|
301 | n/a | nextkey = gdbm_nextkey(self->di_dbm, key); |
---|
302 | n/a | free(key.dptr); |
---|
303 | n/a | key = nextkey; |
---|
304 | n/a | } |
---|
305 | n/a | return v; |
---|
306 | n/a | } |
---|
307 | n/a | |
---|
308 | n/a | static int |
---|
309 | n/a | dbm_contains(PyObject *self, PyObject *arg) |
---|
310 | n/a | { |
---|
311 | n/a | dbmobject *dp = (dbmobject *)self; |
---|
312 | n/a | datum key; |
---|
313 | n/a | Py_ssize_t size; |
---|
314 | n/a | |
---|
315 | n/a | if ((dp)->di_dbm == NULL) { |
---|
316 | n/a | PyErr_SetString(DbmError, |
---|
317 | n/a | "GDBM object has already been closed"); |
---|
318 | n/a | return -1; |
---|
319 | n/a | } |
---|
320 | n/a | if (PyUnicode_Check(arg)) { |
---|
321 | n/a | key.dptr = (char *)PyUnicode_AsUTF8AndSize(arg, &size); |
---|
322 | n/a | key.dsize = size; |
---|
323 | n/a | if (key.dptr == NULL) |
---|
324 | n/a | return -1; |
---|
325 | n/a | } |
---|
326 | n/a | else if (!PyBytes_Check(arg)) { |
---|
327 | n/a | PyErr_Format(PyExc_TypeError, |
---|
328 | n/a | "gdbm key must be bytes or string, not %.100s", |
---|
329 | n/a | arg->ob_type->tp_name); |
---|
330 | n/a | return -1; |
---|
331 | n/a | } |
---|
332 | n/a | else { |
---|
333 | n/a | key.dptr = PyBytes_AS_STRING(arg); |
---|
334 | n/a | key.dsize = PyBytes_GET_SIZE(arg); |
---|
335 | n/a | } |
---|
336 | n/a | return gdbm_exists(dp->di_dbm, key); |
---|
337 | n/a | } |
---|
338 | n/a | |
---|
339 | n/a | static PySequenceMethods dbm_as_sequence = { |
---|
340 | n/a | 0, /* sq_length */ |
---|
341 | n/a | 0, /* sq_concat */ |
---|
342 | n/a | 0, /* sq_repeat */ |
---|
343 | n/a | 0, /* sq_item */ |
---|
344 | n/a | 0, /* sq_slice */ |
---|
345 | n/a | 0, /* sq_ass_item */ |
---|
346 | n/a | 0, /* sq_ass_slice */ |
---|
347 | n/a | dbm_contains, /* sq_contains */ |
---|
348 | n/a | 0, /* sq_inplace_concat */ |
---|
349 | n/a | 0, /* sq_inplace_repeat */ |
---|
350 | n/a | }; |
---|
351 | n/a | |
---|
352 | n/a | /*[clinic input] |
---|
353 | n/a | _gdbm.gdbm.firstkey |
---|
354 | n/a | |
---|
355 | n/a | Return the starting key for the traversal. |
---|
356 | n/a | |
---|
357 | n/a | It's possible to loop over every key in the database using this method |
---|
358 | n/a | and the nextkey() method. The traversal is ordered by GDBM's internal |
---|
359 | n/a | hash values, and won't be sorted by the key values. |
---|
360 | n/a | [clinic start generated code]*/ |
---|
361 | n/a | |
---|
362 | n/a | static PyObject * |
---|
363 | n/a | _gdbm_gdbm_firstkey_impl(dbmobject *self) |
---|
364 | n/a | /*[clinic end generated code: output=9ff85628d84b65d2 input=0dbd6a335d69bba0]*/ |
---|
365 | n/a | { |
---|
366 | n/a | PyObject *v; |
---|
367 | n/a | datum key; |
---|
368 | n/a | |
---|
369 | n/a | check_dbmobject_open(self); |
---|
370 | n/a | key = gdbm_firstkey(self->di_dbm); |
---|
371 | n/a | if (key.dptr) { |
---|
372 | n/a | v = PyBytes_FromStringAndSize(key.dptr, key.dsize); |
---|
373 | n/a | free(key.dptr); |
---|
374 | n/a | return v; |
---|
375 | n/a | } |
---|
376 | n/a | else { |
---|
377 | n/a | Py_RETURN_NONE; |
---|
378 | n/a | } |
---|
379 | n/a | } |
---|
380 | n/a | |
---|
381 | n/a | /*[clinic input] |
---|
382 | n/a | _gdbm.gdbm.nextkey |
---|
383 | n/a | |
---|
384 | n/a | key: str(accept={str, robuffer}, zeroes=True) |
---|
385 | n/a | / |
---|
386 | n/a | |
---|
387 | n/a | Returns the key that follows key in the traversal. |
---|
388 | n/a | |
---|
389 | n/a | The following code prints every key in the database db, without having |
---|
390 | n/a | to create a list in memory that contains them all: |
---|
391 | n/a | |
---|
392 | n/a | k = db.firstkey() |
---|
393 | n/a | while k != None: |
---|
394 | n/a | print(k) |
---|
395 | n/a | k = db.nextkey(k) |
---|
396 | n/a | [clinic start generated code]*/ |
---|
397 | n/a | |
---|
398 | n/a | static PyObject * |
---|
399 | n/a | _gdbm_gdbm_nextkey_impl(dbmobject *self, const char *key, |
---|
400 | n/a | Py_ssize_clean_t key_length) |
---|
401 | n/a | /*[clinic end generated code: output=192ab892de6eb2f6 input=1f1606943614e36f]*/ |
---|
402 | n/a | { |
---|
403 | n/a | PyObject *v; |
---|
404 | n/a | datum dbm_key, nextkey; |
---|
405 | n/a | |
---|
406 | n/a | dbm_key.dptr = (char *)key; |
---|
407 | n/a | dbm_key.dsize = key_length; |
---|
408 | n/a | check_dbmobject_open(self); |
---|
409 | n/a | nextkey = gdbm_nextkey(self->di_dbm, dbm_key); |
---|
410 | n/a | if (nextkey.dptr) { |
---|
411 | n/a | v = PyBytes_FromStringAndSize(nextkey.dptr, nextkey.dsize); |
---|
412 | n/a | free(nextkey.dptr); |
---|
413 | n/a | return v; |
---|
414 | n/a | } |
---|
415 | n/a | else { |
---|
416 | n/a | Py_RETURN_NONE; |
---|
417 | n/a | } |
---|
418 | n/a | } |
---|
419 | n/a | |
---|
420 | n/a | /*[clinic input] |
---|
421 | n/a | _gdbm.gdbm.reorganize |
---|
422 | n/a | |
---|
423 | n/a | Reorganize the database. |
---|
424 | n/a | |
---|
425 | n/a | If you have carried out a lot of deletions and would like to shrink |
---|
426 | n/a | the space used by the GDBM file, this routine will reorganize the |
---|
427 | n/a | database. GDBM will not shorten the length of a database file except |
---|
428 | n/a | by using this reorganization; otherwise, deleted file space will be |
---|
429 | n/a | kept and reused as new (key,value) pairs are added. |
---|
430 | n/a | [clinic start generated code]*/ |
---|
431 | n/a | |
---|
432 | n/a | static PyObject * |
---|
433 | n/a | _gdbm_gdbm_reorganize_impl(dbmobject *self) |
---|
434 | n/a | /*[clinic end generated code: output=38d9624df92e961d input=f6bea85bcfd40dd2]*/ |
---|
435 | n/a | { |
---|
436 | n/a | check_dbmobject_open(self); |
---|
437 | n/a | errno = 0; |
---|
438 | n/a | if (gdbm_reorganize(self->di_dbm) < 0) { |
---|
439 | n/a | if (errno != 0) |
---|
440 | n/a | PyErr_SetFromErrno(DbmError); |
---|
441 | n/a | else |
---|
442 | n/a | PyErr_SetString(DbmError, gdbm_strerror(gdbm_errno)); |
---|
443 | n/a | return NULL; |
---|
444 | n/a | } |
---|
445 | n/a | Py_RETURN_NONE; |
---|
446 | n/a | } |
---|
447 | n/a | |
---|
448 | n/a | /*[clinic input] |
---|
449 | n/a | _gdbm.gdbm.sync |
---|
450 | n/a | |
---|
451 | n/a | Flush the database to the disk file. |
---|
452 | n/a | |
---|
453 | n/a | When the database has been opened in fast mode, this method forces |
---|
454 | n/a | any unwritten data to be written to the disk. |
---|
455 | n/a | [clinic start generated code]*/ |
---|
456 | n/a | |
---|
457 | n/a | static PyObject * |
---|
458 | n/a | _gdbm_gdbm_sync_impl(dbmobject *self) |
---|
459 | n/a | /*[clinic end generated code: output=488b15f47028f125 input=2a47d2c9e153ab8a]*/ |
---|
460 | n/a | { |
---|
461 | n/a | check_dbmobject_open(self); |
---|
462 | n/a | gdbm_sync(self->di_dbm); |
---|
463 | n/a | Py_RETURN_NONE; |
---|
464 | n/a | } |
---|
465 | n/a | |
---|
466 | n/a | static PyObject * |
---|
467 | n/a | dbm__enter__(PyObject *self, PyObject *args) |
---|
468 | n/a | { |
---|
469 | n/a | Py_INCREF(self); |
---|
470 | n/a | return self; |
---|
471 | n/a | } |
---|
472 | n/a | |
---|
473 | n/a | static PyObject * |
---|
474 | n/a | dbm__exit__(PyObject *self, PyObject *args) |
---|
475 | n/a | { |
---|
476 | n/a | _Py_IDENTIFIER(close); |
---|
477 | n/a | return _PyObject_CallMethodId(self, &PyId_close, NULL); |
---|
478 | n/a | } |
---|
479 | n/a | |
---|
480 | n/a | static PyMethodDef dbm_methods[] = { |
---|
481 | n/a | _GDBM_GDBM_CLOSE_METHODDEF |
---|
482 | n/a | _GDBM_GDBM_KEYS_METHODDEF |
---|
483 | n/a | _GDBM_GDBM_FIRSTKEY_METHODDEF |
---|
484 | n/a | _GDBM_GDBM_NEXTKEY_METHODDEF |
---|
485 | n/a | _GDBM_GDBM_REORGANIZE_METHODDEF |
---|
486 | n/a | _GDBM_GDBM_SYNC_METHODDEF |
---|
487 | n/a | _GDBM_GDBM_GET_METHODDEF |
---|
488 | n/a | _GDBM_GDBM_GET_METHODDEF |
---|
489 | n/a | _GDBM_GDBM_SETDEFAULT_METHODDEF |
---|
490 | n/a | {"__enter__", dbm__enter__, METH_NOARGS, NULL}, |
---|
491 | n/a | {"__exit__", dbm__exit__, METH_VARARGS, NULL}, |
---|
492 | n/a | {NULL, NULL} /* sentinel */ |
---|
493 | n/a | }; |
---|
494 | n/a | |
---|
495 | n/a | static PyTypeObject Dbmtype = { |
---|
496 | n/a | PyVarObject_HEAD_INIT(0, 0) |
---|
497 | n/a | "_gdbm.gdbm", |
---|
498 | n/a | sizeof(dbmobject), |
---|
499 | n/a | 0, |
---|
500 | n/a | (destructor)dbm_dealloc, /*tp_dealloc*/ |
---|
501 | n/a | 0, /*tp_print*/ |
---|
502 | n/a | 0, /*tp_getattr*/ |
---|
503 | n/a | 0, /*tp_setattr*/ |
---|
504 | n/a | 0, /*tp_reserved*/ |
---|
505 | n/a | 0, /*tp_repr*/ |
---|
506 | n/a | 0, /*tp_as_number*/ |
---|
507 | n/a | &dbm_as_sequence, /*tp_as_sequence*/ |
---|
508 | n/a | &dbm_as_mapping, /*tp_as_mapping*/ |
---|
509 | n/a | 0, /*tp_hash*/ |
---|
510 | n/a | 0, /*tp_call*/ |
---|
511 | n/a | 0, /*tp_str*/ |
---|
512 | n/a | 0, /*tp_getattro*/ |
---|
513 | n/a | 0, /*tp_setattro*/ |
---|
514 | n/a | 0, /*tp_as_buffer*/ |
---|
515 | n/a | Py_TPFLAGS_DEFAULT, /*tp_xxx4*/ |
---|
516 | n/a | gdbm_object__doc__, /*tp_doc*/ |
---|
517 | n/a | 0, /*tp_traverse*/ |
---|
518 | n/a | 0, /*tp_clear*/ |
---|
519 | n/a | 0, /*tp_richcompare*/ |
---|
520 | n/a | 0, /*tp_weaklistoffset*/ |
---|
521 | n/a | 0, /*tp_iter*/ |
---|
522 | n/a | 0, /*tp_iternext*/ |
---|
523 | n/a | dbm_methods, /*tp_methods*/ |
---|
524 | n/a | }; |
---|
525 | n/a | |
---|
526 | n/a | /* ----------------------------------------------------------------- */ |
---|
527 | n/a | |
---|
528 | n/a | /*[clinic input] |
---|
529 | n/a | _gdbm.open as dbmopen |
---|
530 | n/a | filename as name: str |
---|
531 | n/a | flags: str="r" |
---|
532 | n/a | mode: int(py_default="0o666") = 0o666 |
---|
533 | n/a | / |
---|
534 | n/a | |
---|
535 | n/a | Open a dbm database and return a dbm object. |
---|
536 | n/a | |
---|
537 | n/a | The filename argument is the name of the database file. |
---|
538 | n/a | |
---|
539 | n/a | The optional flags argument can be 'r' (to open an existing database |
---|
540 | n/a | for reading only -- default), 'w' (to open an existing database for |
---|
541 | n/a | reading and writing), 'c' (which creates the database if it doesn't |
---|
542 | n/a | exist), or 'n' (which always creates a new empty database). |
---|
543 | n/a | |
---|
544 | n/a | Some versions of gdbm support additional flags which must be |
---|
545 | n/a | appended to one of the flags described above. The module constant |
---|
546 | n/a | 'open_flags' is a string of valid additional flags. The 'f' flag |
---|
547 | n/a | opens the database in fast mode; altered data will not automatically |
---|
548 | n/a | be written to the disk after every change. This results in faster |
---|
549 | n/a | writes to the database, but may result in an inconsistent database |
---|
550 | n/a | if the program crashes while the database is still open. Use the |
---|
551 | n/a | sync() method to force any unwritten data to be written to the disk. |
---|
552 | n/a | The 's' flag causes all database operations to be synchronized to |
---|
553 | n/a | disk. The 'u' flag disables locking of the database file. |
---|
554 | n/a | |
---|
555 | n/a | The optional mode argument is the Unix mode of the file, used only |
---|
556 | n/a | when the database has to be created. It defaults to octal 0o666. |
---|
557 | n/a | [clinic start generated code]*/ |
---|
558 | n/a | |
---|
559 | n/a | static PyObject * |
---|
560 | n/a | dbmopen_impl(PyObject *module, const char *name, const char *flags, int mode) |
---|
561 | n/a | /*[clinic end generated code: output=31aa1bafdf5da688 input=55563cd60e51984a]*/ |
---|
562 | n/a | { |
---|
563 | n/a | int iflags; |
---|
564 | n/a | |
---|
565 | n/a | switch (flags[0]) { |
---|
566 | n/a | case 'r': |
---|
567 | n/a | iflags = GDBM_READER; |
---|
568 | n/a | break; |
---|
569 | n/a | case 'w': |
---|
570 | n/a | iflags = GDBM_WRITER; |
---|
571 | n/a | break; |
---|
572 | n/a | case 'c': |
---|
573 | n/a | iflags = GDBM_WRCREAT; |
---|
574 | n/a | break; |
---|
575 | n/a | case 'n': |
---|
576 | n/a | iflags = GDBM_NEWDB; |
---|
577 | n/a | break; |
---|
578 | n/a | default: |
---|
579 | n/a | PyErr_SetString(DbmError, |
---|
580 | n/a | "First flag must be one of 'r', 'w', 'c' or 'n'"); |
---|
581 | n/a | return NULL; |
---|
582 | n/a | } |
---|
583 | n/a | for (flags++; *flags != '\0'; flags++) { |
---|
584 | n/a | char buf[40]; |
---|
585 | n/a | switch (*flags) { |
---|
586 | n/a | #ifdef GDBM_FAST |
---|
587 | n/a | case 'f': |
---|
588 | n/a | iflags |= GDBM_FAST; |
---|
589 | n/a | break; |
---|
590 | n/a | #endif |
---|
591 | n/a | #ifdef GDBM_SYNC |
---|
592 | n/a | case 's': |
---|
593 | n/a | iflags |= GDBM_SYNC; |
---|
594 | n/a | break; |
---|
595 | n/a | #endif |
---|
596 | n/a | #ifdef GDBM_NOLOCK |
---|
597 | n/a | case 'u': |
---|
598 | n/a | iflags |= GDBM_NOLOCK; |
---|
599 | n/a | break; |
---|
600 | n/a | #endif |
---|
601 | n/a | default: |
---|
602 | n/a | PyOS_snprintf(buf, sizeof(buf), "Flag '%c' is not supported.", |
---|
603 | n/a | *flags); |
---|
604 | n/a | PyErr_SetString(DbmError, buf); |
---|
605 | n/a | return NULL; |
---|
606 | n/a | } |
---|
607 | n/a | } |
---|
608 | n/a | |
---|
609 | n/a | return newdbmobject(name, iflags, mode); |
---|
610 | n/a | } |
---|
611 | n/a | |
---|
612 | n/a | static const char dbmmodule_open_flags[] = "rwcn" |
---|
613 | n/a | #ifdef GDBM_FAST |
---|
614 | n/a | "f" |
---|
615 | n/a | #endif |
---|
616 | n/a | #ifdef GDBM_SYNC |
---|
617 | n/a | "s" |
---|
618 | n/a | #endif |
---|
619 | n/a | #ifdef GDBM_NOLOCK |
---|
620 | n/a | "u" |
---|
621 | n/a | #endif |
---|
622 | n/a | ; |
---|
623 | n/a | |
---|
624 | n/a | static PyMethodDef dbmmodule_methods[] = { |
---|
625 | n/a | DBMOPEN_METHODDEF |
---|
626 | n/a | { 0, 0 }, |
---|
627 | n/a | }; |
---|
628 | n/a | |
---|
629 | n/a | |
---|
630 | n/a | static struct PyModuleDef _gdbmmodule = { |
---|
631 | n/a | PyModuleDef_HEAD_INIT, |
---|
632 | n/a | "_gdbm", |
---|
633 | n/a | gdbmmodule__doc__, |
---|
634 | n/a | -1, |
---|
635 | n/a | dbmmodule_methods, |
---|
636 | n/a | NULL, |
---|
637 | n/a | NULL, |
---|
638 | n/a | NULL, |
---|
639 | n/a | NULL |
---|
640 | n/a | }; |
---|
641 | n/a | |
---|
642 | n/a | PyMODINIT_FUNC |
---|
643 | n/a | PyInit__gdbm(void) { |
---|
644 | n/a | PyObject *m, *d, *s; |
---|
645 | n/a | |
---|
646 | n/a | if (PyType_Ready(&Dbmtype) < 0) |
---|
647 | n/a | return NULL; |
---|
648 | n/a | m = PyModule_Create(&_gdbmmodule); |
---|
649 | n/a | if (m == NULL) |
---|
650 | n/a | return NULL; |
---|
651 | n/a | d = PyModule_GetDict(m); |
---|
652 | n/a | DbmError = PyErr_NewException("_gdbm.error", PyExc_IOError, NULL); |
---|
653 | n/a | if (DbmError != NULL) { |
---|
654 | n/a | PyDict_SetItemString(d, "error", DbmError); |
---|
655 | n/a | s = PyUnicode_FromString(dbmmodule_open_flags); |
---|
656 | n/a | PyDict_SetItemString(d, "open_flags", s); |
---|
657 | n/a | Py_DECREF(s); |
---|
658 | n/a | } |
---|
659 | n/a | return m; |
---|
660 | n/a | } |
---|