Arnold Roa
Arnold Roa

Reputation: 7718

shelve db type could not be determined, whichdb is not recognizing gdb

Why shelve raise an error if I try to open a file just created by shelve?

import shelve
info_file_name = "/Users/bacon/myproject/temp/test.info"

info_file = shelve.open(info_file_name)
info_file['ok'] = 'wass'
info_file.close()

info_file = shelve.open(info_file_name) # raise exception db type could not be determined..
info_file.close()

I'm running python 2.5 in case is relevant

The precise error is raising is:

db type could not be determined its raised by anydbm.py open method.

I know it;s using gdbm. I checked on the whichdb.py file, and it tries to identify gdbm files with this

 # Read the start of the file -- the magic number
s16 = f.read(16)
s = s16[0:4]

# Convert to 4-byte int in native byte order -- return "" if impossible
(magic,) = struct.unpack("=l", s)

# Check for GNU dbm
if magic == 0x13579ace:
    return "gdbm"

But the "magic" number in my file is 324508367 (0x13579acf) (only the last digit change!!)

I tried opening the file with another language (ruby) and I were able to open it without any problem, so this seems to be a bug in whichdb.py trying to identify the correct dbm

Upvotes: 1

Views: 1866

Answers (1)

Arnold Roa
Arnold Roa

Reputation: 7718

As explained on the question this error was due to a bug in whichdb that is not able to identify some newest gdb files, more information is on this bug report: https://bugs.python.org/issue13007

The best solution is to force the db defining a method that load the shelve with gdbm instead of trying to guess the dbm.

def gdbm_shelve(filename, flag="c"):
    mod = __import__("gdbm")
    return shelve.Shelf(mod.open(filename, flag))

And then use it instead of shelve.open:

info_file = gdbm_shelve(info_file_name)

Upvotes: 2

Related Questions