2

我刚刚遇到了一个完全奇怪的错误。我将选择的东西保存到一个文件中。其中之一是一个名为 Tracker 的类的版本,这个副本称为 tracker。

        file = shelve.open(worldname, 'n')

        file['Terramap'] = terramap
        file['Satmap'] = satmap
        file['Gasmap'] = gasmap
        file['Planetset'] = planetset
        file['World'] = minimap
        file['Picture'] = picturemap                                    
        file['Worlddata'] = worlddata
        file['Dimension'] = dimension
        print "check", len(tracker.families)
        file['Tracker'] = tracker
        file['Schedule'] = schedule
        file['Time'] = time
        file.close()      

如果我让它在文件之前从跟踪器打印一些东西,纯粹是为了测试该跟踪器仍然存在,它确实可以检测到跟踪器。但随后单行,它出现了:

  File "C:\Users\Mark\Desktop\Ultima Ratio Regum\URR0-2-1.py", line 17522, in world_menu
    file['Tracker'] = tracker
  File "C:\Python27\lib\shelve.py", line 132, in __setitem__
    p.dump(value)
TypeError: expected string or Unicode object, NoneType found

我只是……难住了。这以前从未发生过,我不明白这是怎么回事!任何人都可以对此有所了解吗?我今天所做的只是向跟踪器添加一些东西,但跟踪器只真正存储字符串列表,最多几千个,但仅此而已。但是,如果我让它在保存开始之前创建一个新的跟踪器,它保存得很好。

编辑:

运行 pickle.dumps(tracker) 显示:

  File "C:\Users\Mark\Desktop\Ultima Ratio Regum\29-12-test.py", line 17515, in world_menu
    pickle.dumps(tracker)
  File "C:\Python27\lib\pickle.py", line 1374, in dumps
    Pickler(file, protocol).dump(obj)
  File "C:\Python27\lib\pickle.py", line 224, in dump
    self.save(obj)
  File "C:\Python27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "C:\Python27\lib\pickle.py", line 725, in save_inst
    save(stuff)
  File "C:\Python27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "C:\Python27\lib\pickle.py", line 649, in save_dict
    self._batch_setitems(obj.iteritems())
  File "C:\Python27\lib\pickle.py", line 663, in _batch_setitems
    save(v)
  File "C:\Python27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "C:\Python27\lib\pickle.py", line 600, in save_list
    self._batch_appends(iter(obj))
  File "C:\Python27\lib\pickle.py", line 615, in _batch_appends
    save(x)
  File "C:\Python27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "C:\Python27\lib\pickle.py", line 725, in save_inst
    save(stuff)
  File "C:\Python27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "C:\Python27\lib\pickle.py", line 649, in save_dict
    self._batch_setitems(obj.iteritems())
  File "C:\Python27\lib\pickle.py", line 663, in _batch_setitems
    save(v)
  File "C:\Python27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "C:\Python27\lib\pickle.py", line 748, in save_global
    (obj, module, name))
PicklingError: Can't pickle <built-in method capitalize of str object at 0x0A1341C0>: it's not found as __main__.capitalize
4

1 回答 1

5

该异常来自 的内部cPickle.Pickler.dump,但由于它位于已编译的 C 模块中,因此它没有出现在回溯中。要了解错误是什么,请尝试手动触发相同的事情,但使用纯 Pythonpickle模块而不是编译的 CcPickle模块:

import pickle
pickle.dumps(tracker)

这应该揭示错误在哪里。我的第一个猜测是您的实现__reduce_ex__or__reduce__不返回值。

于 2012-12-29T02:49:55.727 回答