blob: 93ea4161a7f48b45bc5a8c070ff7fe3b0ed91b3d [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
Larry Hastingsc2047262014-01-25 20:43:29 -080033class dbm.dbm "dbmobject *" "&Dbmtype"
Larry Hastings61272b72014-01-07 12:41:53 -080034[clinic start generated code]*/
Larry Hastings581ee362014-01-28 05:00:08 -080035/*[clinic end generated code: output=da39a3ee5e6b4b0d input=92450564684a69a3]*/
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 *"
Larry Hastings7726ac92014-01-31 22:03:12 -080055 def pre_render(self):
56 super().pre_render()
Larry Hastingsebdcb502013-11-23 14:54:00 -080057 self.name = 'dp'
Larry Hastings61272b72014-01-07 12:41:53 -080058[python start generated code]*/
Larry Hastings7726ac92014-01-31 22:03:12 -080059/*[python end generated code: output=da39a3ee5e6b4b0d input=6ad536357913879a]*/
Larry Hastingsebdcb502013-11-23 14:54:00 -080060
Roger E. Masseb0dfe961996-12-10 00:07:00 +000061static PyObject *
Larry Hastings31826802013-10-19 00:09:25 -070062newdbmobject(const char *file, int flags, int mode)
Guido van Rossumdd9ed831992-06-29 17:10:40 +000063{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000064 dbmobject *dp;
Guido van Rossumdd9ed831992-06-29 17:10:40 +000065
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000066 dp = PyObject_New(dbmobject, &Dbmtype);
67 if (dp == NULL)
68 return NULL;
69 dp->di_size = -1;
Christian Heimes8ff6f3e2013-12-06 00:20:00 +010070 /* See issue #19296 */
71 if ( (dp->di_dbm = dbm_open((char *)file, flags, mode)) == 0 ) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000072 PyErr_SetFromErrno(DbmError);
73 Py_DECREF(dp);
74 return NULL;
75 }
76 return (PyObject *)dp;
Guido van Rossumdd9ed831992-06-29 17:10:40 +000077}
78
79/* Methods */
80
81static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020082dbm_dealloc(dbmobject *dp)
Guido van Rossumdd9ed831992-06-29 17:10:40 +000083{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 if ( dp->di_dbm )
85 dbm_close(dp->di_dbm);
86 PyObject_Del(dp);
Guido van Rossumdd9ed831992-06-29 17:10:40 +000087}
88
Martin v. Löwis18e16552006-02-15 17:27:45 +000089static Py_ssize_t
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +000090dbm_length(dbmobject *dp)
Guido van Rossumdd9ed831992-06-29 17:10:40 +000091{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 if (dp->di_dbm == NULL) {
93 PyErr_SetString(DbmError, "DBM object has already been closed");
94 return -1;
95 }
96 if ( dp->di_size < 0 ) {
97 datum key;
98 int size;
Guido van Rossumdd9ed831992-06-29 17:10:40 +000099
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 size = 0;
101 for ( key=dbm_firstkey(dp->di_dbm); key.dptr;
102 key = dbm_nextkey(dp->di_dbm))
103 size++;
104 dp->di_size = size;
105 }
106 return dp->di_size;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000107}
108
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000109static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200110dbm_subscript(dbmobject *dp, PyObject *key)
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000111{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000112 datum drec, krec;
113 Py_ssize_t tmp_size;
114
115 if (!PyArg_Parse(key, "s#", &krec.dptr, &tmp_size) )
116 return NULL;
117
118 krec.dsize = tmp_size;
119 check_dbmobject_open(dp);
120 drec = dbm_fetch(dp->di_dbm, krec);
121 if ( drec.dptr == 0 ) {
122 PyErr_SetObject(PyExc_KeyError, key);
123 return NULL;
124 }
125 if ( dbm_error(dp->di_dbm) ) {
126 dbm_clearerr(dp->di_dbm);
127 PyErr_SetString(DbmError, "");
128 return NULL;
129 }
130 return PyBytes_FromStringAndSize(drec.dptr, drec.dsize);
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000131}
132
133static int
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +0000134dbm_ass_sub(dbmobject *dp, PyObject *v, PyObject *w)
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000135{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000136 datum krec, drec;
137 Py_ssize_t tmp_size;
138
139 if ( !PyArg_Parse(v, "s#", &krec.dptr, &tmp_size) ) {
140 PyErr_SetString(PyExc_TypeError,
141 "dbm mappings have bytes or string keys only");
142 return -1;
143 }
144 krec.dsize = tmp_size;
145 if (dp->di_dbm == NULL) {
146 PyErr_SetString(DbmError, "DBM object has already been closed");
147 return -1;
148 }
149 dp->di_size = -1;
150 if (w == NULL) {
151 if ( dbm_delete(dp->di_dbm, krec) < 0 ) {
152 dbm_clearerr(dp->di_dbm);
153 PyErr_SetObject(PyExc_KeyError, v);
154 return -1;
Guido van Rossum77eecfa1997-07-17 22:56:01 +0000155 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 } else {
157 if ( !PyArg_Parse(w, "s#", &drec.dptr, &tmp_size) ) {
158 PyErr_SetString(PyExc_TypeError,
159 "dbm mappings have byte or string elements only");
160 return -1;
161 }
162 drec.dsize = tmp_size;
163 if ( dbm_store(dp->di_dbm, krec, drec, DBM_REPLACE) < 0 ) {
164 dbm_clearerr(dp->di_dbm);
165 PyErr_SetString(DbmError,
166 "cannot add item to database");
167 return -1;
168 }
169 }
170 if ( dbm_error(dp->di_dbm) ) {
171 dbm_clearerr(dp->di_dbm);
172 PyErr_SetString(DbmError, "");
173 return -1;
174 }
175 return 0;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000176}
177
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000178static PyMappingMethods dbm_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000179 (lenfunc)dbm_length, /*mp_length*/
180 (binaryfunc)dbm_subscript, /*mp_subscript*/
181 (objobjargproc)dbm_ass_sub, /*mp_ass_subscript*/
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000182};
183
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000184static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200185dbm__close(dbmobject *dp, PyObject *unused)
Guido van Rossum807b7be1995-07-07 22:37:11 +0000186{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000187 if (dp->di_dbm)
188 dbm_close(dp->di_dbm);
189 dp->di_dbm = NULL;
190 Py_INCREF(Py_None);
191 return Py_None;
Guido van Rossum807b7be1995-07-07 22:37:11 +0000192}
193
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000194static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200195dbm_keys(dbmobject *dp, PyObject *unused)
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000196{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200197 PyObject *v, *item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 datum key;
199 int err;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000200
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 check_dbmobject_open(dp);
202 v = PyList_New(0);
203 if (v == NULL)
204 return NULL;
205 for (key = dbm_firstkey(dp->di_dbm); key.dptr;
206 key = dbm_nextkey(dp->di_dbm)) {
207 item = PyBytes_FromStringAndSize(key.dptr, key.dsize);
208 if (item == NULL) {
209 Py_DECREF(v);
210 return NULL;
211 }
212 err = PyList_Append(v, item);
213 Py_DECREF(item);
214 if (err != 0) {
215 Py_DECREF(v);
216 return NULL;
217 }
218 }
219 return v;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000220}
221
Guido van Rossumef671112006-08-19 23:11:47 +0000222static int
223dbm_contains(PyObject *self, PyObject *arg)
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000224{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000225 dbmobject *dp = (dbmobject *)self;
226 datum key, val;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200227 Py_ssize_t size;
Guido van Rossumef671112006-08-19 23:11:47 +0000228
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 if ((dp)->di_dbm == NULL) {
230 PyErr_SetString(DbmError,
231 "DBM object has already been closed");
232 return -1;
233 }
234 if (PyUnicode_Check(arg)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200235 key.dptr = PyUnicode_AsUTF8AndSize(arg, &size);
236 key.dsize = size;
237 if (key.dptr == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 return -1;
239 }
Serhiy Storchaka9da33ab2013-10-24 23:59:28 +0300240 else if (!PyBytes_Check(arg)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000241 PyErr_Format(PyExc_TypeError,
Serhiy Storchaka9da33ab2013-10-24 23:59:28 +0300242 "dbm key must be bytes or string, not %.100s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000243 arg->ob_type->tp_name);
244 return -1;
245 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200246 else {
247 key.dptr = PyBytes_AS_STRING(arg);
248 key.dsize = PyBytes_GET_SIZE(arg);
249 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 val = dbm_fetch(dp->di_dbm, key);
251 return val.dptr != NULL;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000252}
253
Guido van Rossumef671112006-08-19 23:11:47 +0000254static PySequenceMethods dbm_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000255 0, /* sq_length */
256 0, /* sq_concat */
257 0, /* sq_repeat */
258 0, /* sq_item */
259 0, /* sq_slice */
260 0, /* sq_ass_item */
261 0, /* sq_ass_slice */
262 dbm_contains, /* sq_contains */
263 0, /* sq_inplace_concat */
264 0, /* sq_inplace_repeat */
Guido van Rossumef671112006-08-19 23:11:47 +0000265};
266
Larry Hastings61272b72014-01-07 12:41:53 -0800267/*[clinic input]
Fred Drake1ac526d2000-09-15 21:35:14 +0000268
Larry Hastingsebdcb502013-11-23 14:54:00 -0800269dbm.dbm.get
270
271 self: dbmobject
272
273 key: str(length=True)
Larry Hastings7726ac92014-01-31 22:03:12 -0800274 default: object = None
Larry Hastingsebdcb502013-11-23 14:54:00 -0800275 /
276
277Return the value for key if present, otherwise default.
Larry Hastings61272b72014-01-07 12:41:53 -0800278[clinic start generated code]*/
Larry Hastingsebdcb502013-11-23 14:54:00 -0800279
280PyDoc_STRVAR(dbm_dbm_get__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800281"get($self, key, default=None, /)\n"
282"--\n"
283"\n"
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800284"Return the value for key if present, otherwise default.");
Larry Hastingsebdcb502013-11-23 14:54:00 -0800285
286#define DBM_DBM_GET_METHODDEF \
287 {"get", (PyCFunction)dbm_dbm_get, METH_VARARGS, dbm_dbm_get__doc__},
288
289static PyObject *
Larry Hastings7726ac92014-01-31 22:03:12 -0800290dbm_dbm_get_impl(dbmobject *dp, const char *key, Py_ssize_clean_t key_length, PyObject *default_value);
Larry Hastingsebdcb502013-11-23 14:54:00 -0800291
292static PyObject *
Larry Hastings5c661892014-01-24 06:17:25 -0800293dbm_dbm_get(dbmobject *dp, PyObject *args)
Larry Hastingsebdcb502013-11-23 14:54:00 -0800294{
295 PyObject *return_value = NULL;
296 const char *key;
297 Py_ssize_clean_t key_length;
Larry Hastings7726ac92014-01-31 22:03:12 -0800298 PyObject *default_value = Py_None;
Larry Hastingsebdcb502013-11-23 14:54:00 -0800299
Larry Hastings7726ac92014-01-31 22:03:12 -0800300 if (!PyArg_ParseTuple(args,
301 "s#|O:get",
302 &key, &key_length, &default_value))
303 goto exit;
304 return_value = dbm_dbm_get_impl(dp, key, key_length, default_value);
Larry Hastingsebdcb502013-11-23 14:54:00 -0800305
Larry Hastings46258262014-01-22 03:05:49 -0800306exit:
Larry Hastingsebdcb502013-11-23 14:54:00 -0800307 return return_value;
308}
309
310static PyObject *
Larry Hastings7726ac92014-01-31 22:03:12 -0800311dbm_dbm_get_impl(dbmobject *dp, const char *key, Py_ssize_clean_t key_length, PyObject *default_value)
Larry Hastings2623c8c2014-02-08 22:15:29 -0800312/*[clinic end generated code: output=452ea11394e7e92d input=aecf5efd2f2b1a3b]*/
Larry Hastingsebdcb502013-11-23 14:54:00 -0800313{
314 datum dbm_key, val;
315
Larry Hastingsebdcb502013-11-23 14:54:00 -0800316 dbm_key.dptr = (char *)key;
317 dbm_key.dsize = key_length;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 check_dbmobject_open(dp);
Larry Hastingsebdcb502013-11-23 14:54:00 -0800319 val = dbm_fetch(dp->di_dbm, dbm_key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320 if (val.dptr != NULL)
321 return PyBytes_FromStringAndSize(val.dptr, val.dsize);
Larry Hastingsebdcb502013-11-23 14:54:00 -0800322
323 Py_INCREF(default_value);
324 return default_value;
Fred Drake1ac526d2000-09-15 21:35:14 +0000325}
326
327static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200328dbm_setdefault(dbmobject *dp, PyObject *args)
Fred Drake1ac526d2000-09-15 21:35:14 +0000329{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 datum key, val;
331 PyObject *defvalue = NULL;
332 char *tmp_ptr;
333 Py_ssize_t tmp_size;
Fred Drake1ac526d2000-09-15 21:35:14 +0000334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 if (!PyArg_ParseTuple(args, "s#|O:setdefault",
336 &tmp_ptr, &tmp_size, &defvalue))
337 return NULL;
338 key.dptr = tmp_ptr;
339 key.dsize = tmp_size;
340 check_dbmobject_open(dp);
341 val = dbm_fetch(dp->di_dbm, key);
342 if (val.dptr != NULL)
343 return PyBytes_FromStringAndSize(val.dptr, val.dsize);
344 if (defvalue == NULL) {
345 defvalue = PyBytes_FromStringAndSize(NULL, 0);
346 if (defvalue == NULL)
347 return NULL;
348 val.dptr = NULL;
349 val.dsize = 0;
350 }
351 else {
352 if ( !PyArg_Parse(defvalue, "s#", &val.dptr, &tmp_size) ) {
353 PyErr_SetString(PyExc_TypeError,
354 "dbm mappings have byte string elements only");
355 return NULL;
356 }
357 val.dsize = tmp_size;
358 Py_INCREF(defvalue);
359 }
360 if (dbm_store(dp->di_dbm, key, val, DBM_INSERT) < 0) {
361 dbm_clearerr(dp->di_dbm);
362 PyErr_SetString(DbmError, "cannot add item to database");
363 Py_DECREF(defvalue);
364 return NULL;
365 }
366 return defvalue;
Fred Drake1ac526d2000-09-15 21:35:14 +0000367}
368
Nick Coghlanc610aba2013-11-17 15:59:51 +1000369static PyObject *
370dbm__enter__(PyObject *self, PyObject *args)
371{
372 Py_INCREF(self);
373 return self;
374}
375
376static PyObject *
377dbm__exit__(PyObject *self, PyObject *args)
378{
379 _Py_IDENTIFIER(close);
380 return _PyObject_CallMethodId(self, &PyId_close, NULL);
381}
382
383
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000384static PyMethodDef dbm_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 {"close", (PyCFunction)dbm__close, METH_NOARGS,
386 "close()\nClose the database."},
387 {"keys", (PyCFunction)dbm_keys, METH_NOARGS,
388 "keys() -> list\nReturn a list of all keys in the database."},
Larry Hastingsebdcb502013-11-23 14:54:00 -0800389 DBM_DBM_GET_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 {"setdefault", (PyCFunction)dbm_setdefault, METH_VARARGS,
391 "setdefault(key[, default]) -> value\n"
392 "Return the value for key if present, otherwise default. If key\n"
393 "is not in the database, it is inserted with default as the value."},
Nick Coghlanc610aba2013-11-17 15:59:51 +1000394 {"__enter__", dbm__enter__, METH_NOARGS, NULL},
395 {"__exit__", dbm__exit__, METH_VARARGS, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 {NULL, NULL} /* sentinel */
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000397};
398
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000399static PyTypeObject Dbmtype = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 PyVarObject_HEAD_INIT(NULL, 0)
401 "_dbm.dbm",
402 sizeof(dbmobject),
403 0,
404 (destructor)dbm_dealloc, /*tp_dealloc*/
405 0, /*tp_print*/
406 0, /*tp_getattr*/
407 0, /*tp_setattr*/
408 0, /*tp_reserved*/
409 0, /*tp_repr*/
410 0, /*tp_as_number*/
411 &dbm_as_sequence, /*tp_as_sequence*/
412 &dbm_as_mapping, /*tp_as_mapping*/
413 0, /*tp_hash*/
414 0, /*tp_call*/
415 0, /*tp_str*/
416 0, /*tp_getattro*/
417 0, /*tp_setattro*/
418 0, /*tp_as_buffer*/
419 Py_TPFLAGS_DEFAULT, /*tp_flags*/
420 0, /*tp_doc*/
421 0, /*tp_traverse*/
422 0, /*tp_clear*/
423 0, /*tp_richcompare*/
424 0, /*tp_weaklistoffset*/
425 0, /*tp_iter*/
426 0, /*tp_iternext*/
427 dbm_methods, /*tp_methods*/
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000428};
429
430/* ----------------------------------------------------------------- */
431
Larry Hastings61272b72014-01-07 12:41:53 -0800432/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -0700433
434dbm.open as dbmopen
435
436 filename: str
437 The filename to open.
438
439 flags: str="r"
440 How to open the file. "r" for reading, "w" for writing, etc.
441
Larry Hastings2a727912014-01-16 11:32:01 -0800442 mode: int(py_default="0o666") = 0o666
Larry Hastings31826802013-10-19 00:09:25 -0700443 If creating a new file, the mode bits for the new file
444 (e.g. os.O_RDWR).
445
446 /
447
448Return a database object.
449
Larry Hastings61272b72014-01-07 12:41:53 -0800450[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -0700451
452PyDoc_STRVAR(dbmopen__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800453"open($module, filename, flags=\'r\', mode=0o666, /)\n"
454"--\n"
455"\n"
Larry Hastings31826802013-10-19 00:09:25 -0700456"Return a database object.\n"
457"\n"
Larry Hastings31826802013-10-19 00:09:25 -0700458" filename\n"
459" The filename to open.\n"
460" flags\n"
461" How to open the file. \"r\" for reading, \"w\" for writing, etc.\n"
462" mode\n"
463" If creating a new file, the mode bits for the new file\n"
464" (e.g. os.O_RDWR).");
465
466#define DBMOPEN_METHODDEF \
467 {"open", (PyCFunction)dbmopen, METH_VARARGS, dbmopen__doc__},
468
469static PyObject *
Larry Hastingsebdcb502013-11-23 14:54:00 -0800470dbmopen_impl(PyModuleDef *module, const char *filename, const char *flags, int mode);
Larry Hastings31826802013-10-19 00:09:25 -0700471
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000472static PyObject *
Larry Hastingsebdcb502013-11-23 14:54:00 -0800473dbmopen(PyModuleDef *module, PyObject *args)
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000474{
Larry Hastings31826802013-10-19 00:09:25 -0700475 PyObject *return_value = NULL;
476 const char *filename;
477 const char *flags = "r";
478 int mode = 438;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000479
Larry Hastings31826802013-10-19 00:09:25 -0700480 if (!PyArg_ParseTuple(args,
481 "s|si:open",
482 &filename, &flags, &mode))
483 goto exit;
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800484 return_value = dbmopen_impl(module, filename, flags, mode);
Larry Hastings31826802013-10-19 00:09:25 -0700485
486exit:
487 return return_value;
488}
489
490static PyObject *
Larry Hastingsebdcb502013-11-23 14:54:00 -0800491dbmopen_impl(PyModuleDef *module, const char *filename, const char *flags, int mode)
Larry Hastings2623c8c2014-02-08 22:15:29 -0800492/*[clinic end generated code: output=9a7b725f9c4dcec2 input=6499ab0fab1333ac]*/
Larry Hastings31826802013-10-19 00:09:25 -0700493{
494 int iflags;
495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 if ( strcmp(flags, "r") == 0 )
497 iflags = O_RDONLY;
498 else if ( strcmp(flags, "w") == 0 )
499 iflags = O_RDWR;
500 else if ( strcmp(flags, "rw") == 0 ) /* B/W compat */
501 iflags = O_RDWR|O_CREAT;
502 else if ( strcmp(flags, "c") == 0 )
503 iflags = O_RDWR|O_CREAT;
504 else if ( strcmp(flags, "n") == 0 )
505 iflags = O_RDWR|O_CREAT|O_TRUNC;
506 else {
507 PyErr_SetString(DbmError,
508 "arg 2 to open should be 'r', 'w', 'c', or 'n'");
509 return NULL;
510 }
Larry Hastings31826802013-10-19 00:09:25 -0700511 return newdbmobject(filename, iflags, mode);
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000512}
513
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000514static PyMethodDef dbmmodule_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -0700515 DBMOPEN_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000516 { 0, 0 },
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000517};
518
Martin v. Löwis1a214512008-06-11 05:26:20 +0000519
520static struct PyModuleDef _dbmmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 PyModuleDef_HEAD_INIT,
522 "_dbm",
523 NULL,
524 -1,
525 dbmmodule_methods,
526 NULL,
527 NULL,
528 NULL,
529 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000530};
531
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000532PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000533PyInit__dbm(void) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 PyObject *m, *d, *s;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000535
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 if (PyType_Ready(&Dbmtype) < 0)
537 return NULL;
538 m = PyModule_Create(&_dbmmodule);
539 if (m == NULL)
540 return NULL;
541 d = PyModule_GetDict(m);
542 if (DbmError == NULL)
543 DbmError = PyErr_NewException("_dbm.error",
544 PyExc_IOError, NULL);
545 s = PyUnicode_FromString(which_dbm);
546 if (s != NULL) {
547 PyDict_SetItemString(d, "library", s);
548 Py_DECREF(s);
549 }
550 if (DbmError != NULL)
551 PyDict_SetItemString(d, "error", DbmError);
552 if (PyErr_Occurred()) {
553 Py_DECREF(m);
554 m = NULL;
555 }
556 return m;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000557}