##// END OF EJS Templates
Merge pull request #4239 from minrk/better-errors-for-aaron...
Min RK -
r12780:6cc58b80 merge
parent child Browse files
Show More
@@ -436,8 +436,15 b' class Session(Configurable):'
436 msg = dict(a=[1,'hi'])
436 msg = dict(a=[1,'hi'])
437 try:
437 try:
438 packed = pack(msg)
438 packed = pack(msg)
439 except Exception:
439 except Exception as e:
440 raise ValueError("packer could not serialize a simple message")
440 msg = "packer '{packer}' could not serialize a simple message: {e}{jsonmsg}"
441 if self.packer == 'json':
442 jsonmsg = "\nzmq.utils.jsonapi.jsonmod = %s" % jsonapi.jsonmod
443 else:
444 jsonmsg = ""
445 raise ValueError(
446 msg.format(packer=self.packer, e=e, jsonmsg=jsonmsg)
447 )
441
448
442 # ensure packed message is bytes
449 # ensure packed message is bytes
443 if not isinstance(packed, bytes):
450 if not isinstance(packed, bytes):
@@ -446,8 +453,16 b' class Session(Configurable):'
446 # check that unpack is pack's inverse
453 # check that unpack is pack's inverse
447 try:
454 try:
448 unpacked = unpack(packed)
455 unpacked = unpack(packed)
449 except Exception:
456 assert unpacked == msg
450 raise ValueError("unpacker could not handle the packer's output")
457 except Exception as e:
458 msg = "unpacker '{unpacker}' could not handle output from packer '{packer}': {e}{jsonmsg}"
459 if self.packer == 'json':
460 jsonmsg = "\nzmq.utils.jsonapi.jsonmod = %s" % jsonapi.jsonmod
461 else:
462 jsonmsg = ""
463 raise ValueError(
464 msg.format(packer=self.packer, unpacker=self.unpacker, e=e, jsonmsg=jsonmsg)
465 )
451
466
452 # check datetime support
467 # check datetime support
453 msg = dict(t=datetime.now())
468 msg = dict(t=datetime.now())
@@ -20,6 +20,12 b' from zmq.eventloop.zmqstream import ZMQStream'
20
20
21 from IPython.kernel.zmq import session as ss
21 from IPython.kernel.zmq import session as ss
22
22
23 def _bad_packer(obj):
24 raise TypeError("I don't work")
25
26 def _bad_unpacker(bytes):
27 raise TypeError("I don't work either")
28
23 class SessionTestCase(BaseZMQTestCase):
29 class SessionTestCase(BaseZMQTestCase):
24
30
25 def setUp(self):
31 def setUp(self):
@@ -222,4 +228,44 b' class TestSession(SessionTestCase):'
222 self.assertTrue(len(session.digest_history) == 100)
228 self.assertTrue(len(session.digest_history) == 100)
223 session._add_digest(uuid.uuid4().bytes)
229 session._add_digest(uuid.uuid4().bytes)
224 self.assertTrue(len(session.digest_history) == 91)
230 self.assertTrue(len(session.digest_history) == 91)
225
231
232 def test_bad_pack(self):
233 try:
234 session = ss.Session(pack=_bad_packer)
235 except ValueError as e:
236 self.assertIn("could not serialize", str(e))
237 self.assertIn("don't work", str(e))
238 else:
239 self.fail("Should have raised ValueError")
240
241 def test_bad_unpack(self):
242 try:
243 session = ss.Session(unpack=_bad_unpacker)
244 except ValueError as e:
245 self.assertIn("could not handle output", str(e))
246 self.assertIn("don't work either", str(e))
247 else:
248 self.fail("Should have raised ValueError")
249
250 def test_bad_packer(self):
251 try:
252 session = ss.Session(packer=__name__ + '._bad_packer')
253 except ValueError as e:
254 self.assertIn("could not serialize", str(e))
255 self.assertIn("don't work", str(e))
256 else:
257 self.fail("Should have raised ValueError")
258
259 def test_bad_unpacker(self):
260 try:
261 session = ss.Session(unpacker=__name__ + '._bad_unpacker')
262 except ValueError as e:
263 self.assertIn("could not handle output", str(e))
264 self.assertIn("don't work either", str(e))
265 else:
266 self.fail("Should have raised ValueError")
267
268 def test_bad_roundtrip(self):
269 with self.assertRaises(ValueError):
270 session= ss.Session(unpack=lambda b: 5)
271
General Comments 0
You need to be logged in to leave comments. Login now