提交 78fd7c53 authored 作者: Olivier Delalleau's avatar Olivier Delalleau

Restored assert removed in e53532fa0e76 => protocol 0 should not be used

anymore when pickling functions Fixed corresponding tests that were using protocol 0.
上级 667cfdda
...@@ -429,7 +429,7 @@ class T_picklefunction(unittest.TestCase): ...@@ -429,7 +429,7 @@ class T_picklefunction(unittest.TestCase):
f = function([x, In(a, value=1.0,name='a'), In(s, value=0.0, update=s+a*x, mutable=True)], s+a*x) f = function([x, In(a, value=1.0,name='a'), In(s, value=0.0, update=s+a*x, mutable=True)], s+a*x)
try: try:
g = cPickle.loads(cPickle.dumps(f)) g = cPickle.loads(cPickle.dumps(f, protocol=-1))
except NotImplementedError, e: except NotImplementedError, e:
if e[0].startswith('DebugMode is not picklable'): if e[0].startswith('DebugMode is not picklable'):
return return
...@@ -467,7 +467,7 @@ class T_picklefunction(unittest.TestCase): ...@@ -467,7 +467,7 @@ class T_picklefunction(unittest.TestCase):
old_default_link = config.linker old_default_link = config.linker
try: try:
try: try:
str_f = cPickle.dumps(f) str_f = cPickle.dumps(f, protocol=-1)
config.mode = 'Mode' config.mode = 'Mode'
config.linker = 'py' config.linker = 'py'
config.optimizer = 'None' config.optimizer = 'None'
...@@ -527,7 +527,7 @@ class T_picklefunction(unittest.TestCase): ...@@ -527,7 +527,7 @@ class T_picklefunction(unittest.TestCase):
# try to pickle the entire things # try to pickle the entire things
try: try:
saved_format = cPickle.dumps(list_of_things) saved_format = cPickle.dumps(list_of_things, protocol=-1)
new_list_of_things = cPickle.loads(saved_format) new_list_of_things = cPickle.loads(saved_format)
except NotImplementedError, e: except NotImplementedError, e:
if e[0].startswith('DebugMode is not picklable'): if e[0].startswith('DebugMode is not picklable'):
......
...@@ -700,7 +700,7 @@ def test_pickle(): ...@@ -700,7 +700,7 @@ def test_pickle():
mode = get_mode() mode = get_mode()
m = M.make(x=numpy.zeros((4,5)), y=numpy.ones((2,3)), mode=mode) m = M.make(x=numpy.zeros((4,5)), y=numpy.ones((2,3)), mode=mode)
m_dup = cPickle.loads(cPickle.dumps(m)) m_dup = cPickle.loads(cPickle.dumps(m, protocol=-1))
assert numpy.all(m.x == m_dup.x) and numpy.all(m.y == m_dup.y) assert numpy.all(m.x == m_dup.x) and numpy.all(m.y == m_dup.y)
......
...@@ -49,14 +49,31 @@ class DeepCopiableFunction(Singleton): ...@@ -49,14 +49,31 @@ class DeepCopiableFunction(Singleton):
if type(self) != type(other): if type(self) != type(other):
return False return False
# Since it is a singleton there should be no two different instances # Since it is a singleton there should be no two different instances
# of this class. However it looks like this can actually happen under # of this class.
# Linux when pickling with protocol 0 => need to look into this. Until assert self is other
# then, the assert below is commented and it might happen that two
# instances actually exist (which should not be an issue unless someone
# does a comparison with the 'is' operator).
#assert self is other
return True return True
def __hash__(self): def __hash__(self):
# Required for Ops that contain such functions. # Required for Ops that contain such functions.
return hash(type(self)) return hash(type(self))
def __getstate__(self):
# Only implemented to ensure __setstate__ will always be called
# after unpickling such an object, even when __dict__ is empty.
if self.__dict__:
return self.__dict__
else:
# We need to make sure we return a value whose boolean cast is not
# False, otherwise __setstate__ will not be called.
return True
def __setstate__(self, state):
# Only implemented to enforce the "singletonness" of this class.
if state is not True:
self.__dict__.update(state)
if self is not type(self)._instance:
raise AssertionError(
"Unpickling a singleton should yield this singleton. If "
"this is not the case, you may have pickled it with "
"protocol 0. You will need to use a higher protocol to "
"properly unpickle this object.")
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论