1 | n/a | |
---|
2 | n/a | /* DBM module using dictionary interface */ |
---|
3 | n/a | |
---|
4 | n/a | |
---|
5 | n/a | #define PY_SSIZE_T_CLEAN |
---|
6 | n/a | #include "Python.h" |
---|
7 | n/a | |
---|
8 | n/a | #include <sys/types.h> |
---|
9 | n/a | #include <sys/stat.h> |
---|
10 | n/a | #include <fcntl.h> |
---|
11 | n/a | |
---|
12 | n/a | /* Some Linux systems install gdbm/ndbm.h, but not ndbm.h. This supports |
---|
13 | n/a | * whichever configure was able to locate. |
---|
14 | n/a | */ |
---|
15 | n/a | #if defined(HAVE_NDBM_H) |
---|
16 | n/a | #include <ndbm.h> |
---|
17 | n/a | static const char which_dbm[] = "GNU gdbm"; /* EMX port of GDBM */ |
---|
18 | n/a | #elif defined(HAVE_GDBM_NDBM_H) |
---|
19 | n/a | #include <gdbm/ndbm.h> |
---|
20 | n/a | static const char which_dbm[] = "GNU gdbm"; |
---|
21 | n/a | #elif defined(HAVE_GDBM_DASH_NDBM_H) |
---|
22 | n/a | #include <gdbm-ndbm.h> |
---|
23 | n/a | static const char which_dbm[] = "GNU gdbm"; |
---|
24 | n/a | #elif defined(HAVE_BERKDB_H) |
---|
25 | n/a | #include <db.h> |
---|
26 | n/a | static const char which_dbm[] = "Berkeley DB"; |
---|
27 | n/a | #else |
---|
28 | n/a | #error "No ndbm.h available!" |
---|
29 | n/a | #endif |
---|
30 | n/a | |
---|
31 | n/a | /*[clinic input] |
---|
32 | n/a | module _dbm |
---|
33 | n/a | class _dbm.dbm "dbmobject *" "&Dbmtype" |
---|
34 | n/a | [clinic start generated code]*/ |
---|
35 | n/a | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=9b1aa8756d16150e]*/ |
---|
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 | DBM *di_dbm; |
---|
41 | n/a | } dbmobject; |
---|
42 | n/a | |
---|
43 | n/a | #include "clinic/_dbmmodule.c.h" |
---|
44 | n/a | |
---|
45 | n/a | static PyTypeObject Dbmtype; |
---|
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, "DBM object has already been closed"); \ |
---|
50 | n/a | return NULL; } |
---|
51 | n/a | |
---|
52 | n/a | static PyObject *DbmError; |
---|
53 | n/a | |
---|
54 | n/a | static PyObject * |
---|
55 | n/a | newdbmobject(const char *file, int flags, int mode) |
---|
56 | n/a | { |
---|
57 | n/a | dbmobject *dp; |
---|
58 | n/a | |
---|
59 | n/a | dp = PyObject_New(dbmobject, &Dbmtype); |
---|
60 | n/a | if (dp == NULL) |
---|
61 | n/a | return NULL; |
---|
62 | n/a | dp->di_size = -1; |
---|
63 | n/a | /* See issue #19296 */ |
---|
64 | n/a | if ( (dp->di_dbm = dbm_open((char *)file, flags, mode)) == 0 ) { |
---|
65 | n/a | PyErr_SetFromErrno(DbmError); |
---|
66 | n/a | Py_DECREF(dp); |
---|
67 | n/a | return NULL; |
---|
68 | n/a | } |
---|
69 | n/a | return (PyObject *)dp; |
---|
70 | n/a | } |
---|
71 | n/a | |
---|
72 | n/a | /* Methods */ |
---|
73 | n/a | |
---|
74 | n/a | static void |
---|
75 | n/a | dbm_dealloc(dbmobject *dp) |
---|
76 | n/a | { |
---|
77 | n/a | if ( dp->di_dbm ) |
---|
78 | n/a | dbm_close(dp->di_dbm); |
---|
79 | n/a | PyObject_Del(dp); |
---|
80 | n/a | } |
---|
81 | n/a | |
---|
82 | n/a | static Py_ssize_t |
---|
83 | n/a | dbm_length(dbmobject *dp) |
---|
84 | n/a | { |
---|
85 | n/a | if (dp->di_dbm == NULL) { |
---|
86 | n/a | PyErr_SetString(DbmError, "DBM object has already been closed"); |
---|
87 | n/a | return -1; |
---|
88 | n/a | } |
---|
89 | n/a | if ( dp->di_size < 0 ) { |
---|
90 | n/a | datum key; |
---|
91 | n/a | int size; |
---|
92 | n/a | |
---|
93 | n/a | size = 0; |
---|
94 | n/a | for ( key=dbm_firstkey(dp->di_dbm); key.dptr; |
---|
95 | n/a | key = dbm_nextkey(dp->di_dbm)) |
---|
96 | n/a | size++; |
---|
97 | n/a | dp->di_size = size; |
---|
98 | n/a | } |
---|
99 | n/a | return dp->di_size; |
---|
100 | n/a | } |
---|
101 | n/a | |
---|
102 | n/a | static PyObject * |
---|
103 | n/a | dbm_subscript(dbmobject *dp, PyObject *key) |
---|
104 | n/a | { |
---|
105 | n/a | datum drec, krec; |
---|
106 | n/a | Py_ssize_t tmp_size; |
---|
107 | n/a | |
---|
108 | n/a | if (!PyArg_Parse(key, "s#", &krec.dptr, &tmp_size) ) |
---|
109 | n/a | return NULL; |
---|
110 | n/a | |
---|
111 | n/a | krec.dsize = tmp_size; |
---|
112 | n/a | check_dbmobject_open(dp); |
---|
113 | n/a | drec = dbm_fetch(dp->di_dbm, krec); |
---|
114 | n/a | if ( drec.dptr == 0 ) { |
---|
115 | n/a | PyErr_SetObject(PyExc_KeyError, key); |
---|
116 | n/a | return NULL; |
---|
117 | n/a | } |
---|
118 | n/a | if ( dbm_error(dp->di_dbm) ) { |
---|
119 | n/a | dbm_clearerr(dp->di_dbm); |
---|
120 | n/a | PyErr_SetString(DbmError, ""); |
---|
121 | n/a | return NULL; |
---|
122 | n/a | } |
---|
123 | n/a | return PyBytes_FromStringAndSize(drec.dptr, drec.dsize); |
---|
124 | n/a | } |
---|
125 | n/a | |
---|
126 | n/a | static int |
---|
127 | n/a | dbm_ass_sub(dbmobject *dp, PyObject *v, PyObject *w) |
---|
128 | n/a | { |
---|
129 | n/a | datum krec, drec; |
---|
130 | n/a | Py_ssize_t tmp_size; |
---|
131 | n/a | |
---|
132 | n/a | if ( !PyArg_Parse(v, "s#", &krec.dptr, &tmp_size) ) { |
---|
133 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
134 | n/a | "dbm mappings have bytes or string keys only"); |
---|
135 | n/a | return -1; |
---|
136 | n/a | } |
---|
137 | n/a | krec.dsize = tmp_size; |
---|
138 | n/a | if (dp->di_dbm == NULL) { |
---|
139 | n/a | PyErr_SetString(DbmError, "DBM object has already been closed"); |
---|
140 | n/a | return -1; |
---|
141 | n/a | } |
---|
142 | n/a | dp->di_size = -1; |
---|
143 | n/a | if (w == NULL) { |
---|
144 | n/a | if ( dbm_delete(dp->di_dbm, krec) < 0 ) { |
---|
145 | n/a | dbm_clearerr(dp->di_dbm); |
---|
146 | n/a | PyErr_SetObject(PyExc_KeyError, v); |
---|
147 | n/a | return -1; |
---|
148 | n/a | } |
---|
149 | n/a | } else { |
---|
150 | n/a | if ( !PyArg_Parse(w, "s#", &drec.dptr, &tmp_size) ) { |
---|
151 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
152 | n/a | "dbm mappings have byte or string elements only"); |
---|
153 | n/a | return -1; |
---|
154 | n/a | } |
---|
155 | n/a | drec.dsize = tmp_size; |
---|
156 | n/a | if ( dbm_store(dp->di_dbm, krec, drec, DBM_REPLACE) < 0 ) { |
---|
157 | n/a | dbm_clearerr(dp->di_dbm); |
---|
158 | n/a | PyErr_SetString(DbmError, |
---|
159 | n/a | "cannot add item to database"); |
---|
160 | n/a | return -1; |
---|
161 | n/a | } |
---|
162 | n/a | } |
---|
163 | n/a | if ( dbm_error(dp->di_dbm) ) { |
---|
164 | n/a | dbm_clearerr(dp->di_dbm); |
---|
165 | n/a | PyErr_SetString(DbmError, ""); |
---|
166 | n/a | return -1; |
---|
167 | n/a | } |
---|
168 | n/a | return 0; |
---|
169 | n/a | } |
---|
170 | n/a | |
---|
171 | n/a | static PyMappingMethods dbm_as_mapping = { |
---|
172 | n/a | (lenfunc)dbm_length, /*mp_length*/ |
---|
173 | n/a | (binaryfunc)dbm_subscript, /*mp_subscript*/ |
---|
174 | n/a | (objobjargproc)dbm_ass_sub, /*mp_ass_subscript*/ |
---|
175 | n/a | }; |
---|
176 | n/a | |
---|
177 | n/a | /*[clinic input] |
---|
178 | n/a | _dbm.dbm.close |
---|
179 | n/a | |
---|
180 | n/a | Close the database. |
---|
181 | n/a | [clinic start generated code]*/ |
---|
182 | n/a | |
---|
183 | n/a | static PyObject * |
---|
184 | n/a | _dbm_dbm_close_impl(dbmobject *self) |
---|
185 | n/a | /*[clinic end generated code: output=c8dc5b6709600b86 input=046db72377d51be8]*/ |
---|
186 | n/a | { |
---|
187 | n/a | if (self->di_dbm) |
---|
188 | n/a | dbm_close(self->di_dbm); |
---|
189 | n/a | self->di_dbm = NULL; |
---|
190 | n/a | Py_RETURN_NONE; |
---|
191 | n/a | } |
---|
192 | n/a | |
---|
193 | n/a | /*[clinic input] |
---|
194 | n/a | _dbm.dbm.keys |
---|
195 | n/a | |
---|
196 | n/a | Return a list of all keys in the database. |
---|
197 | n/a | [clinic start generated code]*/ |
---|
198 | n/a | |
---|
199 | n/a | static PyObject * |
---|
200 | n/a | _dbm_dbm_keys_impl(dbmobject *self) |
---|
201 | n/a | /*[clinic end generated code: output=434549f7c121b33c input=d210ba778cd9c68a]*/ |
---|
202 | n/a | { |
---|
203 | n/a | PyObject *v, *item; |
---|
204 | n/a | datum key; |
---|
205 | n/a | int err; |
---|
206 | n/a | |
---|
207 | n/a | check_dbmobject_open(self); |
---|
208 | n/a | v = PyList_New(0); |
---|
209 | n/a | if (v == NULL) |
---|
210 | n/a | return NULL; |
---|
211 | n/a | for (key = dbm_firstkey(self->di_dbm); key.dptr; |
---|
212 | n/a | key = dbm_nextkey(self->di_dbm)) { |
---|
213 | n/a | item = PyBytes_FromStringAndSize(key.dptr, key.dsize); |
---|
214 | n/a | if (item == NULL) { |
---|
215 | n/a | Py_DECREF(v); |
---|
216 | n/a | return NULL; |
---|
217 | n/a | } |
---|
218 | n/a | err = PyList_Append(v, item); |
---|
219 | n/a | Py_DECREF(item); |
---|
220 | n/a | if (err != 0) { |
---|
221 | n/a | Py_DECREF(v); |
---|
222 | n/a | return NULL; |
---|
223 | n/a | } |
---|
224 | n/a | } |
---|
225 | n/a | return v; |
---|
226 | n/a | } |
---|
227 | n/a | |
---|
228 | n/a | static int |
---|
229 | n/a | dbm_contains(PyObject *self, PyObject *arg) |
---|
230 | n/a | { |
---|
231 | n/a | dbmobject *dp = (dbmobject *)self; |
---|
232 | n/a | datum key, val; |
---|
233 | n/a | Py_ssize_t size; |
---|
234 | n/a | |
---|
235 | n/a | if ((dp)->di_dbm == NULL) { |
---|
236 | n/a | PyErr_SetString(DbmError, |
---|
237 | n/a | "DBM object has already been closed"); |
---|
238 | n/a | return -1; |
---|
239 | n/a | } |
---|
240 | n/a | if (PyUnicode_Check(arg)) { |
---|
241 | n/a | key.dptr = (char *)PyUnicode_AsUTF8AndSize(arg, &size); |
---|
242 | n/a | key.dsize = size; |
---|
243 | n/a | if (key.dptr == NULL) |
---|
244 | n/a | return -1; |
---|
245 | n/a | } |
---|
246 | n/a | else if (!PyBytes_Check(arg)) { |
---|
247 | n/a | PyErr_Format(PyExc_TypeError, |
---|
248 | n/a | "dbm key must be bytes or string, not %.100s", |
---|
249 | n/a | arg->ob_type->tp_name); |
---|
250 | n/a | return -1; |
---|
251 | n/a | } |
---|
252 | n/a | else { |
---|
253 | n/a | key.dptr = PyBytes_AS_STRING(arg); |
---|
254 | n/a | key.dsize = PyBytes_GET_SIZE(arg); |
---|
255 | n/a | } |
---|
256 | n/a | val = dbm_fetch(dp->di_dbm, key); |
---|
257 | n/a | return val.dptr != NULL; |
---|
258 | n/a | } |
---|
259 | n/a | |
---|
260 | n/a | static PySequenceMethods dbm_as_sequence = { |
---|
261 | n/a | 0, /* sq_length */ |
---|
262 | n/a | 0, /* sq_concat */ |
---|
263 | n/a | 0, /* sq_repeat */ |
---|
264 | n/a | 0, /* sq_item */ |
---|
265 | n/a | 0, /* sq_slice */ |
---|
266 | n/a | 0, /* sq_ass_item */ |
---|
267 | n/a | 0, /* sq_ass_slice */ |
---|
268 | n/a | dbm_contains, /* sq_contains */ |
---|
269 | n/a | 0, /* sq_inplace_concat */ |
---|
270 | n/a | 0, /* sq_inplace_repeat */ |
---|
271 | n/a | }; |
---|
272 | n/a | |
---|
273 | n/a | /*[clinic input] |
---|
274 | n/a | _dbm.dbm.get |
---|
275 | n/a | |
---|
276 | n/a | key: str(accept={str, robuffer}, zeroes=True) |
---|
277 | n/a | default: object(c_default="NULL") = b'' |
---|
278 | n/a | / |
---|
279 | n/a | |
---|
280 | n/a | Return the value for key if present, otherwise default. |
---|
281 | n/a | [clinic start generated code]*/ |
---|
282 | n/a | |
---|
283 | n/a | static PyObject * |
---|
284 | n/a | _dbm_dbm_get_impl(dbmobject *self, const char *key, |
---|
285 | n/a | Py_ssize_clean_t key_length, PyObject *default_value) |
---|
286 | n/a | /*[clinic end generated code: output=b44f95eba8203d93 input=a3a279957f85eb6d]*/ |
---|
287 | n/a | /*[clinic end generated code: output=4f5c0e523eaf1251 input=9402c0af8582dc69]*/ |
---|
288 | n/a | { |
---|
289 | n/a | datum dbm_key, val; |
---|
290 | n/a | |
---|
291 | n/a | dbm_key.dptr = (char *)key; |
---|
292 | n/a | dbm_key.dsize = key_length; |
---|
293 | n/a | check_dbmobject_open(self); |
---|
294 | n/a | val = dbm_fetch(self->di_dbm, dbm_key); |
---|
295 | n/a | if (val.dptr != NULL) |
---|
296 | n/a | return PyBytes_FromStringAndSize(val.dptr, val.dsize); |
---|
297 | n/a | |
---|
298 | n/a | Py_INCREF(default_value); |
---|
299 | n/a | return default_value; |
---|
300 | n/a | } |
---|
301 | n/a | |
---|
302 | n/a | /*[clinic input] |
---|
303 | n/a | _dbm.dbm.setdefault |
---|
304 | n/a | key: str(accept={str, robuffer}, zeroes=True) |
---|
305 | n/a | default: object(c_default="NULL") = b'' |
---|
306 | n/a | / |
---|
307 | n/a | |
---|
308 | n/a | Return the value for key if present, otherwise default. |
---|
309 | n/a | |
---|
310 | n/a | If key is not in the database, it is inserted with default as the value. |
---|
311 | n/a | [clinic start generated code]*/ |
---|
312 | n/a | |
---|
313 | n/a | static PyObject * |
---|
314 | n/a | _dbm_dbm_setdefault_impl(dbmobject *self, const char *key, |
---|
315 | n/a | Py_ssize_clean_t key_length, |
---|
316 | n/a | PyObject *default_value) |
---|
317 | n/a | /*[clinic end generated code: output=52545886cf272161 input=bf40c48edaca01d6]*/ |
---|
318 | n/a | { |
---|
319 | n/a | datum dbm_key, val; |
---|
320 | n/a | Py_ssize_t tmp_size; |
---|
321 | n/a | |
---|
322 | n/a | dbm_key.dptr = (char *)key; |
---|
323 | n/a | dbm_key.dsize = key_length; |
---|
324 | n/a | check_dbmobject_open(self); |
---|
325 | n/a | val = dbm_fetch(self->di_dbm, dbm_key); |
---|
326 | n/a | if (val.dptr != NULL) |
---|
327 | n/a | return PyBytes_FromStringAndSize(val.dptr, val.dsize); |
---|
328 | n/a | if (default_value == NULL) { |
---|
329 | n/a | default_value = PyBytes_FromStringAndSize(NULL, 0); |
---|
330 | n/a | if (default_value == NULL) |
---|
331 | n/a | return NULL; |
---|
332 | n/a | val.dptr = NULL; |
---|
333 | n/a | val.dsize = 0; |
---|
334 | n/a | } |
---|
335 | n/a | else { |
---|
336 | n/a | if ( !PyArg_Parse(default_value, "s#", &val.dptr, &tmp_size) ) { |
---|
337 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
338 | n/a | "dbm mappings have byte string elements only"); |
---|
339 | n/a | return NULL; |
---|
340 | n/a | } |
---|
341 | n/a | val.dsize = tmp_size; |
---|
342 | n/a | Py_INCREF(default_value); |
---|
343 | n/a | } |
---|
344 | n/a | if (dbm_store(self->di_dbm, dbm_key, val, DBM_INSERT) < 0) { |
---|
345 | n/a | dbm_clearerr(self->di_dbm); |
---|
346 | n/a | PyErr_SetString(DbmError, "cannot add item to database"); |
---|
347 | n/a | Py_DECREF(default_value); |
---|
348 | n/a | return NULL; |
---|
349 | n/a | } |
---|
350 | n/a | return default_value; |
---|
351 | n/a | } |
---|
352 | n/a | |
---|
353 | n/a | static PyObject * |
---|
354 | n/a | dbm__enter__(PyObject *self, PyObject *args) |
---|
355 | n/a | { |
---|
356 | n/a | Py_INCREF(self); |
---|
357 | n/a | return self; |
---|
358 | n/a | } |
---|
359 | n/a | |
---|
360 | n/a | static PyObject * |
---|
361 | n/a | dbm__exit__(PyObject *self, PyObject *args) |
---|
362 | n/a | { |
---|
363 | n/a | _Py_IDENTIFIER(close); |
---|
364 | n/a | return _PyObject_CallMethodId(self, &PyId_close, NULL); |
---|
365 | n/a | } |
---|
366 | n/a | |
---|
367 | n/a | |
---|
368 | n/a | static PyMethodDef dbm_methods[] = { |
---|
369 | n/a | _DBM_DBM_CLOSE_METHODDEF |
---|
370 | n/a | _DBM_DBM_KEYS_METHODDEF |
---|
371 | n/a | _DBM_DBM_GET_METHODDEF |
---|
372 | n/a | _DBM_DBM_SETDEFAULT_METHODDEF |
---|
373 | n/a | {"__enter__", dbm__enter__, METH_NOARGS, NULL}, |
---|
374 | n/a | {"__exit__", dbm__exit__, METH_VARARGS, NULL}, |
---|
375 | n/a | {NULL, NULL} /* sentinel */ |
---|
376 | n/a | }; |
---|
377 | n/a | |
---|
378 | n/a | static PyTypeObject Dbmtype = { |
---|
379 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
---|
380 | n/a | "_dbm.dbm", |
---|
381 | n/a | sizeof(dbmobject), |
---|
382 | n/a | 0, |
---|
383 | n/a | (destructor)dbm_dealloc, /*tp_dealloc*/ |
---|
384 | n/a | 0, /*tp_print*/ |
---|
385 | n/a | 0, /*tp_getattr*/ |
---|
386 | n/a | 0, /*tp_setattr*/ |
---|
387 | n/a | 0, /*tp_reserved*/ |
---|
388 | n/a | 0, /*tp_repr*/ |
---|
389 | n/a | 0, /*tp_as_number*/ |
---|
390 | n/a | &dbm_as_sequence, /*tp_as_sequence*/ |
---|
391 | n/a | &dbm_as_mapping, /*tp_as_mapping*/ |
---|
392 | n/a | 0, /*tp_hash*/ |
---|
393 | n/a | 0, /*tp_call*/ |
---|
394 | n/a | 0, /*tp_str*/ |
---|
395 | n/a | 0, /*tp_getattro*/ |
---|
396 | n/a | 0, /*tp_setattro*/ |
---|
397 | n/a | 0, /*tp_as_buffer*/ |
---|
398 | n/a | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
---|
399 | n/a | 0, /*tp_doc*/ |
---|
400 | n/a | 0, /*tp_traverse*/ |
---|
401 | n/a | 0, /*tp_clear*/ |
---|
402 | n/a | 0, /*tp_richcompare*/ |
---|
403 | n/a | 0, /*tp_weaklistoffset*/ |
---|
404 | n/a | 0, /*tp_iter*/ |
---|
405 | n/a | 0, /*tp_iternext*/ |
---|
406 | n/a | dbm_methods, /*tp_methods*/ |
---|
407 | n/a | }; |
---|
408 | n/a | |
---|
409 | n/a | /* ----------------------------------------------------------------- */ |
---|
410 | n/a | |
---|
411 | n/a | /*[clinic input] |
---|
412 | n/a | |
---|
413 | n/a | _dbm.open as dbmopen |
---|
414 | n/a | |
---|
415 | n/a | filename: str |
---|
416 | n/a | The filename to open. |
---|
417 | n/a | |
---|
418 | n/a | flags: str="r" |
---|
419 | n/a | How to open the file. "r" for reading, "w" for writing, etc. |
---|
420 | n/a | |
---|
421 | n/a | mode: int(py_default="0o666") = 0o666 |
---|
422 | n/a | If creating a new file, the mode bits for the new file |
---|
423 | n/a | (e.g. os.O_RDWR). |
---|
424 | n/a | |
---|
425 | n/a | / |
---|
426 | n/a | |
---|
427 | n/a | Return a database object. |
---|
428 | n/a | |
---|
429 | n/a | [clinic start generated code]*/ |
---|
430 | n/a | |
---|
431 | n/a | static PyObject * |
---|
432 | n/a | dbmopen_impl(PyObject *module, const char *filename, const char *flags, |
---|
433 | n/a | int mode) |
---|
434 | n/a | /*[clinic end generated code: output=5fade8cf16e0755f input=226334bade5764e6]*/ |
---|
435 | n/a | { |
---|
436 | n/a | int iflags; |
---|
437 | n/a | |
---|
438 | n/a | if ( strcmp(flags, "r") == 0 ) |
---|
439 | n/a | iflags = O_RDONLY; |
---|
440 | n/a | else if ( strcmp(flags, "w") == 0 ) |
---|
441 | n/a | iflags = O_RDWR; |
---|
442 | n/a | else if ( strcmp(flags, "rw") == 0 ) /* B/W compat */ |
---|
443 | n/a | iflags = O_RDWR|O_CREAT; |
---|
444 | n/a | else if ( strcmp(flags, "c") == 0 ) |
---|
445 | n/a | iflags = O_RDWR|O_CREAT; |
---|
446 | n/a | else if ( strcmp(flags, "n") == 0 ) |
---|
447 | n/a | iflags = O_RDWR|O_CREAT|O_TRUNC; |
---|
448 | n/a | else { |
---|
449 | n/a | PyErr_SetString(DbmError, |
---|
450 | n/a | "arg 2 to open should be 'r', 'w', 'c', or 'n'"); |
---|
451 | n/a | return NULL; |
---|
452 | n/a | } |
---|
453 | n/a | return newdbmobject(filename, iflags, mode); |
---|
454 | n/a | } |
---|
455 | n/a | |
---|
456 | n/a | static PyMethodDef dbmmodule_methods[] = { |
---|
457 | n/a | DBMOPEN_METHODDEF |
---|
458 | n/a | { 0, 0 }, |
---|
459 | n/a | }; |
---|
460 | n/a | |
---|
461 | n/a | |
---|
462 | n/a | static struct PyModuleDef _dbmmodule = { |
---|
463 | n/a | PyModuleDef_HEAD_INIT, |
---|
464 | n/a | "_dbm", |
---|
465 | n/a | NULL, |
---|
466 | n/a | -1, |
---|
467 | n/a | dbmmodule_methods, |
---|
468 | n/a | NULL, |
---|
469 | n/a | NULL, |
---|
470 | n/a | NULL, |
---|
471 | n/a | NULL |
---|
472 | n/a | }; |
---|
473 | n/a | |
---|
474 | n/a | PyMODINIT_FUNC |
---|
475 | n/a | PyInit__dbm(void) { |
---|
476 | n/a | PyObject *m, *d, *s; |
---|
477 | n/a | |
---|
478 | n/a | if (PyType_Ready(&Dbmtype) < 0) |
---|
479 | n/a | return NULL; |
---|
480 | n/a | m = PyModule_Create(&_dbmmodule); |
---|
481 | n/a | if (m == NULL) |
---|
482 | n/a | return NULL; |
---|
483 | n/a | d = PyModule_GetDict(m); |
---|
484 | n/a | if (DbmError == NULL) |
---|
485 | n/a | DbmError = PyErr_NewException("_dbm.error", |
---|
486 | n/a | PyExc_IOError, NULL); |
---|
487 | n/a | s = PyUnicode_FromString(which_dbm); |
---|
488 | n/a | if (s != NULL) { |
---|
489 | n/a | PyDict_SetItemString(d, "library", s); |
---|
490 | n/a | Py_DECREF(s); |
---|
491 | n/a | } |
---|
492 | n/a | if (DbmError != NULL) |
---|
493 | n/a | PyDict_SetItemString(d, "error", DbmError); |
---|
494 | n/a | if (PyErr_Occurred()) { |
---|
495 | n/a | Py_DECREF(m); |
---|
496 | n/a | m = NULL; |
---|
497 | n/a | } |
---|
498 | n/a | return m; |
---|
499 | n/a | } |
---|