summaryrefslogtreecommitdiff
path: root/Modules/_dbmmodule.c
diff options
context:
space:
mode:
Diffstat (limited to 'Modules/_dbmmodule.c')
-rw-r--r--Modules/_dbmmodule.c16
1 files changed, 10 insertions, 6 deletions
diff --git a/Modules/_dbmmodule.c b/Modules/_dbmmodule.c
index 827acce895..83c051c505 100644
--- a/Modules/_dbmmodule.c
+++ b/Modules/_dbmmodule.c
@@ -212,6 +212,7 @@ dbm_contains(PyObject *self, PyObject *arg)
{
dbmobject *dp = (dbmobject *)self;
datum key, val;
+ Py_ssize_t size;
if ((dp)->di_dbm == NULL) {
PyErr_SetString(DbmError,
@@ -219,18 +220,21 @@ dbm_contains(PyObject *self, PyObject *arg)
return -1;
}
if (PyUnicode_Check(arg)) {
- arg = _PyUnicode_AsDefaultEncodedString(arg, NULL);
- if (arg == NULL)
+ key.dptr = PyUnicode_AsUTF8AndSize(arg, &size);
+ key.dsize = size;
+ if (key.dptr == NULL)
return -1;
}
- if (!PyBytes_Check(arg)) {
+ else if (!PyBytes_Check(arg)) {
PyErr_Format(PyExc_TypeError,
- "dbm key must be string, not %.100s",
+ "dbm 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);
+ }
val = dbm_fetch(dp->di_dbm, key);
return val.dptr != NULL;
}