This repository was archived by the owner on Oct 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
This repository was archived by the owner on Oct 27, 2022. It is now read-only.
TimeoutExpired is not pickleable. #57
Copy link
Copy link
Closed
Description
In Python 2.x, subclasses of Exception must either call super(...).__init__(*args) or set self.args in order to be successfully pickled/unpickled.
Example:
>>> import subprocess32, pickle
>>> t = subprocess32.TimeoutExpired(['foo', 'bar'], 60)
>>> pickle.loads(pickle.dumps(t))
TypeError Traceback (most recent call last)
<ipython-input-6-c105e6dd98fb> in <module>()
----> 1 pickle.loads(pickle.dumps(t))
/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.pyc in loads(str)
1386 def loads(str):
1387 file = StringIO(str)
-> 1388 return Unpickler(file).load()
1389
1390 # Doctest
/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.pyc in load(self)
862 while 1:
863 key = read(1)
--> 864 dispatch[key](self)
865 except _Stop, stopinst:
866 return stopinst.value
/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.pyc in load_reduce(self)
1137 args = stack.pop()
1138 func = stack[-1]
-> 1139 value = func(*args)
1140 stack[-1] = value
1141 dispatch[REDUCE] = load_reduce
TypeError: __init__() takes at least 3 arguments (1 given)Python bug: https://bugs.python.org/issue1692335
Explanation: https://stackoverflow.com/questions/41808912/cannot-unpickle-exception-subclass
Example Solution:
>>> import subprocess32, pickle
>>> t = subprocess32.TimeoutExpired(['foo', 'bar'], 60)
>>> pickle.loads(pickle.dumps(t))
>>> t.args = (t.cmd, t.timeout, t.stdout, t.stderr) # Magic!
>>> pickle.loads(pickle.dumps(t))
# No exception now!
Metadata
Metadata
Assignees
Labels
No labels