Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 1 | |
| 2 | /* DBM module using dictionary interface */ |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 3 | /* Author: Anthony Baxter, after dbmmodule.c */ |
| 4 | /* Doc strings: Mitch Chapman */ |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 5 | |
| 6 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 7 | #include "Python.h" |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 8 | |
| 9 | #include <sys/types.h> |
| 10 | #include <sys/stat.h> |
| 11 | #include <fcntl.h> |
| 12 | #include "gdbm.h" |
| 13 | |
Tim Peters | 5687ffe | 2001-02-28 16:44:18 +0000 | [diff] [blame] | 14 | #if defined(WIN32) && !defined(__CYGWIN__) |
Guido van Rossum | b6e2a99 | 1998-10-03 05:13:27 +0000 | [diff] [blame] | 15 | #include "gdbmerrno.h" |
| 16 | extern const char * gdbm_strerror(gdbm_error); |
| 17 | #endif |
| 18 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 19 | PyDoc_STRVAR(gdbmmodule__doc__, |
| 20 | "This module provides an interface to the GNU DBM (GDBM) library.\n\ |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 21 | \n\ |
| 22 | This module is quite similar to the dbm module, but uses GDBM instead to\n\ |
| 23 | provide some additional functionality. Please note that the file formats\n\ |
| 24 | created by GDBM and dbm are incompatible. \n\ |
| 25 | \n\ |
| 26 | GDBM objects behave like mappings (dictionaries), except that keys and\n\ |
| 27 | values are always strings. Printing a GDBM object doesn't print the\n\ |
| 28 | keys and values, and the items() and values() methods are not\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 29 | supported."); |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 30 | |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 31 | typedef struct { |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 32 | PyObject_HEAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 33 | int di_size; /* -1 means recompute */ |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 34 | GDBM_FILE di_dbm; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 35 | } dbmobject; |
| 36 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 37 | static PyTypeObject Dbmtype; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 38 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 39 | #define is_dbmobject(v) (Py_TYPE(v) == &Dbmtype) |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 40 | #define check_dbmobject_open(v) if ((v)->di_dbm == NULL) \ |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 41 | { PyErr_SetString(DbmError, "GDBM object has already been closed"); \ |
| 42 | return NULL; } |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 43 | |
| 44 | |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 45 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 46 | static PyObject *DbmError; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 47 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 48 | PyDoc_STRVAR(gdbm_object__doc__, |
| 49 | "This object represents a GDBM database.\n\ |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 50 | GDBM objects behave like mappings (dictionaries), except that keys and\n\ |
| 51 | values are always strings. Printing a GDBM object doesn't print the\n\ |
| 52 | keys and values, and the items() and values() methods are not\n\ |
| 53 | supported.\n\ |
| 54 | \n\ |
| 55 | GDBM objects also support additional operations such as firstkey,\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 56 | nextkey, reorganize, and sync."); |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 57 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 58 | static PyObject * |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 59 | newdbmobject(char *file, int flags, int mode) |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 60 | { |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 61 | dbmobject *dp; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 62 | |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 63 | dp = PyObject_New(dbmobject, &Dbmtype); |
| 64 | if (dp == NULL) |
| 65 | return NULL; |
| 66 | dp->di_size = -1; |
| 67 | errno = 0; |
| 68 | if ((dp->di_dbm = gdbm_open(file, 0, flags, mode, NULL)) == 0) { |
| 69 | if (errno != 0) |
| 70 | PyErr_SetFromErrno(DbmError); |
| 71 | else |
| 72 | PyErr_SetString(DbmError, gdbm_strerror(gdbm_errno)); |
| 73 | Py_DECREF(dp); |
| 74 | return NULL; |
| 75 | } |
| 76 | return (PyObject *)dp; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +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 | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 83 | { |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 84 | if (dp->di_dbm) |
| 85 | gdbm_close(dp->di_dbm); |
| 86 | PyObject_Del(dp); |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 89 | static Py_ssize_t |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 90 | dbm_length(dbmobject *dp) |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 91 | { |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 92 | if (dp->di_dbm == NULL) { |
Guido van Rossum | 6252e10 | 2007-05-23 20:51:02 +0000 | [diff] [blame] | 93 | PyErr_SetString(DbmError, "GDBM object has already been closed"); |
| 94 | return -1; |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 95 | } |
| 96 | if (dp->di_size < 0) { |
| 97 | datum key,okey; |
| 98 | int size; |
| 99 | okey.dsize=0; |
Thomas Wouters | a5fa2a8 | 2006-03-01 22:54:36 +0000 | [diff] [blame] | 100 | okey.dptr=NULL; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 101 | |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 102 | size = 0; |
| 103 | for (key=gdbm_firstkey(dp->di_dbm); key.dptr; |
| 104 | key = gdbm_nextkey(dp->di_dbm,okey)) { |
| 105 | size++; |
| 106 | if(okey.dsize) free(okey.dptr); |
| 107 | okey=key; |
| 108 | } |
| 109 | dp->di_size = size; |
| 110 | } |
| 111 | return dp->di_size; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 114 | static PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 115 | dbm_subscript(dbmobject *dp, PyObject *key) |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 116 | { |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 117 | PyObject *v; |
| 118 | datum drec, krec; |
Fred Drake | da8d216 | 2000-02-07 17:19:41 +0000 | [diff] [blame] | 119 | |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 120 | if (!PyArg_Parse(key, "s#", &krec.dptr, &krec.dsize) ) |
| 121 | return NULL; |
| 122 | |
| 123 | if (dp->di_dbm == NULL) { |
| 124 | PyErr_SetString(DbmError, |
| 125 | "GDBM object has already been closed"); |
| 126 | return NULL; |
| 127 | } |
| 128 | drec = gdbm_fetch(dp->di_dbm, krec); |
| 129 | if (drec.dptr == 0) { |
Guido van Rossum | 6252e10 | 2007-05-23 20:51:02 +0000 | [diff] [blame] | 130 | PyErr_SetObject(PyExc_KeyError, key); |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 131 | return NULL; |
| 132 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 133 | v = PyBytes_FromStringAndSize(drec.dptr, drec.dsize); |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 134 | free(drec.dptr); |
| 135 | return v; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Georg Brandl | d9e833c | 2010-12-04 09:14:36 +0000 | [diff] [blame] | 138 | PyDoc_STRVAR(dbm_get__doc__, |
| 139 | "get(key[, default]) -> value\n\ |
| 140 | Get the value for key, or default if not present; if not given,\n\ |
| 141 | default is None."); |
| 142 | |
| 143 | static PyObject * |
| 144 | dbm_get(dbmobject *dp, PyObject *args) |
| 145 | { |
| 146 | PyObject *v, *res; |
| 147 | PyObject *def = Py_None; |
| 148 | |
| 149 | if (!PyArg_UnpackTuple(args, "get", 1, 2, &v, &def)) |
| 150 | return NULL; |
| 151 | res = dbm_subscript(dp, v); |
| 152 | if (res == NULL && PyErr_ExceptionMatches(PyExc_KeyError)) { |
| 153 | PyErr_Clear(); |
| 154 | Py_INCREF(def); |
| 155 | return def; |
| 156 | } |
| 157 | return res; |
| 158 | } |
| 159 | |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 160 | static int |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 161 | dbm_ass_sub(dbmobject *dp, PyObject *v, PyObject *w) |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 162 | { |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 163 | datum krec, drec; |
| 164 | |
| 165 | if (!PyArg_Parse(v, "s#", &krec.dptr, &krec.dsize) ) { |
| 166 | PyErr_SetString(PyExc_TypeError, |
Brett Cannon | 7317c1e | 2008-11-25 19:19:17 +0000 | [diff] [blame] | 167 | "gdbm mappings have bytes or string indices only"); |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 168 | return -1; |
| 169 | } |
| 170 | if (dp->di_dbm == NULL) { |
| 171 | PyErr_SetString(DbmError, |
Guido van Rossum | 6252e10 | 2007-05-23 20:51:02 +0000 | [diff] [blame] | 172 | "GDBM object has already been closed"); |
| 173 | return -1; |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 174 | } |
| 175 | dp->di_size = -1; |
| 176 | if (w == NULL) { |
| 177 | if (gdbm_delete(dp->di_dbm, krec) < 0) { |
Guido van Rossum | 6252e10 | 2007-05-23 20:51:02 +0000 | [diff] [blame] | 178 | PyErr_SetObject(PyExc_KeyError, v); |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 179 | return -1; |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 180 | } |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 181 | } |
| 182 | else { |
| 183 | if (!PyArg_Parse(w, "s#", &drec.dptr, &drec.dsize)) { |
| 184 | PyErr_SetString(PyExc_TypeError, |
Brett Cannon | 7317c1e | 2008-11-25 19:19:17 +0000 | [diff] [blame] | 185 | "gdbm mappings have byte or string elements only"); |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 186 | return -1; |
| 187 | } |
| 188 | errno = 0; |
| 189 | if (gdbm_store(dp->di_dbm, krec, drec, GDBM_REPLACE) < 0) { |
| 190 | if (errno != 0) |
| 191 | PyErr_SetFromErrno(DbmError); |
| 192 | else |
| 193 | PyErr_SetString(DbmError, |
| 194 | gdbm_strerror(gdbm_errno)); |
| 195 | return -1; |
| 196 | } |
| 197 | } |
| 198 | return 0; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Georg Brandl | d9e833c | 2010-12-04 09:14:36 +0000 | [diff] [blame] | 201 | PyDoc_STRVAR(dbm_setdefault__doc__, |
| 202 | "setdefault(key[, default]) -> value\n\ |
| 203 | Get value for key, or set it to default and return default if not present;\n\ |
| 204 | if not given, default is None."); |
| 205 | |
| 206 | static PyObject * |
| 207 | dbm_setdefault(dbmobject *dp, PyObject *args) |
| 208 | { |
| 209 | PyObject *v, *res; |
| 210 | PyObject *def = Py_None; |
| 211 | |
| 212 | if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &v, &def)) |
| 213 | return NULL; |
| 214 | res = dbm_subscript(dp, v); |
| 215 | if (res == NULL && PyErr_ExceptionMatches(PyExc_KeyError)) { |
| 216 | PyErr_Clear(); |
| 217 | if (dbm_ass_sub(dp, v, def) < 0) |
| 218 | return NULL; |
| 219 | return dbm_subscript(dp, v); |
| 220 | } |
| 221 | return res; |
| 222 | } |
| 223 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 224 | static PyMappingMethods dbm_as_mapping = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 225 | (lenfunc)dbm_length, /*mp_length*/ |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 226 | (binaryfunc)dbm_subscript, /*mp_subscript*/ |
| 227 | (objobjargproc)dbm_ass_sub, /*mp_ass_subscript*/ |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 228 | }; |
| 229 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 230 | PyDoc_STRVAR(dbm_close__doc__, |
| 231 | "close() -> None\n\ |
| 232 | Closes the database."); |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 233 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 234 | static PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 235 | dbm_close(dbmobject *dp, PyObject *unused) |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 236 | { |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 237 | if (dp->di_dbm) |
| 238 | gdbm_close(dp->di_dbm); |
| 239 | dp->di_dbm = NULL; |
| 240 | Py_INCREF(Py_None); |
| 241 | return Py_None; |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Guido van Rossum | 6252e10 | 2007-05-23 20:51:02 +0000 | [diff] [blame] | 244 | /* XXX Should return a set or a set view */ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 245 | PyDoc_STRVAR(dbm_keys__doc__, |
| 246 | "keys() -> list_of_keys\n\ |
| 247 | Get a list of all keys in the database."); |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 248 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 249 | static PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 250 | dbm_keys(dbmobject *dp, PyObject *unused) |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 251 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 252 | PyObject *v, *item; |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 253 | datum key, nextkey; |
| 254 | int err; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 255 | |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 256 | if (dp == NULL || !is_dbmobject(dp)) { |
| 257 | PyErr_BadInternalCall(); |
| 258 | return NULL; |
| 259 | } |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 260 | check_dbmobject_open(dp); |
Guido van Rossum | 3be7140 | 1996-07-21 02:32:44 +0000 | [diff] [blame] | 261 | |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 262 | v = PyList_New(0); |
| 263 | if (v == NULL) |
| 264 | return NULL; |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 265 | |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 266 | key = gdbm_firstkey(dp->di_dbm); |
| 267 | while (key.dptr) { |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 268 | item = PyBytes_FromStringAndSize(key.dptr, key.dsize); |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 269 | if (item == NULL) { |
| 270 | free(key.dptr); |
| 271 | Py_DECREF(v); |
| 272 | return NULL; |
| 273 | } |
| 274 | err = PyList_Append(v, item); |
| 275 | Py_DECREF(item); |
| 276 | if (err != 0) { |
| 277 | free(key.dptr); |
| 278 | Py_DECREF(v); |
| 279 | return NULL; |
| 280 | } |
| 281 | nextkey = gdbm_nextkey(dp->di_dbm, key); |
| 282 | free(key.dptr); |
| 283 | key = nextkey; |
| 284 | } |
| 285 | return v; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Guido van Rossum | 0da7e03 | 2006-08-19 23:18:48 +0000 | [diff] [blame] | 288 | static int |
| 289 | dbm_contains(PyObject *self, PyObject *arg) |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 290 | { |
Guido van Rossum | 0da7e03 | 2006-08-19 23:18:48 +0000 | [diff] [blame] | 291 | dbmobject *dp = (dbmobject *)self; |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 292 | datum key; |
Serhiy Storchaka | 7d6392c | 2013-10-25 00:06:52 +0300 | [diff] [blame] | 293 | Py_ssize_t size; |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 294 | |
Guido van Rossum | 0da7e03 | 2006-08-19 23:18:48 +0000 | [diff] [blame] | 295 | if ((dp)->di_dbm == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 296 | PyErr_SetString(DbmError, |
| 297 | "GDBM object has already been closed"); |
| 298 | return -1; |
Guido van Rossum | 0da7e03 | 2006-08-19 23:18:48 +0000 | [diff] [blame] | 299 | } |
Serhiy Storchaka | 7d6392c | 2013-10-25 00:06:52 +0300 | [diff] [blame] | 300 | if (PyUnicode_Check(arg)) { |
| 301 | key.dptr = PyUnicode_AsUTF8AndSize(arg, &size); |
| 302 | key.dsize = size; |
| 303 | if (key.dptr == NULL) |
| 304 | return -1; |
| 305 | } |
| 306 | else if (!PyBytes_Check(arg)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 307 | PyErr_Format(PyExc_TypeError, |
Serhiy Storchaka | 7d6392c | 2013-10-25 00:06:52 +0300 | [diff] [blame] | 308 | "gdbm key must be bytes or string, not %.100s", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 309 | arg->ob_type->tp_name); |
| 310 | return -1; |
Guido van Rossum | 0da7e03 | 2006-08-19 23:18:48 +0000 | [diff] [blame] | 311 | } |
Serhiy Storchaka | 7d6392c | 2013-10-25 00:06:52 +0300 | [diff] [blame] | 312 | else { |
| 313 | key.dptr = PyBytes_AS_STRING(arg); |
| 314 | key.dsize = PyBytes_GET_SIZE(arg); |
| 315 | } |
Guido van Rossum | 0da7e03 | 2006-08-19 23:18:48 +0000 | [diff] [blame] | 316 | return gdbm_exists(dp->di_dbm, key); |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Guido van Rossum | 0da7e03 | 2006-08-19 23:18:48 +0000 | [diff] [blame] | 319 | static PySequenceMethods dbm_as_sequence = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 320 | 0, /* sq_length */ |
| 321 | 0, /* sq_concat */ |
| 322 | 0, /* sq_repeat */ |
| 323 | 0, /* sq_item */ |
| 324 | 0, /* sq_slice */ |
| 325 | 0, /* sq_ass_item */ |
| 326 | 0, /* sq_ass_slice */ |
| 327 | dbm_contains, /* sq_contains */ |
| 328 | 0, /* sq_inplace_concat */ |
| 329 | 0, /* sq_inplace_repeat */ |
Guido van Rossum | 0da7e03 | 2006-08-19 23:18:48 +0000 | [diff] [blame] | 330 | }; |
| 331 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 332 | PyDoc_STRVAR(dbm_firstkey__doc__, |
| 333 | "firstkey() -> key\n\ |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 334 | It's possible to loop over every key in the database using this method\n\ |
| 335 | and the nextkey() method. The traversal is ordered by GDBM's internal\n\ |
| 336 | hash values, and won't be sorted by the key values. This method\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 337 | returns the starting key."); |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 338 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 339 | static PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 340 | dbm_firstkey(dbmobject *dp, PyObject *unused) |
Guido van Rossum | fbd30e9 | 1995-03-16 16:07:34 +0000 | [diff] [blame] | 341 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 342 | PyObject *v; |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 343 | datum key; |
Guido van Rossum | fbd30e9 | 1995-03-16 16:07:34 +0000 | [diff] [blame] | 344 | |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 345 | check_dbmobject_open(dp); |
| 346 | key = gdbm_firstkey(dp->di_dbm); |
| 347 | if (key.dptr) { |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 348 | v = PyBytes_FromStringAndSize(key.dptr, key.dsize); |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 349 | free(key.dptr); |
| 350 | return v; |
| 351 | } |
| 352 | else { |
| 353 | Py_INCREF(Py_None); |
| 354 | return Py_None; |
| 355 | } |
Guido van Rossum | fbd30e9 | 1995-03-16 16:07:34 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 358 | PyDoc_STRVAR(dbm_nextkey__doc__, |
| 359 | "nextkey(key) -> next_key\n\ |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 360 | Returns the key that follows key in the traversal.\n\ |
| 361 | The following code prints every key in the database db, without having\n\ |
| 362 | to create a list in memory that contains them all:\n\ |
| 363 | \n\ |
| 364 | k = db.firstkey()\n\ |
| 365 | while k != None:\n\ |
| 366 | print k\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 367 | k = db.nextkey(k)"); |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 368 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 369 | static PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 370 | dbm_nextkey(dbmobject *dp, PyObject *args) |
Guido van Rossum | fbd30e9 | 1995-03-16 16:07:34 +0000 | [diff] [blame] | 371 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 372 | PyObject *v; |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 373 | datum key, nextkey; |
Guido van Rossum | fbd30e9 | 1995-03-16 16:07:34 +0000 | [diff] [blame] | 374 | |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 375 | if (!PyArg_ParseTuple(args, "s#:nextkey", &key.dptr, &key.dsize)) |
| 376 | return NULL; |
| 377 | check_dbmobject_open(dp); |
| 378 | nextkey = gdbm_nextkey(dp->di_dbm, key); |
| 379 | if (nextkey.dptr) { |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 380 | v = PyBytes_FromStringAndSize(nextkey.dptr, nextkey.dsize); |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 381 | free(nextkey.dptr); |
| 382 | return v; |
| 383 | } |
| 384 | else { |
| 385 | Py_INCREF(Py_None); |
| 386 | return Py_None; |
| 387 | } |
Guido van Rossum | fbd30e9 | 1995-03-16 16:07:34 +0000 | [diff] [blame] | 388 | } |
| 389 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 390 | PyDoc_STRVAR(dbm_reorganize__doc__, |
| 391 | "reorganize() -> None\n\ |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 392 | If you have carried out a lot of deletions and would like to shrink\n\ |
| 393 | the space used by the GDBM file, this routine will reorganize the\n\ |
| 394 | database. GDBM will not shorten the length of a database file except\n\ |
| 395 | by using this reorganization; otherwise, deleted file space will be\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 396 | kept and reused as new (key,value) pairs are added."); |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 397 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 398 | static PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 399 | dbm_reorganize(dbmobject *dp, PyObject *unused) |
Guido van Rossum | fbd30e9 | 1995-03-16 16:07:34 +0000 | [diff] [blame] | 400 | { |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 401 | check_dbmobject_open(dp); |
| 402 | errno = 0; |
| 403 | if (gdbm_reorganize(dp->di_dbm) < 0) { |
| 404 | if (errno != 0) |
| 405 | PyErr_SetFromErrno(DbmError); |
| 406 | else |
| 407 | PyErr_SetString(DbmError, gdbm_strerror(gdbm_errno)); |
| 408 | return NULL; |
| 409 | } |
| 410 | Py_INCREF(Py_None); |
| 411 | return Py_None; |
Guido van Rossum | fbd30e9 | 1995-03-16 16:07:34 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 414 | PyDoc_STRVAR(dbm_sync__doc__, |
| 415 | "sync() -> None\n\ |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 416 | When the database has been opened in fast mode, this method forces\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 417 | any unwritten data to be written to the disk."); |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 418 | |
Roger E. Masse | e5a9c8f | 1997-03-25 17:39:56 +0000 | [diff] [blame] | 419 | static PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 420 | dbm_sync(dbmobject *dp, PyObject *unused) |
Roger E. Masse | e5a9c8f | 1997-03-25 17:39:56 +0000 | [diff] [blame] | 421 | { |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 422 | check_dbmobject_open(dp); |
| 423 | gdbm_sync(dp->di_dbm); |
| 424 | Py_INCREF(Py_None); |
| 425 | return Py_None; |
Roger E. Masse | e5a9c8f | 1997-03-25 17:39:56 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Nick Coghlan | c610aba | 2013-11-17 15:59:51 +1000 | [diff] [blame] | 428 | static PyObject * |
| 429 | dbm__enter__(PyObject *self, PyObject *args) |
| 430 | { |
| 431 | Py_INCREF(self); |
| 432 | return self; |
| 433 | } |
| 434 | |
| 435 | static PyObject * |
| 436 | dbm__exit__(PyObject *self, PyObject *args) |
| 437 | { |
| 438 | _Py_IDENTIFIER(close); |
| 439 | return _PyObject_CallMethodId(self, &PyId_close, NULL); |
| 440 | } |
| 441 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 442 | static PyMethodDef dbm_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 443 | {"close", (PyCFunction)dbm_close, METH_NOARGS, dbm_close__doc__}, |
| 444 | {"keys", (PyCFunction)dbm_keys, METH_NOARGS, dbm_keys__doc__}, |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 445 | {"firstkey", (PyCFunction)dbm_firstkey,METH_NOARGS, dbm_firstkey__doc__}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 446 | {"nextkey", (PyCFunction)dbm_nextkey, METH_VARARGS, dbm_nextkey__doc__}, |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 447 | {"reorganize",(PyCFunction)dbm_reorganize,METH_NOARGS, dbm_reorganize__doc__}, |
| 448 | {"sync", (PyCFunction)dbm_sync, METH_NOARGS, dbm_sync__doc__}, |
Georg Brandl | d9e833c | 2010-12-04 09:14:36 +0000 | [diff] [blame] | 449 | {"get", (PyCFunction)dbm_get, METH_VARARGS, dbm_get__doc__}, |
| 450 | {"setdefault",(PyCFunction)dbm_setdefault,METH_VARARGS, dbm_setdefault__doc__}, |
Nick Coghlan | c610aba | 2013-11-17 15:59:51 +1000 | [diff] [blame] | 451 | {"__enter__", dbm__enter__, METH_NOARGS, NULL}, |
| 452 | {"__exit__", dbm__exit__, METH_VARARGS, NULL}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 453 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 454 | }; |
| 455 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 456 | static PyTypeObject Dbmtype = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 457 | PyVarObject_HEAD_INIT(0, 0) |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 458 | "_gdbm.gdbm", |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 459 | sizeof(dbmobject), |
| 460 | 0, |
| 461 | (destructor)dbm_dealloc, /*tp_dealloc*/ |
| 462 | 0, /*tp_print*/ |
Amaury Forgeot d'Arc | 1f900f1 | 2008-07-02 22:38:47 +0000 | [diff] [blame] | 463 | 0, /*tp_getattr*/ |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 464 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 465 | 0, /*tp_reserved*/ |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 466 | 0, /*tp_repr*/ |
| 467 | 0, /*tp_as_number*/ |
Guido van Rossum | 0da7e03 | 2006-08-19 23:18:48 +0000 | [diff] [blame] | 468 | &dbm_as_sequence, /*tp_as_sequence*/ |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 469 | &dbm_as_mapping, /*tp_as_mapping*/ |
| 470 | 0, /*tp_hash*/ |
| 471 | 0, /*tp_call*/ |
| 472 | 0, /*tp_str*/ |
| 473 | 0, /*tp_getattro*/ |
| 474 | 0, /*tp_setattro*/ |
| 475 | 0, /*tp_as_buffer*/ |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 476 | Py_TPFLAGS_DEFAULT, /*tp_xxx4*/ |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 477 | gdbm_object__doc__, /*tp_doc*/ |
Amaury Forgeot d'Arc | 1f900f1 | 2008-07-02 22:38:47 +0000 | [diff] [blame] | 478 | 0, /*tp_traverse*/ |
| 479 | 0, /*tp_clear*/ |
| 480 | 0, /*tp_richcompare*/ |
| 481 | 0, /*tp_weaklistoffset*/ |
| 482 | 0, /*tp_iter*/ |
| 483 | 0, /*tp_iternext*/ |
| 484 | dbm_methods, /*tp_methods*/ |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 485 | }; |
| 486 | |
| 487 | /* ----------------------------------------------------------------- */ |
| 488 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 489 | PyDoc_STRVAR(dbmopen__doc__, |
| 490 | "open(filename, [flags, [mode]]) -> dbm_object\n\ |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 491 | Open a dbm database and return a dbm object. The filename argument is\n\ |
| 492 | the name of the database file.\n\ |
| 493 | \n\ |
Neil Schemenauer | e9e860f | 2000-12-17 07:14:13 +0000 | [diff] [blame] | 494 | The optional flags argument can be 'r' (to open an existing database\n\ |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 495 | for reading only -- default), 'w' (to open an existing database for\n\ |
| 496 | reading and writing), 'c' (which creates the database if it doesn't\n\ |
| 497 | exist), or 'n' (which always creates a new empty database).\n\ |
| 498 | \n\ |
Neil Schemenauer | e9e860f | 2000-12-17 07:14:13 +0000 | [diff] [blame] | 499 | Some versions of gdbm support additional flags which must be\n\ |
| 500 | appended to one of the flags described above. The module constant\n\ |
| 501 | 'open_flags' is a string of valid additional flags. The 'f' flag\n\ |
| 502 | opens the database in fast mode; altered data will not automatically\n\ |
| 503 | be written to the disk after every change. This results in faster\n\ |
| 504 | writes to the database, but may result in an inconsistent database\n\ |
| 505 | if the program crashes while the database is still open. Use the\n\ |
| 506 | sync() method to force any unwritten data to be written to the disk.\n\ |
| 507 | The 's' flag causes all database operations to be synchronized to\n\ |
| 508 | disk. The 'u' flag disables locking of the database file.\n\ |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 509 | \n\ |
| 510 | The optional mode argument is the Unix mode of the file, used only\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 511 | when the database has to be created. It defaults to octal 0666. "); |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 512 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 513 | static PyObject * |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 514 | dbmopen(PyObject *self, PyObject *args) |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 515 | { |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 516 | char *name; |
Andrew M. Kuchling | ea271f2 | 2004-07-07 14:19:09 +0000 | [diff] [blame] | 517 | char *flags = "r"; |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 518 | int iflags; |
| 519 | int mode = 0666; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 520 | |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 521 | if (!PyArg_ParseTuple(args, "s|si:open", &name, &flags, &mode)) |
| 522 | return NULL; |
| 523 | switch (flags[0]) { |
| 524 | case 'r': |
| 525 | iflags = GDBM_READER; |
| 526 | break; |
| 527 | case 'w': |
| 528 | iflags = GDBM_WRITER; |
| 529 | break; |
| 530 | case 'c': |
| 531 | iflags = GDBM_WRCREAT; |
| 532 | break; |
| 533 | case 'n': |
| 534 | iflags = GDBM_NEWDB; |
| 535 | break; |
| 536 | default: |
| 537 | PyErr_SetString(DbmError, |
Neil Schemenauer | e9e860f | 2000-12-17 07:14:13 +0000 | [diff] [blame] | 538 | "First flag must be one of 'r', 'w', 'c' or 'n'"); |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 539 | return NULL; |
| 540 | } |
Neil Schemenauer | e9e860f | 2000-12-17 07:14:13 +0000 | [diff] [blame] | 541 | for (flags++; *flags != '\0'; flags++) { |
| 542 | char buf[40]; |
| 543 | switch (*flags) { |
| 544 | #ifdef GDBM_FAST |
| 545 | case 'f': |
| 546 | iflags |= GDBM_FAST; |
| 547 | break; |
| 548 | #endif |
| 549 | #ifdef GDBM_SYNC |
| 550 | case 's': |
| 551 | iflags |= GDBM_SYNC; |
| 552 | break; |
| 553 | #endif |
| 554 | #ifdef GDBM_NOLOCK |
| 555 | case 'u': |
| 556 | iflags |= GDBM_NOLOCK; |
| 557 | break; |
| 558 | #endif |
| 559 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 560 | PyOS_snprintf(buf, sizeof(buf), "Flag '%c' is not supported.", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 561 | *flags); |
Neil Schemenauer | e9e860f | 2000-12-17 07:14:13 +0000 | [diff] [blame] | 562 | PyErr_SetString(DbmError, buf); |
| 563 | return NULL; |
| 564 | } |
| 565 | } |
| 566 | |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 567 | return newdbmobject(name, iflags, mode); |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 568 | } |
| 569 | |
Neil Schemenauer | e9e860f | 2000-12-17 07:14:13 +0000 | [diff] [blame] | 570 | static char dbmmodule_open_flags[] = "rwcn" |
| 571 | #ifdef GDBM_FAST |
| 572 | "f" |
| 573 | #endif |
| 574 | #ifdef GDBM_SYNC |
| 575 | "s" |
| 576 | #endif |
| 577 | #ifdef GDBM_NOLOCK |
| 578 | "u" |
| 579 | #endif |
| 580 | ; |
| 581 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 582 | static PyMethodDef dbmmodule_methods[] = { |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 583 | { "open", (PyCFunction)dbmopen, METH_VARARGS, dbmopen__doc__}, |
| 584 | { 0, 0 }, |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 585 | }; |
| 586 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 587 | |
| 588 | static struct PyModuleDef _gdbmmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 589 | PyModuleDef_HEAD_INIT, |
| 590 | "_gdbm", |
| 591 | gdbmmodule__doc__, |
| 592 | -1, |
| 593 | dbmmodule_methods, |
| 594 | NULL, |
| 595 | NULL, |
| 596 | NULL, |
| 597 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 598 | }; |
| 599 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 600 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 601 | PyInit__gdbm(void) { |
Martin v. Löwis | 00ff10c | 2001-11-11 14:24:05 +0000 | [diff] [blame] | 602 | PyObject *m, *d, *s; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 603 | |
Guido van Rossum | 0da7e03 | 2006-08-19 23:18:48 +0000 | [diff] [blame] | 604 | if (PyType_Ready(&Dbmtype) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 605 | return NULL; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 606 | m = PyModule_Create(&_gdbmmodule); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 607 | if (m == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 608 | return NULL; |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 609 | d = PyModule_GetDict(m); |
Georg Brandl | b17acad | 2008-05-28 08:43:17 +0000 | [diff] [blame] | 610 | DbmError = PyErr_NewException("_gdbm.error", PyExc_IOError, NULL); |
Neil Schemenauer | e9e860f | 2000-12-17 07:14:13 +0000 | [diff] [blame] | 611 | if (DbmError != NULL) { |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 612 | PyDict_SetItemString(d, "error", DbmError); |
Martin v. Löwis | 7ac9712 | 2007-08-11 19:28:53 +0000 | [diff] [blame] | 613 | s = PyUnicode_FromString(dbmmodule_open_flags); |
Martin v. Löwis | 00ff10c | 2001-11-11 14:24:05 +0000 | [diff] [blame] | 614 | PyDict_SetItemString(d, "open_flags", s); |
| 615 | Py_DECREF(s); |
Neil Schemenauer | e9e860f | 2000-12-17 07:14:13 +0000 | [diff] [blame] | 616 | } |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 617 | return m; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 618 | } |