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 | |
Inada Naoki | c5a216e | 2019-03-20 19:01:55 +0900 | [diff] [blame] | 6 | #define PY_SSIZE_T_CLEAN |
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 | |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 19 | /*[clinic input] |
| 20 | module _gdbm |
| 21 | class _gdbm.gdbm "dbmobject *" "&Dbmtype" |
| 22 | [clinic start generated code]*/ |
| 23 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=113927c6170729b2]*/ |
| 24 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 25 | PyDoc_STRVAR(gdbmmodule__doc__, |
| 26 | "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] | 27 | \n\ |
| 28 | This module is quite similar to the dbm module, but uses GDBM instead to\n\ |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 29 | provide some additional functionality. Please note that the file formats\n\ |
| 30 | created by GDBM and dbm are incompatible.\n\ |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 31 | \n\ |
| 32 | GDBM objects behave like mappings (dictionaries), except that keys and\n\ |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 33 | values are always immutable bytes-like objects or strings. Printing\n\ |
| 34 | a GDBM object doesn't print the keys and values, and the items() and\n\ |
| 35 | values() methods are not supported."); |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 36 | |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 37 | typedef struct { |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 38 | PyObject_HEAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 39 | int di_size; /* -1 means recompute */ |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 40 | GDBM_FILE di_dbm; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +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 | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 44 | |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 45 | #include "clinic/_gdbmmodule.c.h" |
| 46 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 47 | #define is_dbmobject(v) (Py_TYPE(v) == &Dbmtype) |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 48 | #define check_dbmobject_open(v) if ((v)->di_dbm == NULL) \ |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 49 | { PyErr_SetString(DbmError, "GDBM object has already been closed"); \ |
| 50 | return NULL; } |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 51 | |
| 52 | |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 53 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 54 | static PyObject *DbmError; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 55 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 56 | PyDoc_STRVAR(gdbm_object__doc__, |
| 57 | "This object represents a GDBM database.\n\ |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 58 | GDBM objects behave like mappings (dictionaries), except that keys and\n\ |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 59 | values are always immutable bytes-like objects or strings. Printing\n\ |
| 60 | a GDBM object doesn't print the keys and values, and the items() and\n\ |
| 61 | values() methods are not supported.\n\ |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 62 | \n\ |
| 63 | GDBM objects also support additional operations such as firstkey,\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 64 | nextkey, reorganize, and sync."); |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 65 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 66 | static PyObject * |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 67 | newdbmobject(const char *file, int flags, int mode) |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 68 | { |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 69 | dbmobject *dp; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 70 | |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 71 | dp = PyObject_New(dbmobject, &Dbmtype); |
| 72 | if (dp == NULL) |
| 73 | return NULL; |
| 74 | dp->di_size = -1; |
| 75 | errno = 0; |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 76 | if ((dp->di_dbm = gdbm_open((char *)file, 0, flags, mode, NULL)) == 0) { |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 77 | if (errno != 0) |
Zsolt Cserna | 9df346b | 2018-09-27 21:54:34 +0200 | [diff] [blame] | 78 | PyErr_SetFromErrnoWithFilename(DbmError, file); |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 79 | else |
| 80 | PyErr_SetString(DbmError, gdbm_strerror(gdbm_errno)); |
| 81 | Py_DECREF(dp); |
| 82 | return NULL; |
| 83 | } |
| 84 | return (PyObject *)dp; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /* Methods */ |
| 88 | |
| 89 | static void |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 90 | dbm_dealloc(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) |
| 93 | gdbm_close(dp->di_dbm); |
| 94 | PyObject_Del(dp); |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 97 | static Py_ssize_t |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 98 | dbm_length(dbmobject *dp) |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 99 | { |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 100 | if (dp->di_dbm == NULL) { |
Guido van Rossum | 6252e10 | 2007-05-23 20:51:02 +0000 | [diff] [blame] | 101 | PyErr_SetString(DbmError, "GDBM object has already been closed"); |
| 102 | return -1; |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 103 | } |
| 104 | if (dp->di_size < 0) { |
| 105 | datum key,okey; |
| 106 | int size; |
| 107 | okey.dsize=0; |
Thomas Wouters | a5fa2a8 | 2006-03-01 22:54:36 +0000 | [diff] [blame] | 108 | okey.dptr=NULL; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 109 | |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 110 | size = 0; |
| 111 | for (key=gdbm_firstkey(dp->di_dbm); key.dptr; |
| 112 | key = gdbm_nextkey(dp->di_dbm,okey)) { |
| 113 | size++; |
| 114 | if(okey.dsize) free(okey.dptr); |
| 115 | okey=key; |
| 116 | } |
| 117 | dp->di_size = size; |
| 118 | } |
| 119 | return dp->di_size; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Inada Naoki | c5a216e | 2019-03-20 19:01:55 +0900 | [diff] [blame] | 122 | // Wrapper function for PyArg_Parse(o, "s#", &d.dptr, &d.size). |
| 123 | // This function is needed to support PY_SSIZE_T_CLEAN. |
| 124 | // Return 1 on success, same to PyArg_Parse(). |
| 125 | static int |
| 126 | parse_datum(PyObject *o, datum *d, const char *failmsg) |
| 127 | { |
| 128 | Py_ssize_t size; |
| 129 | if (!PyArg_Parse(o, "s#", &d->dptr, &size)) { |
| 130 | if (failmsg != NULL) { |
| 131 | PyErr_SetString(PyExc_TypeError, failmsg); |
| 132 | } |
| 133 | return 0; |
| 134 | } |
| 135 | if (INT_MAX < size) { |
| 136 | PyErr_SetString(PyExc_OverflowError, "size does not fit in an int"); |
| 137 | return 0; |
| 138 | } |
| 139 | d->dsize = size; |
| 140 | return 1; |
| 141 | } |
| 142 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 143 | static PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 144 | dbm_subscript(dbmobject *dp, PyObject *key) |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 145 | { |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 146 | PyObject *v; |
| 147 | datum drec, krec; |
Fred Drake | da8d216 | 2000-02-07 17:19:41 +0000 | [diff] [blame] | 148 | |
Inada Naoki | c5a216e | 2019-03-20 19:01:55 +0900 | [diff] [blame] | 149 | if (!parse_datum(key, &krec, NULL)) { |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 150 | return NULL; |
Inada Naoki | c5a216e | 2019-03-20 19:01:55 +0900 | [diff] [blame] | 151 | } |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 152 | if (dp->di_dbm == NULL) { |
| 153 | PyErr_SetString(DbmError, |
| 154 | "GDBM object has already been closed"); |
| 155 | return NULL; |
| 156 | } |
| 157 | drec = gdbm_fetch(dp->di_dbm, krec); |
| 158 | if (drec.dptr == 0) { |
Guido van Rossum | 6252e10 | 2007-05-23 20:51:02 +0000 | [diff] [blame] | 159 | PyErr_SetObject(PyExc_KeyError, key); |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 160 | return NULL; |
| 161 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 162 | v = PyBytes_FromStringAndSize(drec.dptr, drec.dsize); |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 163 | free(drec.dptr); |
| 164 | return v; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 167 | /*[clinic input] |
| 168 | _gdbm.gdbm.get |
| 169 | |
| 170 | key: object |
| 171 | default: object = None |
| 172 | / |
| 173 | |
| 174 | Get the value for key, or default if not present. |
| 175 | [clinic start generated code]*/ |
Georg Brandl | d9e833c | 2010-12-04 09:14:36 +0000 | [diff] [blame] | 176 | |
| 177 | static PyObject * |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 178 | _gdbm_gdbm_get_impl(dbmobject *self, PyObject *key, PyObject *default_value) |
| 179 | /*[clinic end generated code: output=19b7c585ad4f554a input=a9c20423f34c17b6]*/ |
Georg Brandl | d9e833c | 2010-12-04 09:14:36 +0000 | [diff] [blame] | 180 | { |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 181 | PyObject *res; |
Georg Brandl | d9e833c | 2010-12-04 09:14:36 +0000 | [diff] [blame] | 182 | |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 183 | res = dbm_subscript(self, key); |
Georg Brandl | d9e833c | 2010-12-04 09:14:36 +0000 | [diff] [blame] | 184 | if (res == NULL && PyErr_ExceptionMatches(PyExc_KeyError)) { |
| 185 | PyErr_Clear(); |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 186 | Py_INCREF(default_value); |
| 187 | return default_value; |
Georg Brandl | d9e833c | 2010-12-04 09:14:36 +0000 | [diff] [blame] | 188 | } |
| 189 | return res; |
| 190 | } |
| 191 | |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 192 | static int |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 193 | dbm_ass_sub(dbmobject *dp, PyObject *v, PyObject *w) |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 194 | { |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 195 | datum krec, drec; |
Inada Naoki | c5a216e | 2019-03-20 19:01:55 +0900 | [diff] [blame] | 196 | const char *failmsg = "gdbm mappings have bytes or string indices only"; |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 197 | |
Inada Naoki | c5a216e | 2019-03-20 19:01:55 +0900 | [diff] [blame] | 198 | if (!parse_datum(v, &krec, failmsg)) { |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 199 | return -1; |
| 200 | } |
| 201 | if (dp->di_dbm == NULL) { |
| 202 | PyErr_SetString(DbmError, |
Guido van Rossum | 6252e10 | 2007-05-23 20:51:02 +0000 | [diff] [blame] | 203 | "GDBM object has already been closed"); |
| 204 | return -1; |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 205 | } |
| 206 | dp->di_size = -1; |
| 207 | if (w == NULL) { |
| 208 | if (gdbm_delete(dp->di_dbm, krec) < 0) { |
Xiang Zhang | 4fb0b8b | 2018-12-12 20:46:55 +0800 | [diff] [blame] | 209 | if (gdbm_errno == GDBM_ITEM_NOT_FOUND) { |
| 210 | PyErr_SetObject(PyExc_KeyError, v); |
| 211 | } |
| 212 | else { |
| 213 | PyErr_SetString(DbmError, gdbm_strerror(gdbm_errno)); |
| 214 | } |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 215 | return -1; |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 216 | } |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 217 | } |
| 218 | else { |
Inada Naoki | c5a216e | 2019-03-20 19:01:55 +0900 | [diff] [blame] | 219 | if (!parse_datum(w, &drec, failmsg)) { |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 220 | return -1; |
| 221 | } |
| 222 | errno = 0; |
| 223 | if (gdbm_store(dp->di_dbm, krec, drec, GDBM_REPLACE) < 0) { |
| 224 | if (errno != 0) |
| 225 | PyErr_SetFromErrno(DbmError); |
| 226 | else |
| 227 | PyErr_SetString(DbmError, |
| 228 | gdbm_strerror(gdbm_errno)); |
| 229 | return -1; |
| 230 | } |
| 231 | } |
| 232 | return 0; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 235 | /*[clinic input] |
| 236 | _gdbm.gdbm.setdefault |
| 237 | |
| 238 | key: object |
| 239 | default: object = None |
| 240 | / |
| 241 | |
| 242 | Get value for key, or set it to default and return default if not present. |
| 243 | [clinic start generated code]*/ |
Georg Brandl | d9e833c | 2010-12-04 09:14:36 +0000 | [diff] [blame] | 244 | |
| 245 | static PyObject * |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 246 | _gdbm_gdbm_setdefault_impl(dbmobject *self, PyObject *key, |
| 247 | PyObject *default_value) |
| 248 | /*[clinic end generated code: output=88760ee520329012 input=0db46b69e9680171]*/ |
Georg Brandl | d9e833c | 2010-12-04 09:14:36 +0000 | [diff] [blame] | 249 | { |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 250 | PyObject *res; |
Georg Brandl | d9e833c | 2010-12-04 09:14:36 +0000 | [diff] [blame] | 251 | |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 252 | res = dbm_subscript(self, key); |
Georg Brandl | d9e833c | 2010-12-04 09:14:36 +0000 | [diff] [blame] | 253 | if (res == NULL && PyErr_ExceptionMatches(PyExc_KeyError)) { |
| 254 | PyErr_Clear(); |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 255 | if (dbm_ass_sub(self, key, default_value) < 0) |
Georg Brandl | d9e833c | 2010-12-04 09:14:36 +0000 | [diff] [blame] | 256 | return NULL; |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 257 | return dbm_subscript(self, key); |
Georg Brandl | d9e833c | 2010-12-04 09:14:36 +0000 | [diff] [blame] | 258 | } |
| 259 | return res; |
| 260 | } |
| 261 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 262 | static PyMappingMethods dbm_as_mapping = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 263 | (lenfunc)dbm_length, /*mp_length*/ |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 264 | (binaryfunc)dbm_subscript, /*mp_subscript*/ |
| 265 | (objobjargproc)dbm_ass_sub, /*mp_ass_subscript*/ |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 266 | }; |
| 267 | |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 268 | /*[clinic input] |
| 269 | _gdbm.gdbm.close |
| 270 | |
| 271 | Close the database. |
| 272 | [clinic start generated code]*/ |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 273 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 274 | static PyObject * |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 275 | _gdbm_gdbm_close_impl(dbmobject *self) |
| 276 | /*[clinic end generated code: output=23512a594598b563 input=0a203447379b45fd]*/ |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 277 | { |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 278 | if (self->di_dbm) |
| 279 | gdbm_close(self->di_dbm); |
| 280 | self->di_dbm = NULL; |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 281 | Py_RETURN_NONE; |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Guido van Rossum | 6252e10 | 2007-05-23 20:51:02 +0000 | [diff] [blame] | 284 | /* XXX Should return a set or a set view */ |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 285 | /*[clinic input] |
| 286 | _gdbm.gdbm.keys |
| 287 | |
| 288 | Get a list of all keys in the database. |
| 289 | [clinic start generated code]*/ |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 290 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 291 | static PyObject * |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 292 | _gdbm_gdbm_keys_impl(dbmobject *self) |
| 293 | /*[clinic end generated code: output=cb4b1776c3645dcc input=1832ee0a3132cfaf]*/ |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 294 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 295 | PyObject *v, *item; |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 296 | datum key, nextkey; |
| 297 | int err; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 298 | |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 299 | if (self == NULL || !is_dbmobject(self)) { |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 300 | PyErr_BadInternalCall(); |
| 301 | return NULL; |
| 302 | } |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 303 | check_dbmobject_open(self); |
Guido van Rossum | 3be7140 | 1996-07-21 02:32:44 +0000 | [diff] [blame] | 304 | |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 305 | v = PyList_New(0); |
| 306 | if (v == NULL) |
| 307 | return NULL; |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 308 | |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 309 | key = gdbm_firstkey(self->di_dbm); |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 310 | while (key.dptr) { |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 311 | item = PyBytes_FromStringAndSize(key.dptr, key.dsize); |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 312 | if (item == NULL) { |
| 313 | free(key.dptr); |
| 314 | Py_DECREF(v); |
| 315 | return NULL; |
| 316 | } |
| 317 | err = PyList_Append(v, item); |
| 318 | Py_DECREF(item); |
| 319 | if (err != 0) { |
| 320 | free(key.dptr); |
| 321 | Py_DECREF(v); |
| 322 | return NULL; |
| 323 | } |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 324 | nextkey = gdbm_nextkey(self->di_dbm, key); |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 325 | free(key.dptr); |
| 326 | key = nextkey; |
| 327 | } |
| 328 | return v; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Guido van Rossum | 0da7e03 | 2006-08-19 23:18:48 +0000 | [diff] [blame] | 331 | static int |
| 332 | dbm_contains(PyObject *self, PyObject *arg) |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 333 | { |
Guido van Rossum | 0da7e03 | 2006-08-19 23:18:48 +0000 | [diff] [blame] | 334 | dbmobject *dp = (dbmobject *)self; |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 335 | datum key; |
Serhiy Storchaka | 7d6392c | 2013-10-25 00:06:52 +0300 | [diff] [blame] | 336 | Py_ssize_t size; |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 337 | |
Guido van Rossum | 0da7e03 | 2006-08-19 23:18:48 +0000 | [diff] [blame] | 338 | if ((dp)->di_dbm == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 339 | PyErr_SetString(DbmError, |
| 340 | "GDBM object has already been closed"); |
| 341 | return -1; |
Guido van Rossum | 0da7e03 | 2006-08-19 23:18:48 +0000 | [diff] [blame] | 342 | } |
Serhiy Storchaka | 7d6392c | 2013-10-25 00:06:52 +0300 | [diff] [blame] | 343 | if (PyUnicode_Check(arg)) { |
Serhiy Storchaka | 2a404b6 | 2017-01-22 23:07:07 +0200 | [diff] [blame] | 344 | key.dptr = (char *)PyUnicode_AsUTF8AndSize(arg, &size); |
Serhiy Storchaka | 7d6392c | 2013-10-25 00:06:52 +0300 | [diff] [blame] | 345 | key.dsize = size; |
| 346 | if (key.dptr == NULL) |
| 347 | return -1; |
| 348 | } |
| 349 | else if (!PyBytes_Check(arg)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 350 | PyErr_Format(PyExc_TypeError, |
Serhiy Storchaka | 7d6392c | 2013-10-25 00:06:52 +0300 | [diff] [blame] | 351 | "gdbm key must be bytes or string, not %.100s", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 352 | arg->ob_type->tp_name); |
| 353 | return -1; |
Guido van Rossum | 0da7e03 | 2006-08-19 23:18:48 +0000 | [diff] [blame] | 354 | } |
Serhiy Storchaka | 7d6392c | 2013-10-25 00:06:52 +0300 | [diff] [blame] | 355 | else { |
| 356 | key.dptr = PyBytes_AS_STRING(arg); |
| 357 | key.dsize = PyBytes_GET_SIZE(arg); |
| 358 | } |
Guido van Rossum | 0da7e03 | 2006-08-19 23:18:48 +0000 | [diff] [blame] | 359 | return gdbm_exists(dp->di_dbm, key); |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 360 | } |
| 361 | |
Guido van Rossum | 0da7e03 | 2006-08-19 23:18:48 +0000 | [diff] [blame] | 362 | static PySequenceMethods dbm_as_sequence = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 363 | 0, /* sq_length */ |
| 364 | 0, /* sq_concat */ |
| 365 | 0, /* sq_repeat */ |
| 366 | 0, /* sq_item */ |
| 367 | 0, /* sq_slice */ |
| 368 | 0, /* sq_ass_item */ |
| 369 | 0, /* sq_ass_slice */ |
| 370 | dbm_contains, /* sq_contains */ |
| 371 | 0, /* sq_inplace_concat */ |
| 372 | 0, /* sq_inplace_repeat */ |
Guido van Rossum | 0da7e03 | 2006-08-19 23:18:48 +0000 | [diff] [blame] | 373 | }; |
| 374 | |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 375 | /*[clinic input] |
| 376 | _gdbm.gdbm.firstkey |
| 377 | |
| 378 | Return the starting key for the traversal. |
| 379 | |
| 380 | It's possible to loop over every key in the database using this method |
| 381 | and the nextkey() method. The traversal is ordered by GDBM's internal |
| 382 | hash values, and won't be sorted by the key values. |
| 383 | [clinic start generated code]*/ |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 384 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 385 | static PyObject * |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 386 | _gdbm_gdbm_firstkey_impl(dbmobject *self) |
| 387 | /*[clinic end generated code: output=9ff85628d84b65d2 input=0dbd6a335d69bba0]*/ |
Guido van Rossum | fbd30e9 | 1995-03-16 16:07:34 +0000 | [diff] [blame] | 388 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 389 | PyObject *v; |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 390 | datum key; |
Guido van Rossum | fbd30e9 | 1995-03-16 16:07:34 +0000 | [diff] [blame] | 391 | |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 392 | check_dbmobject_open(self); |
| 393 | key = gdbm_firstkey(self->di_dbm); |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 394 | if (key.dptr) { |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 395 | v = PyBytes_FromStringAndSize(key.dptr, key.dsize); |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 396 | free(key.dptr); |
| 397 | return v; |
| 398 | } |
| 399 | else { |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 400 | Py_RETURN_NONE; |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 401 | } |
Guido van Rossum | fbd30e9 | 1995-03-16 16:07:34 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 404 | /*[clinic input] |
| 405 | _gdbm.gdbm.nextkey |
| 406 | |
Larry Hastings | 38337d1 | 2015-05-07 23:30:09 -0700 | [diff] [blame] | 407 | key: str(accept={str, robuffer}, zeroes=True) |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 408 | / |
| 409 | |
| 410 | Returns the key that follows key in the traversal. |
| 411 | |
| 412 | The following code prints every key in the database db, without having |
| 413 | to create a list in memory that contains them all: |
| 414 | |
| 415 | k = db.firstkey() |
| 416 | while k != None: |
| 417 | print(k) |
| 418 | k = db.nextkey(k) |
| 419 | [clinic start generated code]*/ |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 420 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 421 | static PyObject * |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 422 | _gdbm_gdbm_nextkey_impl(dbmobject *self, const char *key, |
| 423 | Py_ssize_clean_t key_length) |
Larry Hastings | 38337d1 | 2015-05-07 23:30:09 -0700 | [diff] [blame] | 424 | /*[clinic end generated code: output=192ab892de6eb2f6 input=1f1606943614e36f]*/ |
Guido van Rossum | fbd30e9 | 1995-03-16 16:07:34 +0000 | [diff] [blame] | 425 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 426 | PyObject *v; |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 427 | datum dbm_key, nextkey; |
Guido van Rossum | fbd30e9 | 1995-03-16 16:07:34 +0000 | [diff] [blame] | 428 | |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 429 | dbm_key.dptr = (char *)key; |
| 430 | dbm_key.dsize = key_length; |
| 431 | check_dbmobject_open(self); |
| 432 | nextkey = gdbm_nextkey(self->di_dbm, dbm_key); |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 433 | if (nextkey.dptr) { |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 434 | v = PyBytes_FromStringAndSize(nextkey.dptr, nextkey.dsize); |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 435 | free(nextkey.dptr); |
| 436 | return v; |
| 437 | } |
| 438 | else { |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 439 | Py_RETURN_NONE; |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 440 | } |
Guido van Rossum | fbd30e9 | 1995-03-16 16:07:34 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 443 | /*[clinic input] |
| 444 | _gdbm.gdbm.reorganize |
| 445 | |
| 446 | Reorganize the database. |
| 447 | |
| 448 | If you have carried out a lot of deletions and would like to shrink |
| 449 | the space used by the GDBM file, this routine will reorganize the |
| 450 | database. GDBM will not shorten the length of a database file except |
| 451 | by using this reorganization; otherwise, deleted file space will be |
| 452 | kept and reused as new (key,value) pairs are added. |
| 453 | [clinic start generated code]*/ |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 454 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 455 | static PyObject * |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 456 | _gdbm_gdbm_reorganize_impl(dbmobject *self) |
| 457 | /*[clinic end generated code: output=38d9624df92e961d input=f6bea85bcfd40dd2]*/ |
Guido van Rossum | fbd30e9 | 1995-03-16 16:07:34 +0000 | [diff] [blame] | 458 | { |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 459 | check_dbmobject_open(self); |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 460 | errno = 0; |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 461 | if (gdbm_reorganize(self->di_dbm) < 0) { |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 462 | if (errno != 0) |
| 463 | PyErr_SetFromErrno(DbmError); |
| 464 | else |
| 465 | PyErr_SetString(DbmError, gdbm_strerror(gdbm_errno)); |
| 466 | return NULL; |
| 467 | } |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 468 | Py_RETURN_NONE; |
Guido van Rossum | fbd30e9 | 1995-03-16 16:07:34 +0000 | [diff] [blame] | 469 | } |
| 470 | |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 471 | /*[clinic input] |
| 472 | _gdbm.gdbm.sync |
| 473 | |
| 474 | Flush the database to the disk file. |
| 475 | |
| 476 | When the database has been opened in fast mode, this method forces |
| 477 | any unwritten data to be written to the disk. |
| 478 | [clinic start generated code]*/ |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 479 | |
Roger E. Masse | e5a9c8f | 1997-03-25 17:39:56 +0000 | [diff] [blame] | 480 | static PyObject * |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 481 | _gdbm_gdbm_sync_impl(dbmobject *self) |
| 482 | /*[clinic end generated code: output=488b15f47028f125 input=2a47d2c9e153ab8a]*/ |
Roger E. Masse | e5a9c8f | 1997-03-25 17:39:56 +0000 | [diff] [blame] | 483 | { |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 484 | check_dbmobject_open(self); |
| 485 | gdbm_sync(self->di_dbm); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 486 | Py_RETURN_NONE; |
Roger E. Masse | e5a9c8f | 1997-03-25 17:39:56 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Nick Coghlan | c610aba | 2013-11-17 15:59:51 +1000 | [diff] [blame] | 489 | static PyObject * |
| 490 | dbm__enter__(PyObject *self, PyObject *args) |
| 491 | { |
| 492 | Py_INCREF(self); |
| 493 | return self; |
| 494 | } |
| 495 | |
| 496 | static PyObject * |
| 497 | dbm__exit__(PyObject *self, PyObject *args) |
| 498 | { |
| 499 | _Py_IDENTIFIER(close); |
| 500 | return _PyObject_CallMethodId(self, &PyId_close, NULL); |
| 501 | } |
| 502 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 503 | static PyMethodDef dbm_methods[] = { |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 504 | _GDBM_GDBM_CLOSE_METHODDEF |
| 505 | _GDBM_GDBM_KEYS_METHODDEF |
| 506 | _GDBM_GDBM_FIRSTKEY_METHODDEF |
| 507 | _GDBM_GDBM_NEXTKEY_METHODDEF |
| 508 | _GDBM_GDBM_REORGANIZE_METHODDEF |
| 509 | _GDBM_GDBM_SYNC_METHODDEF |
| 510 | _GDBM_GDBM_GET_METHODDEF |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 511 | _GDBM_GDBM_SETDEFAULT_METHODDEF |
Nick Coghlan | c610aba | 2013-11-17 15:59:51 +1000 | [diff] [blame] | 512 | {"__enter__", dbm__enter__, METH_NOARGS, NULL}, |
| 513 | {"__exit__", dbm__exit__, METH_VARARGS, NULL}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 514 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 515 | }; |
| 516 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 517 | static PyTypeObject Dbmtype = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 518 | PyVarObject_HEAD_INIT(0, 0) |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 519 | "_gdbm.gdbm", |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 520 | sizeof(dbmobject), |
| 521 | 0, |
| 522 | (destructor)dbm_dealloc, /*tp_dealloc*/ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 523 | 0, /*tp_vectorcall_offset*/ |
Amaury Forgeot d'Arc | 1f900f1 | 2008-07-02 22:38:47 +0000 | [diff] [blame] | 524 | 0, /*tp_getattr*/ |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 525 | 0, /*tp_setattr*/ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 526 | 0, /*tp_as_async*/ |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 527 | 0, /*tp_repr*/ |
| 528 | 0, /*tp_as_number*/ |
Guido van Rossum | 0da7e03 | 2006-08-19 23:18:48 +0000 | [diff] [blame] | 529 | &dbm_as_sequence, /*tp_as_sequence*/ |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 530 | &dbm_as_mapping, /*tp_as_mapping*/ |
| 531 | 0, /*tp_hash*/ |
| 532 | 0, /*tp_call*/ |
| 533 | 0, /*tp_str*/ |
| 534 | 0, /*tp_getattro*/ |
| 535 | 0, /*tp_setattro*/ |
| 536 | 0, /*tp_as_buffer*/ |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 537 | Py_TPFLAGS_DEFAULT, /*tp_xxx4*/ |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 538 | gdbm_object__doc__, /*tp_doc*/ |
Amaury Forgeot d'Arc | 1f900f1 | 2008-07-02 22:38:47 +0000 | [diff] [blame] | 539 | 0, /*tp_traverse*/ |
| 540 | 0, /*tp_clear*/ |
| 541 | 0, /*tp_richcompare*/ |
| 542 | 0, /*tp_weaklistoffset*/ |
| 543 | 0, /*tp_iter*/ |
| 544 | 0, /*tp_iternext*/ |
| 545 | dbm_methods, /*tp_methods*/ |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 546 | }; |
| 547 | |
| 548 | /* ----------------------------------------------------------------- */ |
| 549 | |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 550 | /*[clinic input] |
| 551 | _gdbm.open as dbmopen |
Serhiy Storchaka | 6f600ff | 2018-02-26 16:02:22 +0200 | [diff] [blame] | 552 | filename: unicode |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 553 | flags: str="r" |
| 554 | mode: int(py_default="0o666") = 0o666 |
| 555 | / |
| 556 | |
| 557 | Open a dbm database and return a dbm object. |
| 558 | |
| 559 | The filename argument is the name of the database file. |
| 560 | |
| 561 | The optional flags argument can be 'r' (to open an existing database |
| 562 | for reading only -- default), 'w' (to open an existing database for |
| 563 | reading and writing), 'c' (which creates the database if it doesn't |
| 564 | exist), or 'n' (which always creates a new empty database). |
| 565 | |
| 566 | Some versions of gdbm support additional flags which must be |
| 567 | appended to one of the flags described above. The module constant |
| 568 | 'open_flags' is a string of valid additional flags. The 'f' flag |
| 569 | opens the database in fast mode; altered data will not automatically |
| 570 | be written to the disk after every change. This results in faster |
| 571 | writes to the database, but may result in an inconsistent database |
| 572 | if the program crashes while the database is still open. Use the |
| 573 | sync() method to force any unwritten data to be written to the disk. |
| 574 | The 's' flag causes all database operations to be synchronized to |
| 575 | disk. The 'u' flag disables locking of the database file. |
| 576 | |
| 577 | The optional mode argument is the Unix mode of the file, used only |
| 578 | when the database has to be created. It defaults to octal 0o666. |
| 579 | [clinic start generated code]*/ |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 580 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 581 | static PyObject * |
Serhiy Storchaka | 6f600ff | 2018-02-26 16:02:22 +0200 | [diff] [blame] | 582 | dbmopen_impl(PyObject *module, PyObject *filename, const char *flags, |
| 583 | int mode) |
| 584 | /*[clinic end generated code: output=9527750f5df90764 input=3be0b0875974b928]*/ |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 585 | { |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 586 | int iflags; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 587 | |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 588 | switch (flags[0]) { |
| 589 | case 'r': |
| 590 | iflags = GDBM_READER; |
| 591 | break; |
| 592 | case 'w': |
| 593 | iflags = GDBM_WRITER; |
| 594 | break; |
| 595 | case 'c': |
| 596 | iflags = GDBM_WRCREAT; |
| 597 | break; |
| 598 | case 'n': |
| 599 | iflags = GDBM_NEWDB; |
| 600 | break; |
| 601 | default: |
| 602 | PyErr_SetString(DbmError, |
Neil Schemenauer | e9e860f | 2000-12-17 07:14:13 +0000 | [diff] [blame] | 603 | "First flag must be one of 'r', 'w', 'c' or 'n'"); |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 604 | return NULL; |
| 605 | } |
Neil Schemenauer | e9e860f | 2000-12-17 07:14:13 +0000 | [diff] [blame] | 606 | for (flags++; *flags != '\0'; flags++) { |
| 607 | char buf[40]; |
| 608 | switch (*flags) { |
| 609 | #ifdef GDBM_FAST |
| 610 | case 'f': |
| 611 | iflags |= GDBM_FAST; |
| 612 | break; |
| 613 | #endif |
| 614 | #ifdef GDBM_SYNC |
| 615 | case 's': |
| 616 | iflags |= GDBM_SYNC; |
| 617 | break; |
| 618 | #endif |
| 619 | #ifdef GDBM_NOLOCK |
| 620 | case 'u': |
| 621 | iflags |= GDBM_NOLOCK; |
| 622 | break; |
| 623 | #endif |
| 624 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 625 | PyOS_snprintf(buf, sizeof(buf), "Flag '%c' is not supported.", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 626 | *flags); |
Neil Schemenauer | e9e860f | 2000-12-17 07:14:13 +0000 | [diff] [blame] | 627 | PyErr_SetString(DbmError, buf); |
| 628 | return NULL; |
| 629 | } |
| 630 | } |
| 631 | |
Serhiy Storchaka | 6f600ff | 2018-02-26 16:02:22 +0200 | [diff] [blame] | 632 | PyObject *filenamebytes = PyUnicode_EncodeFSDefault(filename); |
| 633 | if (filenamebytes == NULL) { |
| 634 | return NULL; |
| 635 | } |
| 636 | const char *name = PyBytes_AS_STRING(filenamebytes); |
| 637 | if (strlen(name) != (size_t)PyBytes_GET_SIZE(filenamebytes)) { |
| 638 | Py_DECREF(filenamebytes); |
| 639 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 640 | return NULL; |
| 641 | } |
| 642 | PyObject *self = newdbmobject(name, iflags, mode); |
| 643 | Py_DECREF(filenamebytes); |
| 644 | return self; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 645 | } |
| 646 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 647 | static const char dbmmodule_open_flags[] = "rwcn" |
Neil Schemenauer | e9e860f | 2000-12-17 07:14:13 +0000 | [diff] [blame] | 648 | #ifdef GDBM_FAST |
| 649 | "f" |
| 650 | #endif |
| 651 | #ifdef GDBM_SYNC |
| 652 | "s" |
| 653 | #endif |
| 654 | #ifdef GDBM_NOLOCK |
| 655 | "u" |
| 656 | #endif |
| 657 | ; |
| 658 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 659 | static PyMethodDef dbmmodule_methods[] = { |
Serhiy Storchaka | 9260e77 | 2015-04-17 21:05:18 +0300 | [diff] [blame] | 660 | DBMOPEN_METHODDEF |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 661 | { 0, 0 }, |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 662 | }; |
| 663 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 664 | |
| 665 | static struct PyModuleDef _gdbmmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 666 | PyModuleDef_HEAD_INIT, |
| 667 | "_gdbm", |
| 668 | gdbmmodule__doc__, |
| 669 | -1, |
| 670 | dbmmodule_methods, |
| 671 | NULL, |
| 672 | NULL, |
| 673 | NULL, |
| 674 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 675 | }; |
| 676 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 677 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 678 | PyInit__gdbm(void) { |
Victor Stinner | 00f9edb | 2018-06-19 23:29:22 +0200 | [diff] [blame] | 679 | PyObject *m; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 680 | |
Guido van Rossum | 0da7e03 | 2006-08-19 23:18:48 +0000 | [diff] [blame] | 681 | if (PyType_Ready(&Dbmtype) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 682 | return NULL; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 683 | m = PyModule_Create(&_gdbmmodule); |
Victor Stinner | 00f9edb | 2018-06-19 23:29:22 +0200 | [diff] [blame] | 684 | if (m == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 685 | return NULL; |
Neil Schemenauer | e9e860f | 2000-12-17 07:14:13 +0000 | [diff] [blame] | 686 | } |
Victor Stinner | 00f9edb | 2018-06-19 23:29:22 +0200 | [diff] [blame] | 687 | |
| 688 | DbmError = PyErr_NewException("_gdbm.error", PyExc_OSError, NULL); |
| 689 | if (DbmError == NULL) { |
| 690 | goto error; |
| 691 | } |
| 692 | Py_INCREF(DbmError); |
| 693 | if (PyModule_AddObject(m, "error", DbmError) < 0) { |
| 694 | Py_DECREF(DbmError); |
| 695 | goto error; |
| 696 | } |
| 697 | |
| 698 | if (PyModule_AddStringConstant(m, "open_flags", |
| 699 | dbmmodule_open_flags) < 0) { |
| 700 | goto error; |
| 701 | } |
| 702 | |
Xiang Zhang | b248e95 | 2018-06-20 21:23:30 +0800 | [diff] [blame] | 703 | #if defined(GDBM_VERSION_MAJOR) && defined(GDBM_VERSION_MINOR) && \ |
| 704 | defined(GDBM_VERSION_PATCH) |
Victor Stinner | 00f9edb | 2018-06-19 23:29:22 +0200 | [diff] [blame] | 705 | PyObject *obj = Py_BuildValue("iii", GDBM_VERSION_MAJOR, |
| 706 | GDBM_VERSION_MINOR, GDBM_VERSION_PATCH); |
| 707 | if (obj == NULL) { |
| 708 | goto error; |
| 709 | } |
| 710 | if (PyModule_AddObject(m, "_GDBM_VERSION", obj) < 0) { |
| 711 | Py_DECREF(obj); |
| 712 | goto error; |
| 713 | } |
Xiang Zhang | b248e95 | 2018-06-20 21:23:30 +0800 | [diff] [blame] | 714 | #endif |
Victor Stinner | 00f9edb | 2018-06-19 23:29:22 +0200 | [diff] [blame] | 715 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 716 | return m; |
Victor Stinner | 00f9edb | 2018-06-19 23:29:22 +0200 | [diff] [blame] | 717 | |
| 718 | error: |
| 719 | Py_DECREF(m); |
| 720 | return NULL; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 721 | } |