summaryrefslogtreecommitdiff
path: root/Modules/_gdbmmodule.c
diff options
context:
space:
mode:
Diffstat (limited to 'Modules/_gdbmmodule.c')
-rw-r--r--Modules/_gdbmmodule.c17
1 files changed, 13 insertions, 4 deletions
diff --git a/Modules/_gdbmmodule.c b/Modules/_gdbmmodule.c
index 474561b235..0bd59c8aa2 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);
}