[英]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:对于每个类,我将setstate和getstate方法设置如下:
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方法将一个空列表传递给新方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.