泡菜跨平台__dict__属性错误 [英] Pickle cross platform __dict__ attribute error

查看:90
本文介绍了泡菜跨平台__dict__属性错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在泡菜上有问题.在OSX和Linux之间,一切正常,但在Windows和Linux中,一切正常.所有腌制的字符串都存储在内存中,并通过SSL套接字发送.要100%清除,我用":::"替换了所有的"\ n",并用"==="替换了所有的"\ r"(没有).场景:

I'm having an issue with pickle. Things work fine between OSX and Linux, but not Windows and Linux. All pickled strings are stored in memory and sent via an SSL socket. To be 100% clear I have replaced all '\n's with ":::" and all '\r's with "===" (there were none). Scenario:

  • Client-Win:运行Python 2.7的Small Business Server 2011
  • Client-Lin:运行Python 2.7的Fedora Linux
  • 服务器:运行Python 2.7的Fedora Linux

Client-Lin将一个腌制的对象发送到服务器:

Client-Lin sends a pickled object to Server:

ccopy_reg:::_reconstructor:::p0:::(c__main__:::infoCollection:::p1:::c__builtin__:::tuple:::p2:::(VSTRINGA:::p3:::VSTRINGB:::p4:::VSTRINGC:::p5:::tp6:::tp7:::Rp8:::.

Client-Win将选择的对象发送到服务器:

Client-Win sends a picked object to Server:

ccopy_reg:::_reconstructor:::p0:::(c__main__:::infoCollection:::p1:::c__builtin__:::tuple:::p2:::(VSTRINGA:::p3:::VSTRINGB:::p4:::VSTRINGC:::p5:::tp6:::tp7:::Rp8:::ccollections:::OrderedDict:::p9:::((lp10:::(lp11:::S'string_a':::p12:::ag3:::aa(lp13:::S'string_b':::p14:::ag4:::aa(lp15:::S'string_c':::p16:::ag5:::aatp17:::Rp18:::b.

由于某种原因,Windows客户端会与泡菜一起发送额外的信息,而当Linux客户端尝试加载泡菜字符串时,我会得到:

For some reason the Windows client sends extra information along with the pickle, and when the Linux client tries to load the pickle string I get:

Unhandled exception in thread started by <function TestThread at 0x107de60>
Traceback (most recent call last):
  File "./test.py", line 212, in TestThread
    info = pickle.loads(p_string)
  File "/usr/lib64/python2.7/pickle.py", line 1382, in loads
    return Unpickler(file).load()
  File "/usr/lib64/python2.7/pickle.py", line 858, in load
    dispatch[key](self)
  File "/usr/lib64/python2.7/pickle.py", line 1224, in load_build
    d = inst.__dict__
AttributeError: 'infoCollection' object has no attribute '__dict__'

有什么想法吗?

编辑 添加其他要求的信息.

EDIT Adding additional requested information.

infoCollection类的定义方式相同:

The infoCollection class is defined the same way:

infoCollection = collections.namedtuple('infoCollection', 'string_a, string_b, string_c')

def runtest():
    info = infoCollection('STRINGA', 'STRINGB', 'STRINGC')
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    ssl_sock = ssl.wrap_socket(s, ssl_version=ssl.PROTOCOL_TLSv1)
    ssl_sock.connect((server, serverport))
    ssl_sock.write(pickle.dumps(info))
    ssl_sock.close()

接收功能几乎相同,但确实有

And the receiving function is much the same but does a

p_string = ssl_sock.read()
info = pickle.loads(p_string)

推荐答案

一个黑客,但是跨平台问题似乎是由于在跨平台环境中namedtuple和pickle在一起造成的.我已经用自己的班级替换了namedtuple,并且一切正常.

A hack, but the cross-platform issue appears to be due to namedtuples and pickle together in a cross-platform environment. I have replaced the namedtuple with my own class and all works well.

class infoClass(object):
    pass

def infoCollection(string_a, string_b, string_c):
    i = infoClass()
    i.string_a = string_a
    i.string_b = string_b
    i.string_c = string_c
    return i

这篇关于泡菜跨平台__dict__属性错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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