Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 1 | |
| 2 | /* DBM module using dictionary interface */ |
| 3 | |
| 4 | |
Mark Dickinson | fdaa3a3 | 2009-01-16 19:31:13 +0000 | [diff] [blame] | 5 | #define PY_SSIZE_T_CLEAN |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 6 | #include "Python.h" |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 7 | |
| 8 | #include <sys/types.h> |
| 9 | #include <sys/stat.h> |
| 10 | #include <fcntl.h> |
Fred Drake | d94f707 | 2000-09-14 15:48:06 +0000 | [diff] [blame] | 11 | |
| 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 Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 16 | #include <ndbm.h> |
Andrew MacIntyre | 7bf6833 | 2002-03-03 02:59:16 +0000 | [diff] [blame] | 17 | static char *which_dbm = "GNU gdbm"; /* EMX port of GDBM */ |
Fred Drake | d94f707 | 2000-09-14 15:48:06 +0000 | [diff] [blame] | 18 | #elif defined(HAVE_GDBM_NDBM_H) |
| 19 | #include <gdbm/ndbm.h> |
Fred Drake | 1ac526d | 2000-09-15 21:35:14 +0000 | [diff] [blame] | 20 | static char *which_dbm = "GNU gdbm"; |
Skip Montanaro | c1ce286 | 2008-12-06 17:25:02 +0000 | [diff] [blame] | 21 | #elif defined(HAVE_GDBM_DASH_NDBM_H) |
| 22 | #include <gdbm-ndbm.h> |
| 23 | static char *which_dbm = "GNU gdbm"; |
Skip Montanaro | 71ffc5c | 2002-08-02 17:13:01 +0000 | [diff] [blame] | 24 | #elif defined(HAVE_BERKDB_H) |
| 25 | #include <db.h> |
| 26 | static char *which_dbm = "Berkeley DB"; |
Fred Drake | d94f707 | 2000-09-14 15:48:06 +0000 | [diff] [blame] | 27 | #else |
| 28 | #error "No ndbm.h available!" |
| 29 | #endif |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 30 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 31 | /*[clinic input] |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 32 | module dbm |
| 33 | class dbm.dbm |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 34 | [clinic start generated code]*/ |
| 35 | /*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 36 | |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 37 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 38 | PyObject_HEAD |
| 39 | int di_size; /* -1 means recompute */ |
| 40 | DBM *di_dbm; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 41 | } dbmobject; |
| 42 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 43 | static PyTypeObject Dbmtype; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 44 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 45 | #define is_dbmobject(v) (Py_TYPE(v) == &Dbmtype) |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 46 | #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 Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 49 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 50 | static PyObject *DbmError; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 51 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 52 | /*[python input] |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 53 | class dbmobject_converter(self_converter): |
| 54 | type = "dbmobject *" |
| 55 | def converter_init(self): |
| 56 | self.name = 'dp' |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 57 | [python start generated code]*/ |
| 58 | /*[python end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 59 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 60 | static PyObject * |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 61 | newdbmobject(const char *file, int flags, int mode) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 62 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 63 | dbmobject *dp; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 64 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 65 | dp = PyObject_New(dbmobject, &Dbmtype); |
| 66 | if (dp == NULL) |
| 67 | return NULL; |
| 68 | dp->di_size = -1; |
Christian Heimes | 8ff6f3e | 2013-12-06 00:20:00 +0100 | [diff] [blame] | 69 | /* See issue #19296 */ |
| 70 | if ( (dp->di_dbm = dbm_open((char *)file, flags, mode)) == 0 ) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 71 | PyErr_SetFromErrno(DbmError); |
| 72 | Py_DECREF(dp); |
| 73 | return NULL; |
| 74 | } |
| 75 | return (PyObject *)dp; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /* Methods */ |
| 79 | |
| 80 | static void |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 81 | dbm_dealloc(dbmobject *dp) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 82 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 83 | if ( dp->di_dbm ) |
| 84 | dbm_close(dp->di_dbm); |
| 85 | PyObject_Del(dp); |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 88 | static Py_ssize_t |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 89 | dbm_length(dbmobject *dp) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 90 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 91 | 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 Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 98 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 99 | 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 Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 108 | static PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 109 | dbm_subscript(dbmobject *dp, PyObject *key) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 110 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 111 | 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 Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | static int |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 133 | dbm_ass_sub(dbmobject *dp, PyObject *v, PyObject *w) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 134 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 135 | 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 Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 154 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 155 | } 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 Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 177 | static PyMappingMethods dbm_as_mapping = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 178 | (lenfunc)dbm_length, /*mp_length*/ |
| 179 | (binaryfunc)dbm_subscript, /*mp_subscript*/ |
| 180 | (objobjargproc)dbm_ass_sub, /*mp_ass_subscript*/ |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 181 | }; |
| 182 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 183 | static PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 184 | dbm__close(dbmobject *dp, PyObject *unused) |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 185 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 186 | 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 Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 193 | static PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 194 | dbm_keys(dbmobject *dp, PyObject *unused) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 195 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 196 | PyObject *v, *item; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 197 | datum key; |
| 198 | int err; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 199 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 200 | 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 Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Guido van Rossum | ef67111 | 2006-08-19 23:11:47 +0000 | [diff] [blame] | 221 | static int |
| 222 | dbm_contains(PyObject *self, PyObject *arg) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 223 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 224 | dbmobject *dp = (dbmobject *)self; |
| 225 | datum key, val; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 226 | Py_ssize_t size; |
Guido van Rossum | ef67111 | 2006-08-19 23:11:47 +0000 | [diff] [blame] | 227 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 228 | 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öwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 234 | key.dptr = PyUnicode_AsUTF8AndSize(arg, &size); |
| 235 | key.dsize = size; |
| 236 | if (key.dptr == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 237 | return -1; |
| 238 | } |
Serhiy Storchaka | 9da33ab | 2013-10-24 23:59:28 +0300 | [diff] [blame] | 239 | else if (!PyBytes_Check(arg)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 240 | PyErr_Format(PyExc_TypeError, |
Serhiy Storchaka | 9da33ab | 2013-10-24 23:59:28 +0300 | [diff] [blame] | 241 | "dbm key must be bytes or string, not %.100s", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 242 | arg->ob_type->tp_name); |
| 243 | return -1; |
| 244 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 245 | else { |
| 246 | key.dptr = PyBytes_AS_STRING(arg); |
| 247 | key.dsize = PyBytes_GET_SIZE(arg); |
| 248 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 249 | val = dbm_fetch(dp->di_dbm, key); |
| 250 | return val.dptr != NULL; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Guido van Rossum | ef67111 | 2006-08-19 23:11:47 +0000 | [diff] [blame] | 253 | static PySequenceMethods dbm_as_sequence = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 254 | 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 Rossum | ef67111 | 2006-08-19 23:11:47 +0000 | [diff] [blame] | 264 | }; |
| 265 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 266 | /*[clinic input] |
Fred Drake | 1ac526d | 2000-09-15 21:35:14 +0000 | [diff] [blame] | 267 | |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 268 | dbm.dbm.get |
| 269 | |
| 270 | self: dbmobject |
| 271 | |
| 272 | key: str(length=True) |
| 273 | [ |
| 274 | default: object |
| 275 | ] |
| 276 | / |
| 277 | |
| 278 | Return the value for key if present, otherwise default. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 279 | [clinic start generated code]*/ |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 280 | |
| 281 | PyDoc_STRVAR(dbm_dbm_get__doc__, |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame^] | 282 | "get(self, key, [default])\n" |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 283 | "Return the value for key if present, otherwise default."); |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 284 | |
| 285 | #define DBM_DBM_GET_METHODDEF \ |
| 286 | {"get", (PyCFunction)dbm_dbm_get, METH_VARARGS, dbm_dbm_get__doc__}, |
| 287 | |
| 288 | static PyObject * |
| 289 | dbm_dbm_get_impl(dbmobject *dp, const char *key, Py_ssize_clean_t key_length, int group_right_1, PyObject *default_value); |
| 290 | |
| 291 | static PyObject * |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame^] | 292 | dbm_dbm_get(dbmobject *dp, PyObject *args) |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 293 | { |
| 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 Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 300 | switch (PyTuple_GET_SIZE(args)) { |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 301 | case 1: |
| 302 | if (!PyArg_ParseTuple(args, "s#:get", &key, &key_length)) |
Larry Hastings | 4625826 | 2014-01-22 03:05:49 -0800 | [diff] [blame] | 303 | goto exit; |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 304 | break; |
| 305 | case 2: |
| 306 | if (!PyArg_ParseTuple(args, "s#O:get", &key, &key_length, &default_value)) |
Larry Hastings | 4625826 | 2014-01-22 03:05:49 -0800 | [diff] [blame] | 307 | goto exit; |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 308 | group_right_1 = 1; |
| 309 | break; |
| 310 | default: |
| 311 | PyErr_SetString(PyExc_TypeError, "dbm.dbm.get requires 1 to 2 arguments"); |
Larry Hastings | 4625826 | 2014-01-22 03:05:49 -0800 | [diff] [blame] | 312 | goto exit; |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 313 | } |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame^] | 314 | return_value = dbm_dbm_get_impl(dp, key, key_length, group_right_1, default_value); |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 315 | |
Larry Hastings | 4625826 | 2014-01-22 03:05:49 -0800 | [diff] [blame] | 316 | exit: |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 317 | return return_value; |
| 318 | } |
| 319 | |
| 320 | static PyObject * |
| 321 | dbm_dbm_get_impl(dbmobject *dp, const char *key, Py_ssize_clean_t key_length, int group_right_1, PyObject *default_value) |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame^] | 322 | /*[clinic end generated code: checksum=31d5180d6b36f1eafea78ec4391adf3559916379]*/ |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 323 | { |
| 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 Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 330 | check_dbmobject_open(dp); |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 331 | val = dbm_fetch(dp->di_dbm, dbm_key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 332 | if (val.dptr != NULL) |
| 333 | return PyBytes_FromStringAndSize(val.dptr, val.dsize); |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 334 | |
| 335 | Py_INCREF(default_value); |
| 336 | return default_value; |
Fred Drake | 1ac526d | 2000-09-15 21:35:14 +0000 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | static PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 340 | dbm_setdefault(dbmobject *dp, PyObject *args) |
Fred Drake | 1ac526d | 2000-09-15 21:35:14 +0000 | [diff] [blame] | 341 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 342 | datum key, val; |
| 343 | PyObject *defvalue = NULL; |
| 344 | char *tmp_ptr; |
| 345 | Py_ssize_t tmp_size; |
Fred Drake | 1ac526d | 2000-09-15 21:35:14 +0000 | [diff] [blame] | 346 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 347 | 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 Drake | 1ac526d | 2000-09-15 21:35:14 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Nick Coghlan | c610aba | 2013-11-17 15:59:51 +1000 | [diff] [blame] | 381 | static PyObject * |
| 382 | dbm__enter__(PyObject *self, PyObject *args) |
| 383 | { |
| 384 | Py_INCREF(self); |
| 385 | return self; |
| 386 | } |
| 387 | |
| 388 | static PyObject * |
| 389 | dbm__exit__(PyObject *self, PyObject *args) |
| 390 | { |
| 391 | _Py_IDENTIFIER(close); |
| 392 | return _PyObject_CallMethodId(self, &PyId_close, NULL); |
| 393 | } |
| 394 | |
| 395 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 396 | static PyMethodDef dbm_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 397 | {"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 Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 401 | DBM_DBM_GET_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 402 | {"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 Coghlan | c610aba | 2013-11-17 15:59:51 +1000 | [diff] [blame] | 406 | {"__enter__", dbm__enter__, METH_NOARGS, NULL}, |
| 407 | {"__exit__", dbm__exit__, METH_VARARGS, NULL}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 408 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 409 | }; |
| 410 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 411 | static PyTypeObject Dbmtype = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 412 | 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 Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 440 | }; |
| 441 | |
| 442 | /* ----------------------------------------------------------------- */ |
| 443 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 444 | /*[clinic input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 445 | |
| 446 | dbm.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 Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 454 | mode: int(py_default="0o666") = 0o666 |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 455 | If creating a new file, the mode bits for the new file |
| 456 | (e.g. os.O_RDWR). |
| 457 | |
| 458 | / |
| 459 | |
| 460 | Return a database object. |
| 461 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 462 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 463 | |
| 464 | PyDoc_STRVAR(dbmopen__doc__, |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame^] | 465 | "open(module, filename, flags=\'r\', mode=0o666)\n" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 466 | "Return a database object.\n" |
| 467 | "\n" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 468 | " 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 | |
| 479 | static PyObject * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 480 | dbmopen_impl(PyModuleDef *module, const char *filename, const char *flags, int mode); |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 481 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 482 | static PyObject * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 483 | dbmopen(PyModuleDef *module, PyObject *args) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 484 | { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 485 | PyObject *return_value = NULL; |
| 486 | const char *filename; |
| 487 | const char *flags = "r"; |
| 488 | int mode = 438; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 489 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 490 | if (!PyArg_ParseTuple(args, |
| 491 | "s|si:open", |
| 492 | &filename, &flags, &mode)) |
| 493 | goto exit; |
Larry Hastings | ed4a1c5 | 2013-11-18 09:32:13 -0800 | [diff] [blame] | 494 | return_value = dbmopen_impl(module, filename, flags, mode); |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 495 | |
| 496 | exit: |
| 497 | return return_value; |
| 498 | } |
| 499 | |
| 500 | static PyObject * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 501 | dbmopen_impl(PyModuleDef *module, const char *filename, const char *flags, int mode) |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame^] | 502 | /*[clinic end generated code: checksum=9efae7d3c3b67a365011bf4e463e918901ba6c79]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 503 | { |
| 504 | int iflags; |
| 505 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 506 | 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 Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 521 | return newdbmobject(filename, iflags, mode); |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 524 | static PyMethodDef dbmmodule_methods[] = { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 525 | DBMOPEN_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 526 | { 0, 0 }, |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 527 | }; |
| 528 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 529 | |
| 530 | static struct PyModuleDef _dbmmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 531 | PyModuleDef_HEAD_INIT, |
| 532 | "_dbm", |
| 533 | NULL, |
| 534 | -1, |
| 535 | dbmmodule_methods, |
| 536 | NULL, |
| 537 | NULL, |
| 538 | NULL, |
| 539 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 540 | }; |
| 541 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 542 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 543 | PyInit__dbm(void) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 544 | PyObject *m, *d, *s; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 545 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 546 | 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 Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 567 | } |