简体   繁体   English

pickle.loads 不返回相同的数据转储

[英]pickle.loads does not return the same data dumped

PolicyModule is a class created by me. PolicyModule 是我创建的一个类。

When I try to dump/load an instance of it I do not get in return the same data:当我尝试转储/加载它的一个实例时,我没有得到相同的数据:

module = PolicyModule(new.name,new.domain,decomposed_module[switch][port])
module.install()
modules[new.name] = module
print 'POLICY TO DUMP'
print modules[new.name].isInstalled()
print modules[new.name].name
print modules[new.name].domain
print modules[new.name].action
print modules[new.name].policy  
print '\n'
msg = pickle.dumps(modules[new.name],-1)
x = pickle.loads(msg)
print 'POLICY LOADED'
print x.isInstalled()
print x.name
print x.domain
print x.action
print x.policy 
print '\n'

This is what is printed:这是打印出来的:

POLICY TO DUMP
True
routing
match: ('dstip', 10.0.0.1)
parallel:
    sequential:
        match: ('switch', 2) ('dstip', 10.0.0.2)
        fwd 1
    sequential:
        match: ('switch', 2) ('dstip', 10.0.0.3)
        fwd 2
sequential:
    match: ('dstip', 10.0.0.1)
    parallel:
        sequential:
            match: ('switch', 2) ('dstip', 10.0.0.2)
            fwd 1
        sequential:
            match: ('switch', 2) ('dstip', 10.0.0.3)
            fwd 2

POLICY LOADED
True
routing
match: ('dstip', 10.0.0.1)
drop
identity

Note well : match, sequential, fwd and parallel are instances of others classes.请注意:match、sequential、fwd 和 parallel 是其他类的实例。

For each class, I set the setstate and getstate methods as following:对于每个类,我将setstategetstate方法设置如下:

def __getstate__(self):
    odict = self.__dict__.copy() # copy the dict since we change it
    return odict

def __setstate__(self, dict):
    self.__dict__.update(dict)

It think that the problem is here:它认为问题出在这里:

class sequential(CombinatorPolicy):

def __new__(self, policies=[]):
    if len(policies) == 0:
        return identity
    else:
        rv = super(sequential, self).__new__(sequential, policies)
        rv.__init__(policies)
        return rv

and here:和这里:

class parallel(CombinatorPolicy):

def __new__(self, policies=[]):
    # Hackety hack.
    if len(policies) == 0:
        return drop
    else:
        rv = super(parallel, self).__new__(parallel, policies)
        rv.__init__(policies)
        return rv

because I always get a drop instead of a parallel instance, and an identity instead of a sequential .因为我总是得到一个drop而不是并行实例,以及一个身份而不是一个连续的. It's like the setdate method passes an empty list to the new method.这就像setdate方法将一个空列表传递给方法。

I found the solution myself.我自己找到了解决方案。

I just forgot to define getnewargs methods in both parallel and sequential classes.我只是忘了在并行顺序类中定义getnewargs方法。

More info here .更多信息在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何禁用pickle.loads以确保安全? - How to disable pickle.loads for security? pickle.loads() 的问题 - 输入不足 - Problem with pickle.loads() - ran out of input Python:类实例的pickle.loads失败了 - Python: pickle.loads failed for class instance 我正在转储管道数据,但是当我尝试使用pickle.loads读取时,出现错误 - i am dumping a piped data , but when i try to read using pickle.loads i get an error 如何将pickle.loads从Python 2转换为Python 3? - How to convert pickle.loads from Python 2 to Python 3? Python pickle:如何从pickle.loads读取键值对 - Python pickle: how to read key-values pairs from pickle.loads 如果服务器在 pickle.loads 之前调用 pickle.dumps 有没有办法进行 RCE? - If a server calls pickle.dumps before pickle.loads is there any way for RCE? pickle.loads 给出'模块' object 没有属性'<classname> ' 在 Pyspark Pandas Udf 内</classname> - pickle.loads gives 'module' object has no attribute '<ClassName>' inside a Pyspark Pandas Udf EOFError:用完了输入。 尝试从套接字获取pickle.load时遇到此错误 - EOFError: ran out of input. Getting this error when trying to pickle.loads from a socket 用于发送网络数据的Pickle.dump和load是否需要按字节顺序进行更改? - Does Pickle.dumps and loads used for sending network data require change in byte order?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM