blob: 21784ae2ce4fd262ddef2276512ff521636abc27 [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>
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020017static const 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>
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020020static const 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>
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020023static const char which_dbm[] = "GNU gdbm";
Skip Montanaro71ffc5c2002-08-02 17:13:01 +000024#elif defined(HAVE_BERKDB_H)
25#include <db.h>
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020026static const 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]
Serhiy Storchaka9260e772015-04-17 21:05:18 +030032module _dbm
33class _dbm.dbm "dbmobject *" "&Dbmtype"
Larry Hastings61272b72014-01-07 12:41:53 -080034[clinic start generated code]*/
Serhiy Storchaka9260e772015-04-17 21:05:18 +030035/*[clinic end generated code: output=da39a3ee5e6b4b0d input=9b1aa8756d16150e]*/
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
Xiang Zhang4fb0b8b2018-12-12 20:46:55 +080039 int flags;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000040 int di_size; /* -1 means recompute */
41 DBM *di_dbm;
Guido van Rossumdd9ed831992-06-29 17:10:40 +000042} dbmobject;
43
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030044#include "clinic/_dbmmodule.c.h"
45
Jeremy Hylton938ace62002-07-17 16:30:39 +000046static PyTypeObject Dbmtype;
Guido van Rossumdd9ed831992-06-29 17:10:40 +000047
Christian Heimes90aa7642007-12-19 02:45:37 +000048#define is_dbmobject(v) (Py_TYPE(v) == &Dbmtype)
Guido van Rossum77eecfa1997-07-17 22:56:01 +000049#define check_dbmobject_open(v) if ((v)->di_dbm == NULL) \
50 { PyErr_SetString(DbmError, "DBM object has already been closed"); \
51 return NULL; }
Guido van Rossumdd9ed831992-06-29 17:10:40 +000052
Roger E. Masseb0dfe961996-12-10 00:07:00 +000053static PyObject *DbmError;
Guido van Rossumdd9ed831992-06-29 17:10:40 +000054
Roger E. Masseb0dfe961996-12-10 00:07:00 +000055static PyObject *
Larry Hastings31826802013-10-19 00:09:25 -070056newdbmobject(const char *file, int flags, int mode)
Guido van Rossumdd9ed831992-06-29 17:10:40 +000057{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000058 dbmobject *dp;
Guido van Rossumdd9ed831992-06-29 17:10:40 +000059
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 dp = PyObject_New(dbmobject, &Dbmtype);
61 if (dp == NULL)
62 return NULL;
63 dp->di_size = -1;
Xiang Zhang4fb0b8b2018-12-12 20:46:55 +080064 dp->flags = flags;
Christian Heimes8ff6f3e2013-12-06 00:20:00 +010065 /* See issue #19296 */
66 if ( (dp->di_dbm = dbm_open((char *)file, flags, mode)) == 0 ) {
Zsolt Cserna9df346b2018-09-27 21:54:34 +020067 PyErr_SetFromErrnoWithFilename(DbmError, file);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000068 Py_DECREF(dp);
69 return NULL;
70 }
71 return (PyObject *)dp;
Guido van Rossumdd9ed831992-06-29 17:10:40 +000072}
73
74/* Methods */
75
76static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020077dbm_dealloc(dbmobject *dp)
Guido van Rossumdd9ed831992-06-29 17:10:40 +000078{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000079 if ( dp->di_dbm )
80 dbm_close(dp->di_dbm);
81 PyObject_Del(dp);
Guido van Rossumdd9ed831992-06-29 17:10:40 +000082}
83
Martin v. Löwis18e16552006-02-15 17:27:45 +000084static Py_ssize_t
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +000085dbm_length(dbmobject *dp)
Guido van Rossumdd9ed831992-06-29 17:10:40 +000086{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087 if (dp->di_dbm == NULL) {
88 PyErr_SetString(DbmError, "DBM object has already been closed");
89 return -1;
90 }
91 if ( dp->di_size < 0 ) {
92 datum key;
93 int size;
Guido van Rossumdd9ed831992-06-29 17:10:40 +000094
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 size = 0;
96 for ( key=dbm_firstkey(dp->di_dbm); key.dptr;
97 key = dbm_nextkey(dp->di_dbm))
98 size++;
99 dp->di_size = size;
100 }
101 return dp->di_size;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000102}
103
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000104static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200105dbm_subscript(dbmobject *dp, PyObject *key)
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000106{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000107 datum drec, krec;
108 Py_ssize_t tmp_size;
109
110 if (!PyArg_Parse(key, "s#", &krec.dptr, &tmp_size) )
111 return NULL;
112
113 krec.dsize = tmp_size;
114 check_dbmobject_open(dp);
115 drec = dbm_fetch(dp->di_dbm, krec);
116 if ( drec.dptr == 0 ) {
117 PyErr_SetObject(PyExc_KeyError, key);
118 return NULL;
119 }
120 if ( dbm_error(dp->di_dbm) ) {
121 dbm_clearerr(dp->di_dbm);
122 PyErr_SetString(DbmError, "");
123 return NULL;
124 }
125 return PyBytes_FromStringAndSize(drec.dptr, drec.dsize);
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000126}
127
128static int
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +0000129dbm_ass_sub(dbmobject *dp, PyObject *v, PyObject *w)
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000130{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000131 datum krec, drec;
132 Py_ssize_t tmp_size;
133
134 if ( !PyArg_Parse(v, "s#", &krec.dptr, &tmp_size) ) {
135 PyErr_SetString(PyExc_TypeError,
136 "dbm mappings have bytes or string keys only");
137 return -1;
138 }
139 krec.dsize = tmp_size;
140 if (dp->di_dbm == NULL) {
141 PyErr_SetString(DbmError, "DBM object has already been closed");
142 return -1;
143 }
144 dp->di_size = -1;
145 if (w == NULL) {
146 if ( dbm_delete(dp->di_dbm, krec) < 0 ) {
147 dbm_clearerr(dp->di_dbm);
Xiang Zhang4fb0b8b2018-12-12 20:46:55 +0800148 /* we might get a failure for reasons like file corrupted,
149 but we are not able to distinguish it */
150 if (dp->flags & O_RDWR) {
151 PyErr_SetObject(PyExc_KeyError, v);
152 }
153 else {
154 PyErr_SetString(DbmError, "cannot delete item from database");
155 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 return -1;
Guido van Rossum77eecfa1997-07-17 22:56:01 +0000157 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 } else {
159 if ( !PyArg_Parse(w, "s#", &drec.dptr, &tmp_size) ) {
160 PyErr_SetString(PyExc_TypeError,
Xiang Zhang4fb0b8b2018-12-12 20:46:55 +0800161 "dbm mappings have bytes or string elements only");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 return -1;
163 }
164 drec.dsize = tmp_size;
165 if ( dbm_store(dp->di_dbm, krec, drec, DBM_REPLACE) < 0 ) {
166 dbm_clearerr(dp->di_dbm);
167 PyErr_SetString(DbmError,
168 "cannot add item to database");
169 return -1;
170 }
171 }
172 if ( dbm_error(dp->di_dbm) ) {
173 dbm_clearerr(dp->di_dbm);
174 PyErr_SetString(DbmError, "");
175 return -1;
176 }
177 return 0;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000178}
179
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000180static PyMappingMethods dbm_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000181 (lenfunc)dbm_length, /*mp_length*/
182 (binaryfunc)dbm_subscript, /*mp_subscript*/
183 (objobjargproc)dbm_ass_sub, /*mp_ass_subscript*/
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000184};
185
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300186/*[clinic input]
187_dbm.dbm.close
188
189Close the database.
190[clinic start generated code]*/
191
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000192static PyObject *
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300193_dbm_dbm_close_impl(dbmobject *self)
194/*[clinic end generated code: output=c8dc5b6709600b86 input=046db72377d51be8]*/
Guido van Rossum807b7be1995-07-07 22:37:11 +0000195{
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300196 if (self->di_dbm)
197 dbm_close(self->di_dbm);
198 self->di_dbm = NULL;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200199 Py_RETURN_NONE;
Guido van Rossum807b7be1995-07-07 22:37:11 +0000200}
201
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300202/*[clinic input]
203_dbm.dbm.keys
204
205Return a list of all keys in the database.
206[clinic start generated code]*/
207
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000208static PyObject *
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300209_dbm_dbm_keys_impl(dbmobject *self)
210/*[clinic end generated code: output=434549f7c121b33c input=d210ba778cd9c68a]*/
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000211{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200212 PyObject *v, *item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 datum key;
214 int err;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000215
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300216 check_dbmobject_open(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 v = PyList_New(0);
218 if (v == NULL)
219 return NULL;
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300220 for (key = dbm_firstkey(self->di_dbm); key.dptr;
221 key = dbm_nextkey(self->di_dbm)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000222 item = PyBytes_FromStringAndSize(key.dptr, key.dsize);
223 if (item == NULL) {
224 Py_DECREF(v);
225 return NULL;
226 }
227 err = PyList_Append(v, item);
228 Py_DECREF(item);
229 if (err != 0) {
230 Py_DECREF(v);
231 return NULL;
232 }
233 }
234 return v;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000235}
236
Guido van Rossumef671112006-08-19 23:11:47 +0000237static int
238dbm_contains(PyObject *self, PyObject *arg)
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000239{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000240 dbmobject *dp = (dbmobject *)self;
241 datum key, val;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200242 Py_ssize_t size;
Guido van Rossumef671112006-08-19 23:11:47 +0000243
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 if ((dp)->di_dbm == NULL) {
245 PyErr_SetString(DbmError,
246 "DBM object has already been closed");
247 return -1;
248 }
249 if (PyUnicode_Check(arg)) {
Serhiy Storchaka2a404b62017-01-22 23:07:07 +0200250 key.dptr = (char *)PyUnicode_AsUTF8AndSize(arg, &size);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200251 key.dsize = size;
252 if (key.dptr == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 return -1;
254 }
Serhiy Storchaka9da33ab2013-10-24 23:59:28 +0300255 else if (!PyBytes_Check(arg)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 PyErr_Format(PyExc_TypeError,
Serhiy Storchaka9da33ab2013-10-24 23:59:28 +0300257 "dbm key must be bytes or string, not %.100s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 arg->ob_type->tp_name);
259 return -1;
260 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200261 else {
262 key.dptr = PyBytes_AS_STRING(arg);
263 key.dsize = PyBytes_GET_SIZE(arg);
264 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 val = dbm_fetch(dp->di_dbm, key);
266 return val.dptr != NULL;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000267}
268
Guido van Rossumef671112006-08-19 23:11:47 +0000269static PySequenceMethods dbm_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 0, /* sq_length */
271 0, /* sq_concat */
272 0, /* sq_repeat */
273 0, /* sq_item */
274 0, /* sq_slice */
275 0, /* sq_ass_item */
276 0, /* sq_ass_slice */
277 dbm_contains, /* sq_contains */
278 0, /* sq_inplace_concat */
279 0, /* sq_inplace_repeat */
Guido van Rossumef671112006-08-19 23:11:47 +0000280};
281
Larry Hastings61272b72014-01-07 12:41:53 -0800282/*[clinic input]
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300283_dbm.dbm.get
Larry Hastingsebdcb502013-11-23 14:54:00 -0800284
Larry Hastings38337d12015-05-07 23:30:09 -0700285 key: str(accept={str, robuffer}, zeroes=True)
Serhiy Storchaka2e38cc32018-04-29 12:38:06 +0300286 default: object = None
Larry Hastingsebdcb502013-11-23 14:54:00 -0800287 /
288
289Return the value for key if present, otherwise default.
Larry Hastings61272b72014-01-07 12:41:53 -0800290[clinic start generated code]*/
Larry Hastingsebdcb502013-11-23 14:54:00 -0800291
Larry Hastingsebdcb502013-11-23 14:54:00 -0800292static PyObject *
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300293_dbm_dbm_get_impl(dbmobject *self, const char *key,
294 Py_ssize_clean_t key_length, PyObject *default_value)
Serhiy Storchaka2e38cc32018-04-29 12:38:06 +0300295/*[clinic end generated code: output=b44f95eba8203d93 input=b788eba0ffad2e91]*/
Larry Hastings38337d12015-05-07 23:30:09 -0700296/*[clinic end generated code: output=4f5c0e523eaf1251 input=9402c0af8582dc69]*/
Larry Hastingsebdcb502013-11-23 14:54:00 -0800297{
298 datum dbm_key, val;
299
Larry Hastingsebdcb502013-11-23 14:54:00 -0800300 dbm_key.dptr = (char *)key;
301 dbm_key.dsize = key_length;
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300302 check_dbmobject_open(self);
303 val = dbm_fetch(self->di_dbm, dbm_key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 if (val.dptr != NULL)
305 return PyBytes_FromStringAndSize(val.dptr, val.dsize);
Larry Hastingsebdcb502013-11-23 14:54:00 -0800306
307 Py_INCREF(default_value);
308 return default_value;
Fred Drake1ac526d2000-09-15 21:35:14 +0000309}
310
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300311/*[clinic input]
312_dbm.dbm.setdefault
Larry Hastings38337d12015-05-07 23:30:09 -0700313 key: str(accept={str, robuffer}, zeroes=True)
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300314 default: object(c_default="NULL") = b''
315 /
316
317Return the value for key if present, otherwise default.
318
319If key is not in the database, it is inserted with default as the value.
320[clinic start generated code]*/
321
Fred Drake1ac526d2000-09-15 21:35:14 +0000322static PyObject *
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300323_dbm_dbm_setdefault_impl(dbmobject *self, const char *key,
324 Py_ssize_clean_t key_length,
325 PyObject *default_value)
Larry Hastings38337d12015-05-07 23:30:09 -0700326/*[clinic end generated code: output=52545886cf272161 input=bf40c48edaca01d6]*/
Fred Drake1ac526d2000-09-15 21:35:14 +0000327{
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300328 datum dbm_key, val;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 Py_ssize_t tmp_size;
Fred Drake1ac526d2000-09-15 21:35:14 +0000330
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300331 dbm_key.dptr = (char *)key;
332 dbm_key.dsize = key_length;
333 check_dbmobject_open(self);
334 val = dbm_fetch(self->di_dbm, dbm_key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 if (val.dptr != NULL)
336 return PyBytes_FromStringAndSize(val.dptr, val.dsize);
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300337 if (default_value == NULL) {
338 default_value = PyBytes_FromStringAndSize(NULL, 0);
339 if (default_value == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 return NULL;
341 val.dptr = NULL;
342 val.dsize = 0;
343 }
344 else {
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300345 if ( !PyArg_Parse(default_value, "s#", &val.dptr, &tmp_size) ) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 PyErr_SetString(PyExc_TypeError,
Xiang Zhang4fb0b8b2018-12-12 20:46:55 +0800347 "dbm mappings have bytes or string elements only");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 return NULL;
349 }
350 val.dsize = tmp_size;
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300351 Py_INCREF(default_value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 }
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300353 if (dbm_store(self->di_dbm, dbm_key, val, DBM_INSERT) < 0) {
354 dbm_clearerr(self->di_dbm);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000355 PyErr_SetString(DbmError, "cannot add item to database");
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300356 Py_DECREF(default_value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 return NULL;
358 }
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300359 return default_value;
Fred Drake1ac526d2000-09-15 21:35:14 +0000360}
361
Nick Coghlanc610aba2013-11-17 15:59:51 +1000362static PyObject *
363dbm__enter__(PyObject *self, PyObject *args)
364{
365 Py_INCREF(self);
366 return self;
367}
368
369static PyObject *
370dbm__exit__(PyObject *self, PyObject *args)
371{
372 _Py_IDENTIFIER(close);
373 return _PyObject_CallMethodId(self, &PyId_close, NULL);
374}
375
376
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000377static PyMethodDef dbm_methods[] = {
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300378 _DBM_DBM_CLOSE_METHODDEF
379 _DBM_DBM_KEYS_METHODDEF
380 _DBM_DBM_GET_METHODDEF
381 _DBM_DBM_SETDEFAULT_METHODDEF
Nick Coghlanc610aba2013-11-17 15:59:51 +1000382 {"__enter__", dbm__enter__, METH_NOARGS, NULL},
383 {"__exit__", dbm__exit__, METH_VARARGS, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 {NULL, NULL} /* sentinel */
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000385};
386
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000387static PyTypeObject Dbmtype = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 PyVarObject_HEAD_INIT(NULL, 0)
389 "_dbm.dbm",
390 sizeof(dbmobject),
391 0,
392 (destructor)dbm_dealloc, /*tp_dealloc*/
393 0, /*tp_print*/
394 0, /*tp_getattr*/
395 0, /*tp_setattr*/
396 0, /*tp_reserved*/
397 0, /*tp_repr*/
398 0, /*tp_as_number*/
399 &dbm_as_sequence, /*tp_as_sequence*/
400 &dbm_as_mapping, /*tp_as_mapping*/
401 0, /*tp_hash*/
402 0, /*tp_call*/
403 0, /*tp_str*/
404 0, /*tp_getattro*/
405 0, /*tp_setattro*/
406 0, /*tp_as_buffer*/
407 Py_TPFLAGS_DEFAULT, /*tp_flags*/
408 0, /*tp_doc*/
409 0, /*tp_traverse*/
410 0, /*tp_clear*/
411 0, /*tp_richcompare*/
412 0, /*tp_weaklistoffset*/
413 0, /*tp_iter*/
414 0, /*tp_iternext*/
415 dbm_methods, /*tp_methods*/
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000416};
417
418/* ----------------------------------------------------------------- */
419
Larry Hastings61272b72014-01-07 12:41:53 -0800420/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -0700421
Serhiy Storchaka9260e772015-04-17 21:05:18 +0300422_dbm.open as dbmopen
Larry Hastings31826802013-10-19 00:09:25 -0700423
Serhiy Storchaka6f600ff2018-02-26 16:02:22 +0200424 filename: unicode
Larry Hastings31826802013-10-19 00:09:25 -0700425 The filename to open.
426
427 flags: str="r"
428 How to open the file. "r" for reading, "w" for writing, etc.
429
Larry Hastings2a727912014-01-16 11:32:01 -0800430 mode: int(py_default="0o666") = 0o666
Larry Hastings31826802013-10-19 00:09:25 -0700431 If creating a new file, the mode bits for the new file
432 (e.g. os.O_RDWR).
433
434 /
435
436Return a database object.
437
Larry Hastings61272b72014-01-07 12:41:53 -0800438[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -0700439
Larry Hastings31826802013-10-19 00:09:25 -0700440static PyObject *
Serhiy Storchaka6f600ff2018-02-26 16:02:22 +0200441dbmopen_impl(PyObject *module, PyObject *filename, const char *flags,
Larry Hastings89964c42015-04-14 18:07:59 -0400442 int mode)
Serhiy Storchaka6f600ff2018-02-26 16:02:22 +0200443/*[clinic end generated code: output=9527750f5df90764 input=376a9d903a50df59]*/
Larry Hastings31826802013-10-19 00:09:25 -0700444{
445 int iflags;
446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 if ( strcmp(flags, "r") == 0 )
448 iflags = O_RDONLY;
449 else if ( strcmp(flags, "w") == 0 )
450 iflags = O_RDWR;
451 else if ( strcmp(flags, "rw") == 0 ) /* B/W compat */
452 iflags = O_RDWR|O_CREAT;
453 else if ( strcmp(flags, "c") == 0 )
454 iflags = O_RDWR|O_CREAT;
455 else if ( strcmp(flags, "n") == 0 )
456 iflags = O_RDWR|O_CREAT|O_TRUNC;
457 else {
458 PyErr_SetString(DbmError,
459 "arg 2 to open should be 'r', 'w', 'c', or 'n'");
460 return NULL;
461 }
Serhiy Storchaka6f600ff2018-02-26 16:02:22 +0200462
463 PyObject *filenamebytes = PyUnicode_EncodeFSDefault(filename);
464 if (filenamebytes == NULL) {
465 return NULL;
466 }
467 const char *name = PyBytes_AS_STRING(filenamebytes);
468 if (strlen(name) != (size_t)PyBytes_GET_SIZE(filenamebytes)) {
469 Py_DECREF(filenamebytes);
470 PyErr_SetString(PyExc_ValueError, "embedded null character");
471 return NULL;
472 }
473 PyObject *self = newdbmobject(name, iflags, mode);
474 Py_DECREF(filenamebytes);
475 return self;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000476}
477
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000478static PyMethodDef dbmmodule_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -0700479 DBMOPEN_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 { 0, 0 },
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000481};
482
Martin v. Löwis1a214512008-06-11 05:26:20 +0000483
484static struct PyModuleDef _dbmmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 PyModuleDef_HEAD_INIT,
486 "_dbm",
487 NULL,
488 -1,
489 dbmmodule_methods,
490 NULL,
491 NULL,
492 NULL,
493 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000494};
495
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000496PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000497PyInit__dbm(void) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 PyObject *m, *d, *s;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000499
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 if (PyType_Ready(&Dbmtype) < 0)
501 return NULL;
502 m = PyModule_Create(&_dbmmodule);
503 if (m == NULL)
504 return NULL;
505 d = PyModule_GetDict(m);
506 if (DbmError == NULL)
507 DbmError = PyErr_NewException("_dbm.error",
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300508 PyExc_OSError, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 s = PyUnicode_FromString(which_dbm);
510 if (s != NULL) {
511 PyDict_SetItemString(d, "library", s);
512 Py_DECREF(s);
513 }
514 if (DbmError != NULL)
515 PyDict_SetItemString(d, "error", DbmError);
516 if (PyErr_Occurred()) {
517 Py_DECREF(m);
518 m = NULL;
519 }
520 return m;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000521}