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> |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 17 | static const 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> |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 20 | static const 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> |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 23 | static const 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> |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 26 | static const 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] |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 32 | module _dbm |
| 33 | class _dbm.dbm "dbmobject *" "&Dbmtype" |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 34 | [clinic start generated code]*/ |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 35 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=9b1aa8756d16150e]*/ |
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 |
Xiang Zhang | 4fb0b8b | 2018-12-12 20:46:55 +0800 | [diff] [blame] | 39 | int flags; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 40 | int di_size; /* -1 means recompute */ |
| 41 | DBM *di_dbm; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 42 | } dbmobject; |
| 43 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 44 | #include "clinic/_dbmmodule.c.h" |
| 45 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 46 | static PyTypeObject Dbmtype; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 47 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 48 | #define is_dbmobject(v) (Py_TYPE(v) == &Dbmtype) |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 49 | #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 Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 52 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 53 | static PyObject *DbmError; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 54 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 55 | static PyObject * |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 56 | newdbmobject(const char *file, int flags, int mode) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 57 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 58 | dbmobject *dp; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 59 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 60 | dp = PyObject_New(dbmobject, &Dbmtype); |
| 61 | if (dp == NULL) |
| 62 | return NULL; |
| 63 | dp->di_size = -1; |
Xiang Zhang | 4fb0b8b | 2018-12-12 20:46:55 +0800 | [diff] [blame] | 64 | dp->flags = flags; |
Christian Heimes | 8ff6f3e | 2013-12-06 00:20:00 +0100 | [diff] [blame] | 65 | /* See issue #19296 */ |
| 66 | if ( (dp->di_dbm = dbm_open((char *)file, flags, mode)) == 0 ) { |
Zsolt Cserna | 9df346b | 2018-09-27 21:54:34 +0200 | [diff] [blame] | 67 | PyErr_SetFromErrnoWithFilename(DbmError, file); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 68 | Py_DECREF(dp); |
| 69 | return NULL; |
| 70 | } |
| 71 | return (PyObject *)dp; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /* Methods */ |
| 75 | |
| 76 | static void |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 77 | dbm_dealloc(dbmobject *dp) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 78 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 79 | if ( dp->di_dbm ) |
| 80 | dbm_close(dp->di_dbm); |
| 81 | PyObject_Del(dp); |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 84 | static Py_ssize_t |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 85 | dbm_length(dbmobject *dp) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 86 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 87 | 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 Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 94 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 95 | 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 Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 104 | static PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 105 | dbm_subscript(dbmobject *dp, PyObject *key) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 106 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 107 | 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 Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | static int |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 129 | dbm_ass_sub(dbmobject *dp, PyObject *v, PyObject *w) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 130 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 131 | 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 Zhang | 4fb0b8b | 2018-12-12 20:46:55 +0800 | [diff] [blame] | 148 | /* 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 Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 156 | return -1; |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 157 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 158 | } else { |
| 159 | if ( !PyArg_Parse(w, "s#", &drec.dptr, &tmp_size) ) { |
| 160 | PyErr_SetString(PyExc_TypeError, |
Xiang Zhang | 4fb0b8b | 2018-12-12 20:46:55 +0800 | [diff] [blame] | 161 | "dbm mappings have bytes or string elements only"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 162 | 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 Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 180 | static PyMappingMethods dbm_as_mapping = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 181 | (lenfunc)dbm_length, /*mp_length*/ |
| 182 | (binaryfunc)dbm_subscript, /*mp_subscript*/ |
| 183 | (objobjargproc)dbm_ass_sub, /*mp_ass_subscript*/ |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 184 | }; |
| 185 | |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 186 | /*[clinic input] |
| 187 | _dbm.dbm.close |
| 188 | |
| 189 | Close the database. |
| 190 | [clinic start generated code]*/ |
| 191 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 192 | static PyObject * |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 193 | _dbm_dbm_close_impl(dbmobject *self) |
| 194 | /*[clinic end generated code: output=c8dc5b6709600b86 input=046db72377d51be8]*/ |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 195 | { |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 196 | if (self->di_dbm) |
| 197 | dbm_close(self->di_dbm); |
| 198 | self->di_dbm = NULL; |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 199 | Py_RETURN_NONE; |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 202 | /*[clinic input] |
| 203 | _dbm.dbm.keys |
| 204 | |
| 205 | Return a list of all keys in the database. |
| 206 | [clinic start generated code]*/ |
| 207 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 208 | static PyObject * |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 209 | _dbm_dbm_keys_impl(dbmobject *self) |
| 210 | /*[clinic end generated code: output=434549f7c121b33c input=d210ba778cd9c68a]*/ |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 211 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 212 | PyObject *v, *item; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 213 | datum key; |
| 214 | int err; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 215 | |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 216 | check_dbmobject_open(self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 217 | v = PyList_New(0); |
| 218 | if (v == NULL) |
| 219 | return NULL; |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 220 | for (key = dbm_firstkey(self->di_dbm); key.dptr; |
| 221 | key = dbm_nextkey(self->di_dbm)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 222 | 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 Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Guido van Rossum | ef67111 | 2006-08-19 23:11:47 +0000 | [diff] [blame] | 237 | static int |
| 238 | dbm_contains(PyObject *self, PyObject *arg) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 239 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 240 | dbmobject *dp = (dbmobject *)self; |
| 241 | datum key, val; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 242 | Py_ssize_t size; |
Guido van Rossum | ef67111 | 2006-08-19 23:11:47 +0000 | [diff] [blame] | 243 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 244 | 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 Storchaka | 2a404b6 | 2017-01-22 23:07:07 +0200 | [diff] [blame] | 250 | key.dptr = (char *)PyUnicode_AsUTF8AndSize(arg, &size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 251 | key.dsize = size; |
| 252 | if (key.dptr == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 253 | return -1; |
| 254 | } |
Serhiy Storchaka | 9da33ab | 2013-10-24 23:59:28 +0300 | [diff] [blame] | 255 | else if (!PyBytes_Check(arg)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 256 | PyErr_Format(PyExc_TypeError, |
Serhiy Storchaka | 9da33ab | 2013-10-24 23:59:28 +0300 | [diff] [blame] | 257 | "dbm key must be bytes or string, not %.100s", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 258 | arg->ob_type->tp_name); |
| 259 | return -1; |
| 260 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 261 | else { |
| 262 | key.dptr = PyBytes_AS_STRING(arg); |
| 263 | key.dsize = PyBytes_GET_SIZE(arg); |
| 264 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 265 | val = dbm_fetch(dp->di_dbm, key); |
| 266 | return val.dptr != NULL; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Guido van Rossum | ef67111 | 2006-08-19 23:11:47 +0000 | [diff] [blame] | 269 | static PySequenceMethods dbm_as_sequence = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 270 | 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 Rossum | ef67111 | 2006-08-19 23:11:47 +0000 | [diff] [blame] | 280 | }; |
| 281 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 282 | /*[clinic input] |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 283 | _dbm.dbm.get |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 284 | |
Larry Hastings | 38337d1 | 2015-05-07 23:30:09 -0700 | [diff] [blame] | 285 | key: str(accept={str, robuffer}, zeroes=True) |
Serhiy Storchaka | 2e38cc3 | 2018-04-29 12:38:06 +0300 | [diff] [blame] | 286 | default: object = None |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 287 | / |
| 288 | |
| 289 | Return the value for key if present, otherwise default. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 290 | [clinic start generated code]*/ |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 291 | |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 292 | static PyObject * |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 293 | _dbm_dbm_get_impl(dbmobject *self, const char *key, |
| 294 | Py_ssize_clean_t key_length, PyObject *default_value) |
Serhiy Storchaka | 2e38cc3 | 2018-04-29 12:38:06 +0300 | [diff] [blame] | 295 | /*[clinic end generated code: output=b44f95eba8203d93 input=b788eba0ffad2e91]*/ |
Larry Hastings | 38337d1 | 2015-05-07 23:30:09 -0700 | [diff] [blame] | 296 | /*[clinic end generated code: output=4f5c0e523eaf1251 input=9402c0af8582dc69]*/ |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 297 | { |
| 298 | datum dbm_key, val; |
| 299 | |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 300 | dbm_key.dptr = (char *)key; |
| 301 | dbm_key.dsize = key_length; |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 302 | check_dbmobject_open(self); |
| 303 | val = dbm_fetch(self->di_dbm, dbm_key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 304 | if (val.dptr != NULL) |
| 305 | return PyBytes_FromStringAndSize(val.dptr, val.dsize); |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 306 | |
| 307 | Py_INCREF(default_value); |
| 308 | return default_value; |
Fred Drake | 1ac526d | 2000-09-15 21:35:14 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 311 | /*[clinic input] |
| 312 | _dbm.dbm.setdefault |
Larry Hastings | 38337d1 | 2015-05-07 23:30:09 -0700 | [diff] [blame] | 313 | key: str(accept={str, robuffer}, zeroes=True) |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 314 | default: object(c_default="NULL") = b'' |
| 315 | / |
| 316 | |
| 317 | Return the value for key if present, otherwise default. |
| 318 | |
| 319 | If key is not in the database, it is inserted with default as the value. |
| 320 | [clinic start generated code]*/ |
| 321 | |
Fred Drake | 1ac526d | 2000-09-15 21:35:14 +0000 | [diff] [blame] | 322 | static PyObject * |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 323 | _dbm_dbm_setdefault_impl(dbmobject *self, const char *key, |
| 324 | Py_ssize_clean_t key_length, |
| 325 | PyObject *default_value) |
Larry Hastings | 38337d1 | 2015-05-07 23:30:09 -0700 | [diff] [blame] | 326 | /*[clinic end generated code: output=52545886cf272161 input=bf40c48edaca01d6]*/ |
Fred Drake | 1ac526d | 2000-09-15 21:35:14 +0000 | [diff] [blame] | 327 | { |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 328 | datum dbm_key, val; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 329 | Py_ssize_t tmp_size; |
Fred Drake | 1ac526d | 2000-09-15 21:35:14 +0000 | [diff] [blame] | 330 | |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 331 | 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 Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 335 | if (val.dptr != NULL) |
| 336 | return PyBytes_FromStringAndSize(val.dptr, val.dsize); |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 337 | if (default_value == NULL) { |
| 338 | default_value = PyBytes_FromStringAndSize(NULL, 0); |
| 339 | if (default_value == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 340 | return NULL; |
| 341 | val.dptr = NULL; |
| 342 | val.dsize = 0; |
| 343 | } |
| 344 | else { |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 345 | if ( !PyArg_Parse(default_value, "s#", &val.dptr, &tmp_size) ) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 346 | PyErr_SetString(PyExc_TypeError, |
Xiang Zhang | 4fb0b8b | 2018-12-12 20:46:55 +0800 | [diff] [blame] | 347 | "dbm mappings have bytes or string elements only"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 348 | return NULL; |
| 349 | } |
| 350 | val.dsize = tmp_size; |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 351 | Py_INCREF(default_value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 352 | } |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 353 | if (dbm_store(self->di_dbm, dbm_key, val, DBM_INSERT) < 0) { |
| 354 | dbm_clearerr(self->di_dbm); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 355 | PyErr_SetString(DbmError, "cannot add item to database"); |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 356 | Py_DECREF(default_value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 357 | return NULL; |
| 358 | } |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 359 | return default_value; |
Fred Drake | 1ac526d | 2000-09-15 21:35:14 +0000 | [diff] [blame] | 360 | } |
| 361 | |
Nick Coghlan | c610aba | 2013-11-17 15:59:51 +1000 | [diff] [blame] | 362 | static PyObject * |
| 363 | dbm__enter__(PyObject *self, PyObject *args) |
| 364 | { |
| 365 | Py_INCREF(self); |
| 366 | return self; |
| 367 | } |
| 368 | |
| 369 | static PyObject * |
| 370 | dbm__exit__(PyObject *self, PyObject *args) |
| 371 | { |
| 372 | _Py_IDENTIFIER(close); |
| 373 | return _PyObject_CallMethodId(self, &PyId_close, NULL); |
| 374 | } |
| 375 | |
| 376 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 377 | static PyMethodDef dbm_methods[] = { |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 378 | _DBM_DBM_CLOSE_METHODDEF |
| 379 | _DBM_DBM_KEYS_METHODDEF |
| 380 | _DBM_DBM_GET_METHODDEF |
| 381 | _DBM_DBM_SETDEFAULT_METHODDEF |
Nick Coghlan | c610aba | 2013-11-17 15:59:51 +1000 | [diff] [blame] | 382 | {"__enter__", dbm__enter__, METH_NOARGS, NULL}, |
| 383 | {"__exit__", dbm__exit__, METH_VARARGS, NULL}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 384 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 385 | }; |
| 386 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 387 | static PyTypeObject Dbmtype = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 388 | 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 Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 416 | }; |
| 417 | |
| 418 | /* ----------------------------------------------------------------- */ |
| 419 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 420 | /*[clinic input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 421 | |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 422 | _dbm.open as dbmopen |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 423 | |
Serhiy Storchaka | 6f600ff | 2018-02-26 16:02:22 +0200 | [diff] [blame] | 424 | filename: unicode |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 425 | The filename to open. |
| 426 | |
| 427 | flags: str="r" |
| 428 | How to open the file. "r" for reading, "w" for writing, etc. |
| 429 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 430 | mode: int(py_default="0o666") = 0o666 |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 431 | If creating a new file, the mode bits for the new file |
| 432 | (e.g. os.O_RDWR). |
| 433 | |
| 434 | / |
| 435 | |
| 436 | Return a database object. |
| 437 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 438 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 439 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 440 | static PyObject * |
Serhiy Storchaka | 6f600ff | 2018-02-26 16:02:22 +0200 | [diff] [blame] | 441 | dbmopen_impl(PyObject *module, PyObject *filename, const char *flags, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 442 | int mode) |
Serhiy Storchaka | 6f600ff | 2018-02-26 16:02:22 +0200 | [diff] [blame] | 443 | /*[clinic end generated code: output=9527750f5df90764 input=376a9d903a50df59]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 444 | { |
| 445 | int iflags; |
| 446 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 447 | 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 Storchaka | 6f600ff | 2018-02-26 16:02:22 +0200 | [diff] [blame] | 462 | |
| 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 Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 476 | } |
| 477 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 478 | static PyMethodDef dbmmodule_methods[] = { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 479 | DBMOPEN_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 480 | { 0, 0 }, |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 481 | }; |
| 482 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 483 | |
| 484 | static struct PyModuleDef _dbmmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 485 | PyModuleDef_HEAD_INIT, |
| 486 | "_dbm", |
| 487 | NULL, |
| 488 | -1, |
| 489 | dbmmodule_methods, |
| 490 | NULL, |
| 491 | NULL, |
| 492 | NULL, |
| 493 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 494 | }; |
| 495 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 496 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 497 | PyInit__dbm(void) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 498 | PyObject *m, *d, *s; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 499 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 500 | 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 Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 508 | PyExc_OSError, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 509 | 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 Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 521 | } |