Skip to content

Commit e580b07

Browse files
committed
Tests for custom __reduce__, __reduce_ex__ and specifically issue #65
Signed-off-by: Oldřich Jedlička <[email protected]>
1 parent cef6b3f commit e580b07

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

tests/test_issue65.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import pickle
2+
3+
from tblib import pickling_support
4+
5+
6+
class HTTPrettyError(Exception):
7+
pass
8+
9+
10+
class UnmockedError(HTTPrettyError):
11+
def __init__(self):
12+
super().__init__('No mocking was registered, and real connections are not allowed (httpretty.allow_net_connect = False).')
13+
14+
15+
def test_65():
16+
pickling_support.install()
17+
18+
try:
19+
raise UnmockedError
20+
except Exception as e:
21+
exc = e
22+
23+
exc = pickle.loads(pickle.dumps(exc))
24+
25+
assert isinstance(exc, UnmockedError)
26+
assert exc.args == ('No mocking was registered, and real connections are not allowed (httpretty.allow_net_connect = False).',)
27+
assert exc.__traceback__ is not None

tests/test_pickle_exception.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,95 @@ def func(my_arg='2'):
164164

165165
exc = pickle.loads(pickle.dumps(exc, protocol=protocol))
166166
assert exc.__traceback__.tb_next.tb_frame.f_locals == {'my_variable': 1}
167+
168+
169+
class CustomWithAttributesException(Exception):
170+
def __init__(self, message, arg1, arg2, arg3):
171+
super().__init__(message)
172+
self.values12 = (arg1, arg2)
173+
self.value3 = arg3
174+
175+
176+
def test_custom_with_attributes():
177+
try:
178+
raise CustomWithAttributesException('bar', 1, 2, 3)
179+
except Exception as e:
180+
exc = e
181+
182+
tblib.pickling_support.install(exc)
183+
exc = pickle.loads(pickle.dumps(exc))
184+
185+
assert isinstance(exc, CustomWithAttributesException)
186+
assert exc.args == ('bar',)
187+
assert exc.values12 == (1, 2)
188+
assert exc.value3 == 3
189+
assert exc.__traceback__ is not None
190+
191+
192+
class CustomReduceException(Exception):
193+
def __init__(self, message, arg1, arg2, arg3):
194+
super().__init__(message)
195+
self.values12 = (arg1, arg2)
196+
self.value3 = arg3
197+
198+
def __reduce__(self):
199+
return self.__class__, self.args + self.values12 + (self.value3,)
200+
201+
202+
def test_custom_reduce():
203+
try:
204+
raise CustomReduceException('foo', 1, 2, 3)
205+
except Exception as e:
206+
exc = e
207+
208+
tblib.pickling_support.install(exc)
209+
exc = pickle.loads(pickle.dumps(exc))
210+
211+
assert isinstance(exc, CustomReduceException)
212+
assert exc.args == ('foo',)
213+
assert exc.values12 == (1, 2)
214+
assert exc.value3 == 3
215+
assert exc.__traceback__ is not None
216+
217+
218+
class CustomReduceExException(Exception):
219+
def __init__(self, message, arg1, arg2, protocol):
220+
super().__init__(message)
221+
self.values12 = (arg1, arg2)
222+
self.value3 = protocol
223+
224+
def __reduce_ex__(self, protocol):
225+
return self.__class__, self.args + self.values12 + (self.value3,)
226+
227+
228+
@pytest.mark.parametrize('protocol', [None, *list(range(1, pickle.HIGHEST_PROTOCOL + 1))])
229+
def test_custom_reduce_ex(protocol):
230+
try:
231+
raise CustomReduceExException('foo', 1, 2, 3)
232+
except Exception as e:
233+
exc = e
234+
235+
tblib.pickling_support.install(exc)
236+
exc = pickle.loads(pickle.dumps(exc, protocol=protocol))
237+
238+
assert isinstance(exc, CustomReduceExException)
239+
assert exc.args == ('foo',)
240+
assert exc.values12 == (1, 2)
241+
assert exc.value3 == 3
242+
assert exc.__traceback__ is not None
243+
244+
245+
def test_oserror():
246+
try:
247+
raise OSError(13, 'Permission denied')
248+
except Exception as e:
249+
exc = e
250+
251+
tblib.pickling_support.install(exc)
252+
exc = pickle.loads(pickle.dumps(exc))
253+
254+
assert isinstance(exc, OSError)
255+
assert exc.args == (13, 'Permission denied')
256+
assert exc.errno == 13
257+
assert exc.strerror == 'Permission denied'
258+
assert exc.__traceback__ is not None

0 commit comments

Comments
 (0)