python的shelve模块有最大大小吗?

2024-10-01 09:39:16 发布

您现在位置:Python中文网/ 问答频道 /正文

当我试图打开超过某个大小(实际上相当小(<;1MB)的搁置持久化文件时,会遇到此异常,但我不确定确切的数字在哪里。现在,我知道pickle有点像python的私生子,而shelve并没有被认为是一个特别健壮的解决方案,但是它恰好很好地解决了我的问题(理论上),我还没有找到这个异常的原因。在

Traceback (most recent call last):
  File "test_shelve.py", line 27, in <module>
    print len(f.keys())
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shelve.py", line 101, in keys
    return self.dict.keys()
SystemError: Negative size passed to PyString_FromStringAndSize

我可以不断地复制它,但我在google上找不到太多。这是一个脚本,将复制。在

^{pr2}$

Tags: 文件inpyltline原因数字keys
2条回答

如果我将深度从2更改为1,或者在Python3下运行(在修复了print语句并使用items()而不是{})时,代码“有效”。但是,键列表显然不是迭代recursive_dict()返回值时找到的一组键。在

来自shelve文档的以下限制可能适用(重点是我的):

The choice of which database package will be used (such as dbm, gdbm or bsddb) depends on which interface is available. Therefore it is not safe to open the database directly using dbm. The database is also (unfortunately) subject to the limitations of dbm, if it is used — this means that (the pickled representation of) the objects stored in the database should be fairly small, and in rare cases key collisions may cause the database to refuse updates.

关于错误本身:

The idea circulating on the web is the data size exceeded the largest integer possible on that machine (the largest 32 bit (signed) integer is 2 147 483 647), interpreted as a negative size by Python.

您的代码是用2.7.3运行的,因此可能是一个已修复的错误。在

相关问题 更多 >