diff options
author | Guido van Rossum <guido@python.org> | 2022-09-09 02:32:40 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-09 02:32:40 (GMT) |
commit | df50938f583b6abd9f31f1ff1f5ad52d7b04ecbb (patch) | |
tree | aa1fbb0e797563aae648fb1499b3297dcca59b50 /Modules/_gdbmmodule.c | |
parent | 95d6330a3edacacd081f55699bd6f0a787908924 (diff) | |
download | cpython-df50938f583b6abd9f31f1ff1f5ad52d7b04ecbb.zip cpython-df50938f583b6abd9f31f1ff1f5ad52d7b04ecbb.tar.gz cpython-df50938f583b6abd9f31f1ff1f5ad52d7b04ecbb.tar.bz2 |
GH-46412: More efficient bool() for ndbm/_gdbmmodule (#96692)
Diffstat (limited to 'Modules/_gdbmmodule.c')
-rw-r--r-- | Modules/_gdbmmodule.c | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/Modules/_gdbmmodule.c b/Modules/_gdbmmodule.c index e6440fa..a96d323 100644 --- a/Modules/_gdbmmodule.c +++ b/Modules/_gdbmmodule.c @@ -162,6 +162,35 @@ gdbm_length(gdbmobject *dp) return dp->di_size; } +static int +gdbm_bool(gdbmobject *dp) +{ + _gdbm_state *state = PyType_GetModuleState(Py_TYPE(dp)); + if (dp->di_dbm == NULL) { + PyErr_SetString(state->gdbm_error, "GDBM object has already been closed"); + return -1; + } + if (dp->di_size > 0) { + /* Known non-zero size. */ + return 1; + } + if (dp->di_size == 0) { + /* Known zero size. */ + return 0; + } + /* Unknown size. Ensure DBM object has an entry. */ + datum key = gdbm_firstkey(dp->di_dbm); + if (key.dptr == NULL) { + /* Empty. Cache this fact. */ + dp->di_size = 0; + return 0; + } + + /* Non-empty. Don't cache the length since we don't know. */ + free(key.dptr); + return 1; +} + // Wrapper function for PyArg_Parse(o, "s#", &d.dptr, &d.size). // This function is needed to support PY_SSIZE_T_CLEAN. // Return 1 on success, same to PyArg_Parse(). @@ -569,6 +598,7 @@ static PyType_Slot gdbmtype_spec_slots[] = { {Py_mp_length, gdbm_length}, {Py_mp_subscript, gdbm_subscript}, {Py_mp_ass_subscript, gdbm_ass_sub}, + {Py_nb_bool, gdbm_bool}, {Py_tp_doc, (char*)gdbm_object__doc__}, {0, 0} }; |