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