blob: 6cf23287c9c79d83ee5a9fafa9639bf3236b4633 [file] [log] [blame]
Guido van Rossumdd9ed831992-06-29 17:10:40 +00001
2/* DBM module using dictionary interface */
3
4
Mark Dickinsonfdaa3a32009-01-16 19:31:13 +00005#define PY_SSIZE_T_CLEAN
Roger E. Masseb0dfe961996-12-10 00:07:00 +00006#include "Python.h"
Guido van Rossumdd9ed831992-06-29 17:10:40 +00007
8#include <sys/types.h>
9#include <sys/stat.h>
10#include <fcntl.h>
Fred Draked94f7072000-09-14 15:48:06 +000011
12/* Some Linux systems install gdbm/ndbm.h, but not ndbm.h. This supports
13 * whichever configure was able to locate.
14 */
15#if defined(HAVE_NDBM_H)
Guido van Rossumdd9ed831992-06-29 17:10:40 +000016#include <ndbm.h>
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000017static char *which_dbm = "GNU gdbm"; /* EMX port of GDBM */
Fred Draked94f7072000-09-14 15:48:06 +000018#elif defined(HAVE_GDBM_NDBM_H)
19#include <gdbm/ndbm.h>
Fred Drake1ac526d2000-09-15 21:35:14 +000020static char *which_dbm = "GNU gdbm";
Skip Montanaroc1ce2862008-12-06 17:25:02 +000021#elif defined(HAVE_GDBM_DASH_NDBM_H)
22#include <gdbm-ndbm.h>
23static char *which_dbm = "GNU gdbm";
Skip Montanaro71ffc5c2002-08-02 17:13:01 +000024#elif defined(HAVE_BERKDB_H)
25#include <db.h>
26static char *which_dbm = "Berkeley DB";
Fred Draked94f7072000-09-14 15:48:06 +000027#else
28#error "No ndbm.h available!"
29#endif
Guido van Rossumdd9ed831992-06-29 17:10:40 +000030
Larry Hastings61272b72014-01-07 12:41:53 -080031/*[clinic input]
Larry Hastings44e2eaa2013-11-23 15:37:55 -080032module dbm
33class dbm.dbm
Larry Hastings61272b72014-01-07 12:41:53 -080034[clinic start generated code]*/
35/*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/
Larry Hastings44e2eaa2013-11-23 15:37:55 -080036
Guido van Rossumdd9ed831992-06-29 17:10:40 +000037typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000038 PyObject_HEAD
39 int di_size; /* -1 means recompute */
40 DBM *di_dbm;
Guido van Rossumdd9ed831992-06-29 17:10:40 +000041} dbmobject;
42
Jeremy Hylton938ace62002-07-17 16:30:39 +000043static PyTypeObject Dbmtype;
Guido van Rossumdd9ed831992-06-29 17:10:40 +000044
Christian Heimes90aa7642007-12-19 02:45:37 +000045#define is_dbmobject(v) (Py_TYPE(v) == &Dbmtype)
Guido van Rossum77eecfa1997-07-17 22:56:01 +000046#define check_dbmobject_open(v) if ((v)->di_dbm == NULL) \
47 { PyErr_SetString(DbmError, "DBM object has already been closed"); \
48 return NULL; }
Guido van Rossumdd9ed831992-06-29 17:10:40 +000049
Roger E. Masseb0dfe961996-12-10 00:07:00 +000050static PyObject *DbmError;
Guido van Rossumdd9ed831992-06-29 17:10:40 +000051
Larry Hastings61272b72014-01-07 12:41:53 -080052/*[python input]
Larry Hastingsebdcb502013-11-23 14:54:00 -080053class dbmobject_converter(self_converter):
54 type = "dbmobject *"
55 def converter_init(self):
56 self.name = 'dp'
Larry Hastings61272b72014-01-07 12:41:53 -080057[python start generated code]*/
58/*[python end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/
Larry Hastingsebdcb502013-11-23 14:54:00 -080059
Roger E. Masseb0dfe961996-12-10 00:07:00 +000060static PyObject *
Larry Hastings31826802013-10-19 00:09:25 -070061newdbmobject(const char *file, int flags, int mode)
Guido van Rossumdd9ed831992-06-29 17:10:40 +000062{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000063 dbmobject *dp;
Guido van Rossumdd9ed831992-06-29 17:10:40 +000064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 dp = PyObject_New(dbmobject, &Dbmtype);
66 if (dp == NULL)
67 return NULL;
68 dp->di_size = -1;
Christian Heimes8ff6f3e2013-12-06 00:20:00 +010069 /* See issue #19296 */
70 if ( (dp->di_dbm = dbm_open((char *)file, flags, mode)) == 0 ) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 PyErr_SetFromErrno(DbmError);
72 Py_DECREF(dp);
73 return NULL;
74 }
75 return (PyObject *)dp;
Guido van Rossumdd9ed831992-06-29 17:10:40 +000076}
77
78/* Methods */
79
80static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020081dbm_dealloc(dbmobject *dp)
Guido van Rossumdd9ed831992-06-29 17:10:40 +000082{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000083 if ( dp->di_dbm )
84 dbm_close(dp->di_dbm);
85 PyObject_Del(dp);
Guido van Rossumdd9ed831992-06-29 17:10:40 +000086}
87
Martin v. Löwis18e16552006-02-15 17:27:45 +000088static Py_ssize_t
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +000089dbm_length(dbmobject *dp)
Guido van Rossumdd9ed831992-06-29 17:10:40 +000090{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000091 if (dp->di_dbm == NULL) {
92 PyErr_SetString(DbmError, "DBM object has already been closed");
93 return -1;
94 }
95 if ( dp->di_size < 0 ) {
96 datum key;
97 int size;
Guido van Rossumdd9ed831992-06-29 17:10:40 +000098
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 size = 0;
100 for ( key=dbm_firstkey(dp->di_dbm); key.dptr;
101 key = dbm_nextkey(dp->di_dbm))
102 size++;
103 dp->di_size = size;
104 }
105 return dp->di_size;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000106}
107
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000108static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200109dbm_subscript(dbmobject *dp, PyObject *key)
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000110{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 datum drec, krec;
112 Py_ssize_t tmp_size;
113
114 if (!PyArg_Parse(key, "s#", &krec.dptr, &tmp_size) )
115 return NULL;
116
117 krec.dsize = tmp_size;
118 check_dbmobject_open(dp);
119 drec = dbm_fetch(dp->di_dbm, krec);
120 if ( drec.dptr == 0 ) {
121 PyErr_SetObject(PyExc_KeyError, key);
122 return NULL;
123 }
124 if ( dbm_error(dp->di_dbm) ) {
125 dbm_clearerr(dp->di_dbm);
126 PyErr_SetString(DbmError, "");
127 return NULL;
128 }
129 return PyBytes_FromStringAndSize(drec.dptr, drec.dsize);
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000130}
131
132static int
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +0000133dbm_ass_sub(dbmobject *dp, PyObject *v, PyObject *w)
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000134{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135 datum krec, drec;
136 Py_ssize_t tmp_size;
137
138 if ( !PyArg_Parse(v, "s#", &krec.dptr, &tmp_size) ) {
139 PyErr_SetString(PyExc_TypeError,
140 "dbm mappings have bytes or string keys only");
141 return -1;
142 }
143 krec.dsize = tmp_size;
144 if (dp->di_dbm == NULL) {
145 PyErr_SetString(DbmError, "DBM object has already been closed");
146 return -1;
147 }
148 dp->di_size = -1;
149 if (w == NULL) {
150 if ( dbm_delete(dp->di_dbm, krec) < 0 ) {
151 dbm_clearerr(dp->di_dbm);
152 PyErr_SetObject(PyExc_KeyError, v);
153 return -1;
Guido van Rossum77eecfa1997-07-17 22:56:01 +0000154 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000155 } else {
156 if ( !PyArg_Parse(w, "s#", &drec.dptr, &tmp_size) ) {
157 PyErr_SetString(PyExc_TypeError,
158 "dbm mappings have byte or string elements only");
159 return -1;
160 }
161 drec.dsize = tmp_size;
162 if ( dbm_store(dp->di_dbm, krec, drec, DBM_REPLACE) < 0 ) {
163 dbm_clearerr(dp->di_dbm);
164 PyErr_SetString(DbmError,
165 "cannot add item to database");
166 return -1;
167 }
168 }
169 if ( dbm_error(dp->di_dbm) ) {
170 dbm_clearerr(dp->di_dbm);
171 PyErr_SetString(DbmError, "");
172 return -1;
173 }
174 return 0;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000175}
176
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000177static PyMappingMethods dbm_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000178 (lenfunc)dbm_length, /*mp_length*/
179 (binaryfunc)dbm_subscript, /*mp_subscript*/
180 (objobjargproc)dbm_ass_sub, /*mp_ass_subscript*/
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000181};
182
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000183static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200184dbm__close(dbmobject *dp, PyObject *unused)
Guido van Rossum807b7be1995-07-07 22:37:11 +0000185{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000186 if (dp->di_dbm)
187 dbm_close(dp->di_dbm);
188 dp->di_dbm = NULL;
189 Py_INCREF(Py_None);
190 return Py_None;
Guido van Rossum807b7be1995-07-07 22:37:11 +0000191}
192
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000193static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200194dbm_keys(dbmobject *dp, PyObject *unused)
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000195{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200196 PyObject *v, *item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 datum key;
198 int err;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000199
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 check_dbmobject_open(dp);
201 v = PyList_New(0);
202 if (v == NULL)
203 return NULL;
204 for (key = dbm_firstkey(dp->di_dbm); key.dptr;
205 key = dbm_nextkey(dp->di_dbm)) {
206 item = PyBytes_FromStringAndSize(key.dptr, key.dsize);
207 if (item == NULL) {
208 Py_DECREF(v);
209 return NULL;
210 }
211 err = PyList_Append(v, item);
212 Py_DECREF(item);
213 if (err != 0) {
214 Py_DECREF(v);
215 return NULL;
216 }
217 }
218 return v;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000219}
220
Guido van Rossumef671112006-08-19 23:11:47 +0000221static int
222dbm_contains(PyObject *self, PyObject *arg)
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000223{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 dbmobject *dp = (dbmobject *)self;
225 datum key, val;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200226 Py_ssize_t size;
Guido van Rossumef671112006-08-19 23:11:47 +0000227
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 if ((dp)->di_dbm == NULL) {
229 PyErr_SetString(DbmError,
230 "DBM object has already been closed");
231 return -1;
232 }
233 if (PyUnicode_Check(arg)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200234 key.dptr = PyUnicode_AsUTF8AndSize(arg, &size);
235 key.dsize = size;
236 if (key.dptr == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 return -1;
238 }
Serhiy Storchaka9da33ab2013-10-24 23:59:28 +0300239 else if (!PyBytes_Check(arg)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000240 PyErr_Format(PyExc_TypeError,
Serhiy Storchaka9da33ab2013-10-24 23:59:28 +0300241 "dbm key must be bytes or string, not %.100s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242 arg->ob_type->tp_name);
243 return -1;
244 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200245 else {
246 key.dptr = PyBytes_AS_STRING(arg);
247 key.dsize = PyBytes_GET_SIZE(arg);
248 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000249 val = dbm_fetch(dp->di_dbm, key);
250 return val.dptr != NULL;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000251}
252
Guido van Rossumef671112006-08-19 23:11:47 +0000253static PySequenceMethods dbm_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 0, /* sq_length */
255 0, /* sq_concat */
256 0, /* sq_repeat */
257 0, /* sq_item */
258 0, /* sq_slice */
259 0, /* sq_ass_item */
260 0, /* sq_ass_slice */
261 dbm_contains, /* sq_contains */
262 0, /* sq_inplace_concat */
263 0, /* sq_inplace_repeat */
Guido van Rossumef671112006-08-19 23:11:47 +0000264};
265
Larry Hastings61272b72014-01-07 12:41:53 -0800266/*[clinic input]
Fred Drake1ac526d2000-09-15 21:35:14 +0000267
Larry Hastingsebdcb502013-11-23 14:54:00 -0800268dbm.dbm.get
269
270 self: dbmobject
271
272 key: str(length=True)
273 [
274 default: object
275 ]
276 /
277
278Return the value for key if present, otherwise default.
Larry Hastings61272b72014-01-07 12:41:53 -0800279[clinic start generated code]*/
Larry Hastingsebdcb502013-11-23 14:54:00 -0800280
281PyDoc_STRVAR(dbm_dbm_get__doc__,
Larry Hastings5c661892014-01-24 06:17:25 -0800282"get(self, key, [default])\n"
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800283"Return the value for key if present, otherwise default.");
Larry Hastingsebdcb502013-11-23 14:54:00 -0800284
285#define DBM_DBM_GET_METHODDEF \
286 {"get", (PyCFunction)dbm_dbm_get, METH_VARARGS, dbm_dbm_get__doc__},
287
288static PyObject *
289dbm_dbm_get_impl(dbmobject *dp, const char *key, Py_ssize_clean_t key_length, int group_right_1, PyObject *default_value);
290
291static PyObject *
Larry Hastings5c661892014-01-24 06:17:25 -0800292dbm_dbm_get(dbmobject *dp, PyObject *args)
Larry Hastingsebdcb502013-11-23 14:54:00 -0800293{
294 PyObject *return_value = NULL;
295 const char *key;
296 Py_ssize_clean_t key_length;
297 int group_right_1 = 0;
298 PyObject *default_value = NULL;
299
Larry Hastings2a727912014-01-16 11:32:01 -0800300 switch (PyTuple_GET_SIZE(args)) {
Larry Hastingsebdcb502013-11-23 14:54:00 -0800301 case 1:
302 if (!PyArg_ParseTuple(args, "s#:get", &key, &key_length))
Larry Hastings46258262014-01-22 03:05:49 -0800303 goto exit;
Larry Hastingsebdcb502013-11-23 14:54:00 -0800304 break;
305 case 2:
306 if (!PyArg_ParseTuple(args, "s#O:get", &key, &key_length, &default_value))
Larry Hastings46258262014-01-22 03:05:49 -0800307 goto exit;
Larry Hastingsebdcb502013-11-23 14:54:00 -0800308 group_right_1 = 1;
309 break;
310 default:
311 PyErr_SetString(PyExc_TypeError, "dbm.dbm.get requires 1 to 2 arguments");
Larry Hastings46258262014-01-22 03:05:49 -0800312 goto exit;
Larry Hastingsebdcb502013-11-23 14:54:00 -0800313 }
Larry Hastings5c661892014-01-24 06:17:25 -0800314 return_value = dbm_dbm_get_impl(dp, key, key_length, group_right_1, default_value);
Larry Hastingsebdcb502013-11-23 14:54:00 -0800315
Larry Hastings46258262014-01-22 03:05:49 -0800316exit:
Larry Hastingsebdcb502013-11-23 14:54:00 -0800317 return return_value;
318}
319
320static PyObject *
321dbm_dbm_get_impl(dbmobject *dp, const char *key, Py_ssize_clean_t key_length, int group_right_1, PyObject *default_value)
Larry Hastings5c661892014-01-24 06:17:25 -0800322/*[clinic end generated code: checksum=31d5180d6b36f1eafea78ec4391adf3559916379]*/
Larry Hastingsebdcb502013-11-23 14:54:00 -0800323{
324 datum dbm_key, val;
325
326 if (!group_right_1)
327 default_value = Py_None;
328 dbm_key.dptr = (char *)key;
329 dbm_key.dsize = key_length;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 check_dbmobject_open(dp);
Larry Hastingsebdcb502013-11-23 14:54:00 -0800331 val = dbm_fetch(dp->di_dbm, dbm_key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000332 if (val.dptr != NULL)
333 return PyBytes_FromStringAndSize(val.dptr, val.dsize);
Larry Hastingsebdcb502013-11-23 14:54:00 -0800334
335 Py_INCREF(default_value);
336 return default_value;
Fred Drake1ac526d2000-09-15 21:35:14 +0000337}
338
339static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200340dbm_setdefault(dbmobject *dp, PyObject *args)
Fred Drake1ac526d2000-09-15 21:35:14 +0000341{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 datum key, val;
343 PyObject *defvalue = NULL;
344 char *tmp_ptr;
345 Py_ssize_t tmp_size;
Fred Drake1ac526d2000-09-15 21:35:14 +0000346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 if (!PyArg_ParseTuple(args, "s#|O:setdefault",
348 &tmp_ptr, &tmp_size, &defvalue))
349 return NULL;
350 key.dptr = tmp_ptr;
351 key.dsize = tmp_size;
352 check_dbmobject_open(dp);
353 val = dbm_fetch(dp->di_dbm, key);
354 if (val.dptr != NULL)
355 return PyBytes_FromStringAndSize(val.dptr, val.dsize);
356 if (defvalue == NULL) {
357 defvalue = PyBytes_FromStringAndSize(NULL, 0);
358 if (defvalue == NULL)
359 return NULL;
360 val.dptr = NULL;
361 val.dsize = 0;
362 }
363 else {
364 if ( !PyArg_Parse(defvalue, "s#", &val.dptr, &tmp_size) ) {
365 PyErr_SetString(PyExc_TypeError,
366 "dbm mappings have byte string elements only");
367 return NULL;
368 }
369 val.dsize = tmp_size;
370 Py_INCREF(defvalue);
371 }
372 if (dbm_store(dp->di_dbm, key, val, DBM_INSERT) < 0) {
373 dbm_clearerr(dp->di_dbm);
374 PyErr_SetString(DbmError, "cannot add item to database");
375 Py_DECREF(defvalue);
376 return NULL;
377 }
378 return defvalue;
Fred Drake1ac526d2000-09-15 21:35:14 +0000379}
380
Nick Coghlanc610aba2013-11-17 15:59:51 +1000381static PyObject *
382dbm__enter__(PyObject *self, PyObject *args)
383{
384 Py_INCREF(self);
385 return self;
386}
387
388static PyObject *
389dbm__exit__(PyObject *self, PyObject *args)
390{
391 _Py_IDENTIFIER(close);
392 return _PyObject_CallMethodId(self, &PyId_close, NULL);
393}
394
395
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000396static PyMethodDef dbm_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 {"close", (PyCFunction)dbm__close, METH_NOARGS,
398 "close()\nClose the database."},
399 {"keys", (PyCFunction)dbm_keys, METH_NOARGS,
400 "keys() -> list\nReturn a list of all keys in the database."},
Larry Hastingsebdcb502013-11-23 14:54:00 -0800401 DBM_DBM_GET_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 {"setdefault", (PyCFunction)dbm_setdefault, METH_VARARGS,
403 "setdefault(key[, default]) -> value\n"
404 "Return the value for key if present, otherwise default. If key\n"
405 "is not in the database, it is inserted with default as the value."},
Nick Coghlanc610aba2013-11-17 15:59:51 +1000406 {"__enter__", dbm__enter__, METH_NOARGS, NULL},
407 {"__exit__", dbm__exit__, METH_VARARGS, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 {NULL, NULL} /* sentinel */
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000409};
410
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000411static PyTypeObject Dbmtype = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 PyVarObject_HEAD_INIT(NULL, 0)
413 "_dbm.dbm",
414 sizeof(dbmobject),
415 0,
416 (destructor)dbm_dealloc, /*tp_dealloc*/
417 0, /*tp_print*/
418 0, /*tp_getattr*/
419 0, /*tp_setattr*/
420 0, /*tp_reserved*/
421 0, /*tp_repr*/
422 0, /*tp_as_number*/
423 &dbm_as_sequence, /*tp_as_sequence*/
424 &dbm_as_mapping, /*tp_as_mapping*/
425 0, /*tp_hash*/
426 0, /*tp_call*/
427 0, /*tp_str*/
428 0, /*tp_getattro*/
429 0, /*tp_setattro*/
430 0, /*tp_as_buffer*/
431 Py_TPFLAGS_DEFAULT, /*tp_flags*/
432 0, /*tp_doc*/
433 0, /*tp_traverse*/
434 0, /*tp_clear*/
435 0, /*tp_richcompare*/
436 0, /*tp_weaklistoffset*/
437 0, /*tp_iter*/
438 0, /*tp_iternext*/
439 dbm_methods, /*tp_methods*/
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000440};
441
442/* ----------------------------------------------------------------- */
443
Larry Hastings61272b72014-01-07 12:41:53 -0800444/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -0700445
446dbm.open as dbmopen
447
448 filename: str
449 The filename to open.
450
451 flags: str="r"
452 How to open the file. "r" for reading, "w" for writing, etc.
453
Larry Hastings2a727912014-01-16 11:32:01 -0800454 mode: int(py_default="0o666") = 0o666
Larry Hastings31826802013-10-19 00:09:25 -0700455 If creating a new file, the mode bits for the new file
456 (e.g. os.O_RDWR).
457
458 /
459
460Return a database object.
461
Larry Hastings61272b72014-01-07 12:41:53 -0800462[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -0700463
464PyDoc_STRVAR(dbmopen__doc__,
Larry Hastings5c661892014-01-24 06:17:25 -0800465"open(module, filename, flags=\'r\', mode=0o666)\n"
Larry Hastings31826802013-10-19 00:09:25 -0700466"Return a database object.\n"
467"\n"
Larry Hastings31826802013-10-19 00:09:25 -0700468" filename\n"
469" The filename to open.\n"
470" flags\n"
471" How to open the file. \"r\" for reading, \"w\" for writing, etc.\n"
472" mode\n"
473" If creating a new file, the mode bits for the new file\n"
474" (e.g. os.O_RDWR).");
475
476#define DBMOPEN_METHODDEF \
477 {"open", (PyCFunction)dbmopen, METH_VARARGS, dbmopen__doc__},
478
479static PyObject *
Larry Hastingsebdcb502013-11-23 14:54:00 -0800480dbmopen_impl(PyModuleDef *module, const char *filename, const char *flags, int mode);
Larry Hastings31826802013-10-19 00:09:25 -0700481
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000482static PyObject *
Larry Hastingsebdcb502013-11-23 14:54:00 -0800483dbmopen(PyModuleDef *module, PyObject *args)
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000484{
Larry Hastings31826802013-10-19 00:09:25 -0700485 PyObject *return_value = NULL;
486 const char *filename;
487 const char *flags = "r";
488 int mode = 438;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000489
Larry Hastings31826802013-10-19 00:09:25 -0700490 if (!PyArg_ParseTuple(args,
491 "s|si:open",
492 &filename, &flags, &mode))
493 goto exit;
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800494 return_value = dbmopen_impl(module, filename, flags, mode);
Larry Hastings31826802013-10-19 00:09:25 -0700495
496exit:
497 return return_value;
498}
499
500static PyObject *
Larry Hastingsebdcb502013-11-23 14:54:00 -0800501dbmopen_impl(PyModuleDef *module, const char *filename, const char *flags, int mode)
Larry Hastings5c661892014-01-24 06:17:25 -0800502/*[clinic end generated code: checksum=9efae7d3c3b67a365011bf4e463e918901ba6c79]*/
Larry Hastings31826802013-10-19 00:09:25 -0700503{
504 int iflags;
505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 if ( strcmp(flags, "r") == 0 )
507 iflags = O_RDONLY;
508 else if ( strcmp(flags, "w") == 0 )
509 iflags = O_RDWR;
510 else if ( strcmp(flags, "rw") == 0 ) /* B/W compat */
511 iflags = O_RDWR|O_CREAT;
512 else if ( strcmp(flags, "c") == 0 )
513 iflags = O_RDWR|O_CREAT;
514 else if ( strcmp(flags, "n") == 0 )
515 iflags = O_RDWR|O_CREAT|O_TRUNC;
516 else {
517 PyErr_SetString(DbmError,
518 "arg 2 to open should be 'r', 'w', 'c', or 'n'");
519 return NULL;
520 }
Larry Hastings31826802013-10-19 00:09:25 -0700521 return newdbmobject(filename, iflags, mode);
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000522}
523
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000524static PyMethodDef dbmmodule_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -0700525 DBMOPEN_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 { 0, 0 },
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000527};
528
Martin v. Löwis1a214512008-06-11 05:26:20 +0000529
530static struct PyModuleDef _dbmmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 PyModuleDef_HEAD_INIT,
532 "_dbm",
533 NULL,
534 -1,
535 dbmmodule_methods,
536 NULL,
537 NULL,
538 NULL,
539 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000540};
541
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000542PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000543PyInit__dbm(void) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 PyObject *m, *d, *s;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000545
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 if (PyType_Ready(&Dbmtype) < 0)
547 return NULL;
548 m = PyModule_Create(&_dbmmodule);
549 if (m == NULL)
550 return NULL;
551 d = PyModule_GetDict(m);
552 if (DbmError == NULL)
553 DbmError = PyErr_NewException("_dbm.error",
554 PyExc_IOError, NULL);
555 s = PyUnicode_FromString(which_dbm);
556 if (s != NULL) {
557 PyDict_SetItemString(d, "library", s);
558 Py_DECREF(s);
559 }
560 if (DbmError != NULL)
561 PyDict_SetItemString(d, "error", DbmError);
562 if (PyErr_Occurred()) {
563 Py_DECREF(m);
564 m = NULL;
565 }
566 return m;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000567}