Index: BucketTemplate.c =================================================================== --- BucketTemplate.c (revision 30993) +++ BucketTemplate.c (working copy) @@ -682,7 +682,7 @@ Bucket_maxminKey(Bucket *self, PyObject *args, int min) { PyObject *key=0; - int rc, offset; + int rc, offset, empty_bucket = 1; if (args && ! PyArg_ParseTuple(args, "|O", &key)) return NULL; @@ -696,6 +696,7 @@ if ((rc = Bucket_findRangeEnd(self, key, min, 0, &offset)) <= 0) { if (rc < 0) return NULL; + empty_bucket = 0; goto empty; } } @@ -708,7 +709,10 @@ return key; empty: - PyErr_SetString(PyExc_ValueError, "empty bucket"); + if (empty_bucket) + PyErr_SetString(PyExc_ValueError, "empty bucket"); + else + PyErr_SetString(PyExc_ValueError, "doesn't exist any key satisfying the conditions"); PER_UNUSE(self); return NULL; } Index: tests/testBTrees.py =================================================================== --- tests/testBTrees.py (revision 30993) +++ tests/testBTrees.py (working copy) @@ -301,6 +301,14 @@ self.assertEqual(t.minKey(), 1) self.assertEqual(t.minKey(3), 3) self.assertEqual(t.minKey(9), 10) + try: + t.maxKey(t.minKey()-1) + except ValueError, err: + self.assertEqual(str(err), "doesn't exist any key satisfying the conditions") + try: + t.minKey(t.maxKey()+1) + except ValueError, err: + self.assertEqual(str(err), "doesn't exist any key satisfying the conditions") def testClear(self): r = range(100) @@ -671,6 +679,14 @@ self.assert_(t.minKey()-1 not in t) self.assert_(t.maxKey() in t) self.assert_(t.maxKey()+1 not in t) + try: + t.maxKey(t.minKey()-1) + except ValueError, err: + self.assertEqual(str(err), "doesn't exist any key satisfying the conditions") + try: + t.minKey(t.maxKey()+1) + except ValueError, err: + self.assertEqual(str(err), "doesn't exist any key satisfying the conditions") def testUpdate(self): d={} Index: BTreeTemplate.c =================================================================== --- BTreeTemplate.c (revision 30993) +++ BTreeTemplate.c (working copy) @@ -1340,7 +1340,7 @@ { PyObject *key=0; Bucket *bucket = NULL; - int offset, rc; + int offset, rc, empty_tree = 1; UNLESS (PyArg_ParseTuple(args, "|O", &key)) return NULL; @@ -1355,6 +1355,7 @@ if ((rc = BTree_findRangeEnd(self, key, min, 0, &bucket, &offset)) <= 0) { if (rc < 0) goto err; + empty_tree = 0; goto empty; } PER_UNUSE(self); @@ -1392,8 +1393,10 @@ return key; empty: - PyErr_SetString(PyExc_ValueError, "empty tree"); - + if (empty_tree) + PyErr_SetString(PyExc_ValueError, "empty tree"); + else + PyErr_SetString(PyExc_ValueError, "doesn't exist any key satisfying the conditions"); err: PER_UNUSE(self); if (bucket)