diff options
author | 2013-10-25 00:08:13 +0300 | |
---|---|---|
committer | 2013-10-25 00:08:13 +0300 | |
commit | 4f056d43d6ad62d63306cd8a693dda4b52d3ae34 (patch) | |
tree | 6a0a25ab2322468c3eb0b0b236e2cae1a634af21 /Modules/_gdbmmodule.c | |
parent | Issue #19287: Fixed the "in" operator of dbm.ndbm databases for string (diff) | |
parent | Issue #19288: Fixed the "in" operator of dbm.gnu databases for string (diff) | |
download | cpython-4f056d43d6ad62d63306cd8a693dda4b52d3ae34.tar.gz cpython-4f056d43d6ad62d63306cd8a693dda4b52d3ae34.tar.bz2 cpython-4f056d43d6ad62d63306cd8a693dda4b52d3ae34.zip |
Issue #19288: Fixed the "in" operator of dbm.gnu databases for string
argument. Original patch by Arfrever Frehtes Taifersar Arahesis.
Diffstat (limited to 'Modules/_gdbmmodule.c')
-rw-r--r-- | Modules/_gdbmmodule.c | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/Modules/_gdbmmodule.c b/Modules/_gdbmmodule.c index 1db021aaa1f..36c06d13b79 100644 --- a/Modules/_gdbmmodule.c +++ b/Modules/_gdbmmodule.c @@ -290,20 +290,29 @@ dbm_contains(PyObject *self, PyObject *arg) { dbmobject *dp = (dbmobject *)self; datum key; + Py_ssize_t size; if ((dp)->di_dbm == NULL) { PyErr_SetString(DbmError, "GDBM object has already been closed"); return -1; } - if (!PyBytes_Check(arg)) { + if (PyUnicode_Check(arg)) { + key.dptr = PyUnicode_AsUTF8AndSize(arg, &size); + key.dsize = size; + if (key.dptr == NULL) + return -1; + } + else if (!PyBytes_Check(arg)) { PyErr_Format(PyExc_TypeError, - "gdbm key must be bytes, not %.100s", + "gdbm key must be bytes or string, not %.100s", arg->ob_type->tp_name); return -1; } - key.dptr = PyBytes_AS_STRING(arg); - key.dsize = PyBytes_GET_SIZE(arg); + else { + key.dptr = PyBytes_AS_STRING(arg); + key.dsize = PyBytes_GET_SIZE(arg); + } return gdbm_exists(dp->di_dbm, key); } |