2018-03-29 06:08:02 +00:00
|
|
|
import pickle
|
2018-04-17 16:40:45 +00:00
|
|
|
import sys
|
2018-03-29 06:08:02 +00:00
|
|
|
|
|
|
|
import unittest2
|
|
|
|
|
|
|
|
import mitogen.core
|
|
|
|
|
2018-06-26 01:17:50 +00:00
|
|
|
import testlib
|
|
|
|
import plain_old_module
|
2018-04-17 16:40:45 +00:00
|
|
|
|
|
|
|
|
2018-11-03 13:28:37 +00:00
|
|
|
class ConstructorTest(testlib.TestCase):
|
2018-03-29 06:08:02 +00:00
|
|
|
klass = mitogen.core.CallError
|
|
|
|
|
|
|
|
def test_string_noargs(self):
|
|
|
|
e = self.klass('%s%s')
|
2018-04-17 16:40:45 +00:00
|
|
|
self.assertEquals(e.args[0], '%s%s')
|
2019-01-21 15:25:46 +00:00
|
|
|
self.assertTrue(isinstance(e.args[0], mitogen.core.UnicodeType))
|
2018-03-29 06:08:02 +00:00
|
|
|
|
|
|
|
def test_string_args(self):
|
|
|
|
e = self.klass('%s%s', 1, 1)
|
2018-04-17 16:40:45 +00:00
|
|
|
self.assertEquals(e.args[0], '11')
|
2019-01-21 15:25:46 +00:00
|
|
|
self.assertTrue(isinstance(e.args[0], mitogen.core.UnicodeType))
|
2018-03-29 06:08:02 +00:00
|
|
|
|
|
|
|
def test_from_exc(self):
|
2018-06-26 01:17:50 +00:00
|
|
|
ve = plain_old_module.MyError('eek')
|
2018-03-29 06:08:02 +00:00
|
|
|
e = self.klass(ve)
|
2018-06-26 01:17:50 +00:00
|
|
|
self.assertEquals(e.args[0], 'plain_old_module.MyError: eek')
|
2019-01-21 15:25:46 +00:00
|
|
|
self.assertTrue(isinstance(e.args[0], mitogen.core.UnicodeType))
|
2018-03-29 06:08:02 +00:00
|
|
|
|
2018-05-11 13:56:20 +00:00
|
|
|
def test_form_base_exc(self):
|
|
|
|
ve = SystemExit('eek')
|
|
|
|
e = self.klass(ve)
|
2019-01-23 12:44:08 +00:00
|
|
|
cls = ve.__class__
|
2018-06-26 02:20:25 +00:00
|
|
|
self.assertEquals(e.args[0],
|
|
|
|
# varies across 2/3.
|
2019-01-23 12:44:08 +00:00
|
|
|
'%s.%s: eek' % (cls.__module__, cls.__name__))
|
2019-01-21 15:25:46 +00:00
|
|
|
self.assertTrue(isinstance(e.args[0], mitogen.core.UnicodeType))
|
2018-05-11 13:56:20 +00:00
|
|
|
|
2018-03-29 06:08:02 +00:00
|
|
|
def test_from_exc_tb(self):
|
|
|
|
try:
|
2018-06-26 01:17:50 +00:00
|
|
|
raise plain_old_module.MyError('eek')
|
|
|
|
except plain_old_module.MyError:
|
2018-04-17 16:40:45 +00:00
|
|
|
ve = sys.exc_info()[1]
|
2018-03-29 06:08:02 +00:00
|
|
|
e = self.klass(ve)
|
|
|
|
|
2018-06-26 01:17:50 +00:00
|
|
|
self.assertTrue(e.args[0].startswith('plain_old_module.MyError: eek'))
|
2019-01-21 15:25:46 +00:00
|
|
|
self.assertTrue(isinstance(e.args[0], mitogen.core.UnicodeType))
|
2018-04-17 16:40:45 +00:00
|
|
|
self.assertTrue('test_from_exc_tb' in e.args[0])
|
2018-03-29 06:08:02 +00:00
|
|
|
|
2019-01-21 15:25:46 +00:00
|
|
|
def test_bytestring_conversion(self):
|
|
|
|
e = self.klass(mitogen.core.b('bytes'))
|
|
|
|
self.assertEquals(u'bytes', e.args[0])
|
|
|
|
self.assertTrue(isinstance(e.args[0], mitogen.core.UnicodeType))
|
|
|
|
|
|
|
|
def test_reduce(self):
|
|
|
|
e = self.klass('eek')
|
|
|
|
func, (arg,) = e.__reduce__()
|
|
|
|
self.assertTrue(func is mitogen.core._unpickle_call_error)
|
|
|
|
self.assertEquals(arg, e.args[0])
|
|
|
|
|
|
|
|
|
|
|
|
class UnpickleCallErrorTest(testlib.TestCase):
|
|
|
|
func = staticmethod(mitogen.core._unpickle_call_error)
|
|
|
|
|
|
|
|
def test_not_unicode(self):
|
|
|
|
self.assertRaises(TypeError,
|
|
|
|
lambda: self.func(mitogen.core.b('bad')))
|
|
|
|
|
|
|
|
def test_oversized(self):
|
|
|
|
self.assertRaises(TypeError,
|
|
|
|
lambda: self.func(mitogen.core.b('b'*10001)))
|
|
|
|
|
|
|
|
def test_reify(self):
|
|
|
|
e = self.func(u'some error')
|
2019-01-23 12:44:08 +00:00
|
|
|
self.assertEquals(mitogen.core.CallError, e.__class__)
|
2019-01-21 15:25:46 +00:00
|
|
|
self.assertEquals(1, len(e.args))
|
|
|
|
self.assertEquals(mitogen.core.UnicodeType, type(e.args[0]))
|
|
|
|
self.assertEquals(u'some error', e.args[0])
|
|
|
|
|
2018-03-29 06:08:02 +00:00
|
|
|
|
2018-11-03 13:28:37 +00:00
|
|
|
class PickleTest(testlib.TestCase):
|
2018-03-29 06:08:02 +00:00
|
|
|
klass = mitogen.core.CallError
|
|
|
|
|
|
|
|
def test_string_noargs(self):
|
|
|
|
e = self.klass('%s%s')
|
|
|
|
e2 = pickle.loads(pickle.dumps(e))
|
2018-04-17 16:40:45 +00:00
|
|
|
self.assertEquals(e2.args[0], '%s%s')
|
2018-03-29 06:08:02 +00:00
|
|
|
|
|
|
|
def test_string_args(self):
|
|
|
|
e = self.klass('%s%s', 1, 1)
|
|
|
|
e2 = pickle.loads(pickle.dumps(e))
|
2018-04-17 16:40:45 +00:00
|
|
|
self.assertEquals(e2.args[0], '11')
|
2018-03-29 06:08:02 +00:00
|
|
|
|
|
|
|
def test_from_exc(self):
|
2018-06-26 01:17:50 +00:00
|
|
|
ve = plain_old_module.MyError('eek')
|
2018-03-29 06:08:02 +00:00
|
|
|
e = self.klass(ve)
|
|
|
|
e2 = pickle.loads(pickle.dumps(e))
|
2018-06-26 01:17:50 +00:00
|
|
|
self.assertEquals(e2.args[0], 'plain_old_module.MyError: eek')
|
2018-03-29 06:08:02 +00:00
|
|
|
|
|
|
|
def test_from_exc_tb(self):
|
|
|
|
try:
|
2018-06-26 01:17:50 +00:00
|
|
|
raise plain_old_module.MyError('eek')
|
|
|
|
except plain_old_module.MyError:
|
2018-04-17 16:40:45 +00:00
|
|
|
ve = sys.exc_info()[1]
|
2018-03-29 06:08:02 +00:00
|
|
|
e = self.klass(ve)
|
|
|
|
|
|
|
|
e2 = pickle.loads(pickle.dumps(e))
|
2018-06-26 01:17:50 +00:00
|
|
|
self.assertTrue(e2.args[0].startswith('plain_old_module.MyError: eek'))
|
2018-04-17 16:40:45 +00:00
|
|
|
self.assertTrue('test_from_exc_tb' in e2.args[0])
|
2018-03-29 06:08:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest2.main()
|