anydbm bug ?

Discussion in 'Python' started by Gary Richardson, Dec 4, 2003.

  1. 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 () - 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
    >>>
     
    Gary Richardson, Dec 4, 2003
    #1
    1. Advertising

  2. On Thu, 4 Dec 2003, Gary Richardson wrote:

    > 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.

    --
    Andrew I MacIntyre "These thoughts are mine alone..."
    E-mail: (pref) | Snail: PO Box 370
    (alt) | Belconnen ACT 2616
    Web: http://www.andymac.org/ | Australia
     
    Andrew MacIntyre, Dec 4, 2003
    #2
    1. Advertising

  3. 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?

    --
    > ============================================================== <
    > | Wulfraed Dennis Lee Bieber KD6MOG <
    > | Bestiaria Support Staff <
    > ============================================================== <
    > Bestiaria Home Page: http://www.beastie.dm.net/ <
    > Home Page: http://www.dm.net/~wulfraed/ <
     
    Dennis Lee Bieber, Dec 5, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John D.

    Example code for anydbm wanted...

    John D., Aug 4, 2003, in forum: Python
    Replies:
    0
    Views:
    331
    John D.
    Aug 4, 2003
  2. =?ISO-8859-1?Q?=22C=E9dric_V=2E=22?=

    error at "import anydbm"

    =?ISO-8859-1?Q?=22C=E9dric_V=2E=22?=, Jan 13, 2004, in forum: Python
    Replies:
    4
    Views:
    513
    Peter Hansen
    Jan 14, 2004
  3. Anton Sherwood

    text storage: shelve vs anydbm vs ?

    Anton Sherwood, Apr 17, 2004, in forum: Python
    Replies:
    1
    Views:
    432
    Larry Bates
    Apr 17, 2004
  4. Derek Basch
    Replies:
    1
    Views:
    392
    Dan Sommers
    Jan 11, 2005
  5. Eric S. Johansson

    anydbm biasing

    Eric S. Johansson, Jan 18, 2005, in forum: Python
    Replies:
    0
    Views:
    353
    Eric S. Johansson
    Jan 18, 2005
Loading...

Share This Page