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 |
Larry Hastings | c204726 | 2014-01-25 20:43:29 -0800 | [diff] [blame] | 33 | class dbm.dbm "dbmobject *" "&Dbmtype" |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 34 | [clinic start generated code]*/ |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 35 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=92450564684a69a3]*/ |
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 *" |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 55 | def pre_render(self): |
| 56 | super().pre_render() |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 57 | self.name = 'dp' |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 58 | [python start generated code]*/ |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 59 | /*[python end generated code: output=da39a3ee5e6b4b0d input=6ad536357913879a]*/ |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 60 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 61 | static PyObject * |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 62 | newdbmobject(const char *file, int flags, int mode) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 63 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 64 | dbmobject *dp; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 65 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 66 | dp = PyObject_New(dbmobject, &Dbmtype); |
| 67 | if (dp == NULL) |
| 68 | return NULL; |
| 69 | dp->di_size = -1; |
Christian Heimes | 8ff6f3e | 2013-12-06 00:20:00 +0100 | [diff] [blame] | 70 | /* See issue #19296 */ |
| 71 | if ( (dp->di_dbm = dbm_open((char *)file, flags, mode)) == 0 ) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 72 | PyErr_SetFromErrno(DbmError); |
| 73 | Py_DECREF(dp); |
| 74 | return NULL; |
| 75 | } |
| 76 | return (PyObject *)dp; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /* Methods */ |
| 80 | |
| 81 | static void |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 82 | dbm_dealloc(dbmobject *dp) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 83 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 84 | if ( dp->di_dbm ) |
| 85 | dbm_close(dp->di_dbm); |
| 86 | PyObject_Del(dp); |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 89 | static Py_ssize_t |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 90 | dbm_length(dbmobject *dp) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 91 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 92 | 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 Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 99 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 100 | 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 Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 109 | static PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 110 | dbm_subscript(dbmobject *dp, PyObject *key) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 111 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 112 | 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 Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | static int |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 134 | dbm_ass_sub(dbmobject *dp, PyObject *v, PyObject *w) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 135 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 136 | 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 Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 155 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 156 | } 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 Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 178 | static PyMappingMethods dbm_as_mapping = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 179 | (lenfunc)dbm_length, /*mp_length*/ |
| 180 | (binaryfunc)dbm_subscript, /*mp_subscript*/ |
| 181 | (objobjargproc)dbm_ass_sub, /*mp_ass_subscript*/ |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 182 | }; |
| 183 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 184 | static PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 185 | dbm__close(dbmobject *dp, PyObject *unused) |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 186 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 187 | 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 Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 194 | static PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 195 | dbm_keys(dbmobject *dp, PyObject *unused) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 196 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 197 | PyObject *v, *item; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 198 | datum key; |
| 199 | int err; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 200 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 201 | 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 Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Guido van Rossum | ef67111 | 2006-08-19 23:11:47 +0000 | [diff] [blame] | 222 | static int |
| 223 | dbm_contains(PyObject *self, PyObject *arg) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 224 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 225 | dbmobject *dp = (dbmobject *)self; |
| 226 | datum key, val; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 227 | Py_ssize_t size; |
Guido van Rossum | ef67111 | 2006-08-19 23:11:47 +0000 | [diff] [blame] | 228 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 229 | 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öwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 235 | key.dptr = PyUnicode_AsUTF8AndSize(arg, &size); |
| 236 | key.dsize = size; |
| 237 | if (key.dptr == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 238 | return -1; |
| 239 | } |
Serhiy Storchaka | 9da33ab | 2013-10-24 23:59:28 +0300 | [diff] [blame] | 240 | else if (!PyBytes_Check(arg)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 241 | PyErr_Format(PyExc_TypeError, |
Serhiy Storchaka | 9da33ab | 2013-10-24 23:59:28 +0300 | [diff] [blame] | 242 | "dbm key must be bytes or string, not %.100s", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 243 | arg->ob_type->tp_name); |
| 244 | return -1; |
| 245 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 246 | else { |
| 247 | key.dptr = PyBytes_AS_STRING(arg); |
| 248 | key.dsize = PyBytes_GET_SIZE(arg); |
| 249 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 250 | val = dbm_fetch(dp->di_dbm, key); |
| 251 | return val.dptr != NULL; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Guido van Rossum | ef67111 | 2006-08-19 23:11:47 +0000 | [diff] [blame] | 254 | static PySequenceMethods dbm_as_sequence = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 255 | 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 Rossum | ef67111 | 2006-08-19 23:11:47 +0000 | [diff] [blame] | 265 | }; |
| 266 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 267 | /*[clinic input] |
Fred Drake | 1ac526d | 2000-09-15 21:35:14 +0000 | [diff] [blame] | 268 | |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 269 | dbm.dbm.get |
| 270 | |
| 271 | self: dbmobject |
| 272 | |
| 273 | key: str(length=True) |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 274 | default: object = None |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 275 | / |
| 276 | |
| 277 | Return the value for key if present, otherwise default. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 278 | [clinic start generated code]*/ |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 279 | |
| 280 | PyDoc_STRVAR(dbm_dbm_get__doc__, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 281 | "get($self, key, default=None, /)\n" |
| 282 | "--\n" |
| 283 | "\n" |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 284 | "Return the value for key if present, otherwise default."); |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 285 | |
| 286 | #define DBM_DBM_GET_METHODDEF \ |
| 287 | {"get", (PyCFunction)dbm_dbm_get, METH_VARARGS, dbm_dbm_get__doc__}, |
| 288 | |
| 289 | static PyObject * |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 290 | dbm_dbm_get_impl(dbmobject *dp, const char *key, Py_ssize_clean_t key_length, PyObject *default_value); |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 291 | |
| 292 | static PyObject * |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 293 | dbm_dbm_get(dbmobject *dp, PyObject *args) |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 294 | { |
| 295 | PyObject *return_value = NULL; |
| 296 | const char *key; |
| 297 | Py_ssize_clean_t key_length; |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 298 | PyObject *default_value = Py_None; |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 299 | |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 300 | 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 Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 305 | |
Larry Hastings | 4625826 | 2014-01-22 03:05:49 -0800 | [diff] [blame] | 306 | exit: |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 307 | return return_value; |
| 308 | } |
| 309 | |
| 310 | static PyObject * |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 311 | dbm_dbm_get_impl(dbmobject *dp, const char *key, Py_ssize_clean_t key_length, PyObject *default_value) |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 312 | /*[clinic end generated code: output=452ea11394e7e92d input=aecf5efd2f2b1a3b]*/ |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 313 | { |
| 314 | datum dbm_key, val; |
| 315 | |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 316 | dbm_key.dptr = (char *)key; |
| 317 | dbm_key.dsize = key_length; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 318 | check_dbmobject_open(dp); |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 319 | val = dbm_fetch(dp->di_dbm, dbm_key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 320 | if (val.dptr != NULL) |
| 321 | return PyBytes_FromStringAndSize(val.dptr, val.dsize); |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 322 | |
| 323 | Py_INCREF(default_value); |
| 324 | return default_value; |
Fred Drake | 1ac526d | 2000-09-15 21:35:14 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | static PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 328 | dbm_setdefault(dbmobject *dp, PyObject *args) |
Fred Drake | 1ac526d | 2000-09-15 21:35:14 +0000 | [diff] [blame] | 329 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 330 | datum key, val; |
| 331 | PyObject *defvalue = NULL; |
| 332 | char *tmp_ptr; |
| 333 | Py_ssize_t tmp_size; |
Fred Drake | 1ac526d | 2000-09-15 21:35:14 +0000 | [diff] [blame] | 334 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 335 | 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 Drake | 1ac526d | 2000-09-15 21:35:14 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Nick Coghlan | c610aba | 2013-11-17 15:59:51 +1000 | [diff] [blame] | 369 | static PyObject * |
| 370 | dbm__enter__(PyObject *self, PyObject *args) |
| 371 | { |
| 372 | Py_INCREF(self); |
| 373 | return self; |
| 374 | } |
| 375 | |
| 376 | static PyObject * |
| 377 | dbm__exit__(PyObject *self, PyObject *args) |
| 378 | { |
| 379 | _Py_IDENTIFIER(close); |
| 380 | return _PyObject_CallMethodId(self, &PyId_close, NULL); |
| 381 | } |
| 382 | |
| 383 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 384 | static PyMethodDef dbm_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 385 | {"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 Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 389 | DBM_DBM_GET_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 390 | {"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 Coghlan | c610aba | 2013-11-17 15:59:51 +1000 | [diff] [blame] | 394 | {"__enter__", dbm__enter__, METH_NOARGS, NULL}, |
| 395 | {"__exit__", dbm__exit__, METH_VARARGS, NULL}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 396 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 397 | }; |
| 398 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 399 | static PyTypeObject Dbmtype = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 400 | 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 Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 428 | }; |
| 429 | |
| 430 | /* ----------------------------------------------------------------- */ |
| 431 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 432 | /*[clinic input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 433 | |
| 434 | dbm.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 Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 442 | mode: int(py_default="0o666") = 0o666 |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 443 | If creating a new file, the mode bits for the new file |
| 444 | (e.g. os.O_RDWR). |
| 445 | |
| 446 | / |
| 447 | |
| 448 | Return a database object. |
| 449 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 450 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 451 | |
| 452 | PyDoc_STRVAR(dbmopen__doc__, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 453 | "open($module, filename, flags=\'r\', mode=0o666, /)\n" |
| 454 | "--\n" |
| 455 | "\n" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 456 | "Return a database object.\n" |
| 457 | "\n" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 458 | " 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 | |
| 469 | static PyObject * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 470 | dbmopen_impl(PyModuleDef *module, const char *filename, const char *flags, int mode); |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 471 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 472 | static PyObject * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 473 | dbmopen(PyModuleDef *module, PyObject *args) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 474 | { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 475 | PyObject *return_value = NULL; |
| 476 | const char *filename; |
| 477 | const char *flags = "r"; |
| 478 | int mode = 438; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 479 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 480 | if (!PyArg_ParseTuple(args, |
| 481 | "s|si:open", |
| 482 | &filename, &flags, &mode)) |
| 483 | goto exit; |
Larry Hastings | ed4a1c5 | 2013-11-18 09:32:13 -0800 | [diff] [blame] | 484 | return_value = dbmopen_impl(module, filename, flags, mode); |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 485 | |
| 486 | exit: |
| 487 | return return_value; |
| 488 | } |
| 489 | |
| 490 | static PyObject * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 491 | dbmopen_impl(PyModuleDef *module, const char *filename, const char *flags, int mode) |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 492 | /*[clinic end generated code: output=9a7b725f9c4dcec2 input=6499ab0fab1333ac]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 493 | { |
| 494 | int iflags; |
| 495 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 496 | 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 Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 511 | return newdbmobject(filename, iflags, mode); |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 514 | static PyMethodDef dbmmodule_methods[] = { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 515 | DBMOPEN_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 516 | { 0, 0 }, |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 517 | }; |
| 518 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 519 | |
| 520 | static struct PyModuleDef _dbmmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 521 | PyModuleDef_HEAD_INIT, |
| 522 | "_dbm", |
| 523 | NULL, |
| 524 | -1, |
| 525 | dbmmodule_methods, |
| 526 | NULL, |
| 527 | NULL, |
| 528 | NULL, |
| 529 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 530 | }; |
| 531 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 532 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 533 | PyInit__dbm(void) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 534 | PyObject *m, *d, *s; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 535 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 536 | 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 Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 557 | } |