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