无法确定搁置的db类型,哪个db无法识别gdb [英] shelve db type could not be determined, whichdb is not recognizing gdb

查看:98
本文介绍了无法确定搁置的db类型,哪个db无法识别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()

我正在运行python 2.5以防相关

I'm running python 2.5 in case is relevant

引起的确切错误是:

db type could not be determinedanydbm.py open方法引发.

我知道;正在使用gdbm.我检查了whichdb.py文件,并尝试以此来识别gdbm文件

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"

但是我文件中的魔术"数字是324508367(0x13579acf)(只有最后一位数字改变了!! )

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

我尝试用另一种语言(红宝石)打开文件,并且能够毫无问题地打开它,所以这似乎是在db.py中尝试识别正确的dbm的错误

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

推荐答案

如问题所述,此错误是由于whichdb中的错误无法识别一些最新的gdb文件引起的,有关此错误报告的更多信息: https://bugs.python.org/issue13007

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

最好的解决方案是强制db定义一种用gdbm加载货架的方法,而不是尝试猜测dbm.

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

然后使用它代替shelve.open:

info_file = gdbm_shelve(info_file_name)

这篇关于无法确定搁置的db类型,哪个db无法识别gdb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆