Issue #19288: Fixed the "in" operator of dbm.gnu databases for string

argument.  Original patch by Arfrever Frehtes Taifersar Arahesis.
This commit is contained in:
Serhiy Storchaka 2013-10-25 00:06:52 +03:00
parent 9da33ab193
commit 7d6392c517
3 changed files with 17 additions and 4 deletions

View File

@ -24,6 +24,7 @@ class TestGdbm(unittest.TestCase):
self.g[b'bytes'] = b'data' self.g[b'bytes'] = b'data'
key_set = set(self.g.keys()) key_set = set(self.g.keys())
self.assertEqual(key_set, set([b'a', b'bytes', b'12345678910'])) self.assertEqual(key_set, set([b'a', b'bytes', b'12345678910']))
self.assertIn('a', self.g)
self.assertIn(b'a', self.g) self.assertIn(b'a', self.g)
self.assertEqual(self.g[b'bytes'], b'data') self.assertEqual(self.g[b'bytes'], b'data')
key = self.g.firstkey() key = self.g.firstkey()

View File

@ -81,6 +81,9 @@ Core and Builtins
Library Library
------- -------
- Issue #19288: Fixed the "in" operator of dbm.gnu databases for string
argument. Original patch by Arfrever Frehtes Taifersar Arahesis.
- Issue #19287: Fixed the "in" operator of dbm.ndbm databases for string - Issue #19287: Fixed the "in" operator of dbm.ndbm databases for string
argument. Original patch by Arfrever Frehtes Taifersar Arahesis. argument. Original patch by Arfrever Frehtes Taifersar Arahesis.

View File

@ -290,20 +290,29 @@ dbm_contains(PyObject *self, PyObject *arg)
{ {
dbmobject *dp = (dbmobject *)self; dbmobject *dp = (dbmobject *)self;
datum key; datum key;
Py_ssize_t size;
if ((dp)->di_dbm == NULL) { if ((dp)->di_dbm == NULL) {
PyErr_SetString(DbmError, PyErr_SetString(DbmError,
"GDBM object has already been closed"); "GDBM object has already been closed");
return -1; 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, 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); arg->ob_type->tp_name);
return -1; return -1;
} }
key.dptr = PyBytes_AS_STRING(arg); else {
key.dsize = PyBytes_GET_SIZE(arg); key.dptr = PyBytes_AS_STRING(arg);
key.dsize = PyBytes_GET_SIZE(arg);
}
return gdbm_exists(dp->di_dbm, key); return gdbm_exists(dp->di_dbm, key);
} }