cPickle - 仅在稍后使用unpickled内容时才显示unpickling错误

时间:2013-02-09 21:18:20

标签: python pickle

我现在编写的代码工作正常,我甚至可以打印反序列化的对象,没有任何错误,所以我确实知道那里有什么。

@staticmethod
def receiveData(self):
    '''
    This method has to be static, as it is the argument of a Thread.
    It receives Wrapperobjects from the server (as yet containing only a player)
    and resets the local positions accordingly
    '''

    logging.getLogger(__name__).info("Serverinformationen werden nun empfangen")
    from modules.logic import game
    sock = self.sock
    time.sleep(10)
    self.myPlayer = game.get_player()
    while (True):
        try:
            wrapPacked = sock.recv(4096)
            self.myList = cPickle.loads(wrapPacked)
         #   self.setData(self.myList)             
        except Exception as eload:            
            print eload

但是,如果我尝试实际使用此处注释中的行(self.setData(self.myList),

我得到了

unpickling stack underflow

invalid load key, ' '.

只是为了记录,setData的代码是:

def setData(self, list):    
    if (list.__sizeof__()>0):
        first = list [0]
        self.myPlayer.setPos(first[1])
        self.myPlayer.setVelocity(first[2])

我已经在这3天了,真的,我不知道出了什么问题。 你能帮助我吗? 完全追溯:

Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 551, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 504, in run
    self.__target(*self.__args, **self.__kwargs)
  File "mypath/client.py", line 129, in receiveData
    self.myList = cPickle.loads(wrapPacked)
UnpicklingError: unpickling stack underflow –

1 个答案:

答案 0 :(得分:1)

当您尝试访问pickle数据时,您的异常总是会发生这一事实,这似乎表明您正在点击cPickle库中的错误。

可能发生的事情是C库忘记处理异常。异常信息被存储,而不是处理,并且位于解释器中,直到发生另一个异常或另一段C代码 检查异常。此时抛出旧的,未处理的异常。

您的错误显然与cPickle有关,它对您提供的数据非常不满意,但异常本身会在不相关的位置抛出。这个可以与线程相关,它可能是一个常规的非线程相关错误。

您需要查看是否可以在测试设置中加载数据。将wrapPacked写入文件以供以后测试。将该文件加载到解释器shell会话中,使用cPickle.loads()加载它,看看会发生什么。对pickle模块执行相同的操作。

如果您在此测试会话中遇到类似的问题,并且您可以重现它(会话中稍后会抛出奇怪的异常),您需要向Python项目提交一个错误来查看它。

相关问题