[Python-checkins] cpython (3.3): Issue #19288: Fixed the "in" operator of dbm.gnu databases for string

serhiy.storchaka python-checkins at python.org
Thu Oct 24 23:08:47 CEST 2013


http://hg.python.org/cpython/rev/09ed1b3b54f3
changeset:   86606:09ed1b3b54f3
branch:      3.3
parent:      86604:61ab0c6907f9
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Fri Oct 25 00:06:52 2013 +0300
summary:
  Issue #19288: Fixed the "in" operator of dbm.gnu databases for string
argument.  Original patch by Arfrever Frehtes Taifersar Arahesis.

files:
  Lib/test/test_dbm_gnu.py |   1 +
  Misc/NEWS                |   3 +++
  Modules/_gdbmmodule.c    |  17 +++++++++++++----
  3 files changed, 17 insertions(+), 4 deletions(-)


diff --git a/Lib/test/test_dbm_gnu.py b/Lib/test/test_dbm_gnu.py
--- a/Lib/test/test_dbm_gnu.py
+++ b/Lib/test/test_dbm_gnu.py
@@ -24,6 +24,7 @@
         self.g[b'bytes'] = b'data'
         key_set = set(self.g.keys())
         self.assertEqual(key_set, set([b'a', b'bytes', b'12345678910']))
+        self.assertIn('a', self.g)
         self.assertIn(b'a', self.g)
         self.assertEqual(self.g[b'bytes'], b'data')
         key = self.g.firstkey()
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -81,6 +81,9 @@
 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
   argument.  Original patch by Arfrever Frehtes Taifersar Arahesis.
 
diff --git a/Modules/_gdbmmodule.c b/Modules/_gdbmmodule.c
--- a/Modules/_gdbmmodule.c
+++ b/Modules/_gdbmmodule.c
@@ -290,20 +290,29 @@
 {
     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);
 }
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list