[Python-bugs-list] [ python-Bugs-715063 ] bsddb.first()/next() raise undocumented exception

SourceForge.net noreply@sourceforge.net
Mon, 07 Jul 2003 10:49:41 -0700


Bugs item #715063, was opened at 2003-04-03 23:03
Message generated for change (Comment added) made by bwarsaw
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=715063&group_id=5470

Category: Python Library
Group: Python 2.3
Status: Closed
Resolution: Works For Me
Priority: 5
Submitted By: Christian Stork (cst)
Assigned to: Gregory P. Smith (greg)
Summary: bsddb.first()/next() raise undocumented exception

Initial Comment:
bsddb object's first() & next() methods raise an undocumented 
exception.  Wouldn't returning None make much more sense?  
And shouldn't this be documented? 

Python 2.3a2+ (#2, Mar 21 2003, 22:13:05) 
[GCC 3.2.3 20030316 (Debian prerelease)] on linux2
>>> import bsddb
>>> h.bsddb.hashopen("testdb")
>>> h.first()
------------------------------------------------------------
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/lib/python2.3/bsddb/__init__.py", line 134, in first
    rv = self.dbc.first()
DBNotFoundError: (-30991, 'DB_NOTFOUND: No matching key/
data pair found')

Note: The same exception appeared for the equivalent dbhash 
methods.  But I assume this should be fixed if this bug is fixed.

----------------------------------------------------------------------

>Comment By: Barry A. Warsaw (bwarsaw)
Date: 2003-07-07 13:49

Message:
Logged In: YES 
user_id=12800

Greg, what about cursor.set() -- I don't think this follows
set_get_returns_none() does it?  I wonder if it should.


----------------------------------------------------------------------

Comment By: Gregory P. Smith (greg)
Date: 2003-07-06 20:01

Message:
Logged In: YES 
user_id=413

DBNotFoundError derives from KeyError.  The old bsddb library also raised KeyError on first/last/next calls when there was no more data rather than returning None so that
interface cannot be changed.  (I agree, returning None would make more sense; but I
can't break the old interface).

The new pybsddb interface's DB and DBEnv set_get_returns_none method does allow you to control this behaviour.

If you are using the old style bsddb interface then you do not have access to the DBEnv before the DB object is created (DB objects inherit the flag setting from the DBEnv when they are created) so you'll need to call set_get_returns_none on the DB object itself:

import bsddb   # python 2.3 bsddb (aka bsddb3 or pybsddb)

hdb = bsddb.hashopen("myhash", 'c')
hdb.db.set_get_returns_none(1)   # will only work on python 2.3 / pybsddb
spam = hdb.first()
while spam:
  spam = hdb.next()

# notice there were no errors.


I've looked at the code for first/last/prev/get and they all use the same internal DBCursor_get wrapper that obeys the set_get_returns_none flag so that you can
write simple "foo = hdb.first();  while not foo:  foo = hdb.next()"  code.


----------------------------------------------------------------------

Comment By: Gregory P. Smith (greg)
Date: 2003-07-06 19:24

Message:
Logged In: YES 
user_id=413

i'm taking a look.

----------------------------------------------------------------------

Comment By: Barry A. Warsaw (bwarsaw)
Date: 2003-04-07 18:46

Message:
Logged In: YES 
user_id=12800

pybsddb has this option to return None from cursor.get()
methods instead of raising DBNotFoundError.  The DBEnv
method set_get_returns_none() can be used to change the
behavior, although the default is to return None.  I agree
that this is very handy!

I suppose .first() and .last() should perhaps honor this
config as well?


----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=715063&group_id=5470