anydbm bug ?

  • Thread starter Gary Richardson
  • Start date
G

Gary Richardson

According to the Python Library Reference invoking anydbm.open() with flag
value 'n' should always create a new empty database. However, the following
code produces an error. I stumbled upon this while writing some test code.
Opening and closing the file before invoking anydbm.open() was an attempt to
delete the results of a previous test. Does this indicate a bug in anydbm?

import anydbm
filename = r'C:\My Documents\Python\misc\testdb.tmp'
f = open(filename, 'w')
f.close()
file = anydbm.open(filename, 'n')

produces:

PythonWin 2.2.2 (#37, Nov 26 2002, 10:24:37) [MSC 32 bit (Intel)] on win32.
Portions Copyright 1994-2001 Mark Hammond ([email protected]) - see
'Help/About PythonWin' for further copyright information.
Traceback (most recent call last):
File
"C:\Python22\Lib\site-packages\Pythonwin\pywin\framework\scriptutils.py",
line 301, in RunScript
exec codeObject in __main__.__dict__
File "C:\My Documents\Python\PicDas_\Script1.py", line 5, in ?
file = anydbm.open(filename, 'n')
File "C:\PYTHON22\LIB\anydbm.py", line 83, in open
raise error, "db type could not be determined"
error: db type could not be determined
 
A

Andrew MacIntyre

According to the Python Library Reference invoking anydbm.open() with flag
value 'n' should always create a new empty database. However, the following
code produces an error. I stumbled upon this while writing some test code.
Opening and closing the file before invoking anydbm.open() was an attempt to
delete the results of a previous test. Does this indicate a bug in anydbm?

import anydbm
filename = r'C:\My Documents\Python\misc\testdb.tmp'
f = open(filename, 'w')
f.close()
file = anydbm.open(filename, 'n')

I don't think there's a bug in anydbm, though the docs perhaps should make
clear that you should make sure any db files are removed before attempting
to create a new db.

truncation is a poor way to attempt to get rid of anything that might
exist - if it exists, delete it.
 
D

Dennis Lee Bieber

Gary Richardson fed this fish to the penguins on Thursday 04 December
2003 08:07 am:
According to the Python Library Reference invoking anydbm.open() with
flag value 'n' should always create a new empty database. However, the
following code produces an error. I stumbled upon this while writing
some test code. Opening and closing the file before invoking
anydbm.open() was an attempt to delete the results of a previous test.
Does this indicate a bug in anydbm?
If you look at the code for anydbm, the first thing it does is see if
the file exists using whichdb. whichdb is finding the file that you
nulled the contents of, and is unable to determine the type of db file
it found. The "n" flag is only checked /if/ no file is found, otherwise
it is passed to the dbm module selected by whichdb.

Read the paragraph /above/ the one that describes the flags:

"""
If the database file already exists, the whichdb module is used to
determine its type and the appropriate module is used; if it does not
exist, the first module listed above that can be imported is used.
"""

Your open(w)/close() sequence leave a file in place. whichdb is
finding that file, then failing to determine the dbm module that
handles the contents. It is the dbm-specific module that handles the
"n" flag to create a new (which I interpret to me: using the dbm module
that the file /already/ if formatted for, delete all records, leave
file open for use)


Rather than using open/close, why not just delete the file en toto?

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top