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

限于喜欢 提交于 2019-12-01 10:26:51

问题


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


回答1:


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)


来源:https://stackoverflow.com/questions/35771816/shelve-db-type-could-not-be-determined-whichdb-is-not-recognizing-gdb

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!