def testPickleMessageAttribute(self): # Pickling with message attribute must work, as well. e = Exception("foo") f = Exception("foo") f.message = "bar" for p in pickle, cPickle: ep = p.loads(p.dumps(e)) with warnings.catch_warnings(): ignore_message_warning() self.assertEqual(ep.message, "foo") fp = p.loads(p.dumps(f)) self.assertEqual(fp.message, "bar")
def testAttributes(self): # test that exception attributes are happy exceptionList = [ (BaseException, (), {'message' : '', 'args' : ()}), (BaseException, (1, ), {'message' : 1, 'args' : (1,)}), (BaseException, ('foo',), {'message' : 'foo', 'args' : ('foo',)}), (BaseException, ('foo', 1), {'message' : '', 'args' : ('foo', 1)}), (SystemExit, ('foo',), {'message' : 'foo', 'args' : ('foo',), 'code' : 'foo'}), (IOError, ('foo',), {'message' : 'foo', 'args' : ('foo',), 'filename' : None, 'errno' : None, 'strerror' : None}), (IOError, ('foo', 'bar'), {'message' : '', 'args' : ('foo', 'bar'), 'filename' : None, 'errno' : 'foo', 'strerror' : 'bar'}), (IOError, ('foo', 'bar', 'baz'), {'message' : '', 'args' : ('foo', 'bar'), 'filename' : 'baz', 'errno' : 'foo', 'strerror' : 'bar'}), (IOError, ('foo', 'bar', 'baz', 'quux'), {'message' : '', 'args' : ('foo', 'bar', 'baz', 'quux')}), (EnvironmentError, ('errnoStr', 'strErrorStr', 'filenameStr'), {'message' : '', 'args' : ('errnoStr', 'strErrorStr'), 'strerror' : 'strErrorStr', 'errno' : 'errnoStr', 'filename' : 'filenameStr'}), (EnvironmentError, (1, 'strErrorStr', 'filenameStr'), {'message' : '', 'args' : (1, 'strErrorStr'), 'errno' : 1, 'strerror' : 'strErrorStr', 'filename' : 'filenameStr'}), (SyntaxError, (), {'message' : '', 'msg' : None, 'text' : None, 'filename' : None, 'lineno' : None, 'offset' : None, 'print_file_and_line' : None}), (SyntaxError, ('msgStr',), {'message' : 'msgStr', 'args' : ('msgStr',), 'text' : None, 'print_file_and_line' : None, 'msg' : 'msgStr', 'filename' : None, 'lineno' : None, 'offset' : None}), (SyntaxError, ('msgStr', ('filenameStr', 'linenoStr', 'offsetStr', 'textStr')), {'message' : '', 'offset' : 'offsetStr', 'text' : 'textStr', 'args' : ('msgStr', ('filenameStr', 'linenoStr', 'offsetStr', 'textStr')), 'print_file_and_line' : None, 'msg' : 'msgStr', 'filename' : 'filenameStr', 'lineno' : 'linenoStr'}), (SyntaxError, ('msgStr', 'filenameStr', 'linenoStr', 'offsetStr', 'textStr', 'print_file_and_lineStr'), {'message' : '', 'text' : None, 'args' : ('msgStr', 'filenameStr', 'linenoStr', 'offsetStr', 'textStr', 'print_file_and_lineStr'), 'print_file_and_line' : None, 'msg' : 'msgStr', 'filename' : None, 'lineno' : None, 'offset' : None}), (UnicodeError, (), {'message' : '', 'args' : (),}), (UnicodeEncodeError, ('ascii', u'a', 0, 1, 'ordinal not in range'), {'message' : '', 'args' : ('ascii', u'a', 0, 1, 'ordinal not in range'), 'encoding' : 'ascii', 'object' : u'a', 'start' : 0, 'reason' : 'ordinal not in range'}), (UnicodeDecodeError, ('ascii', '\xff', 0, 1, 'ordinal not in range'), {'message' : '', 'args' : ('ascii', '\xff', 0, 1, 'ordinal not in range'), 'encoding' : 'ascii', 'object' : '\xff', 'start' : 0, 'reason' : 'ordinal not in range'}), (UnicodeTranslateError, (u"\u3042", 0, 1, "ouch"), {'message' : '', 'args' : (u'\u3042', 0, 1, 'ouch'), 'object' : u'\u3042', 'reason' : 'ouch', 'start' : 0, 'end' : 1}), ] try: exceptionList.append( (WindowsError, (1, 'strErrorStr', 'filenameStr'), {'message' : '', 'args' : (1, 'strErrorStr'), 'strerror' : 'strErrorStr', 'winerror' : 1, 'errno' : 22, 'filename' : 'filenameStr'}) ) except NameError: pass with warnings.catch_warnings(): ignore_message_warning() for exc, args, expected in exceptionList: try: raise exc(*args) except BaseException, e: if type(e) is not exc: raise # Verify module name self.assertEquals(type(e).__module__, 'exceptions') # Verify no ref leaks in Exc_str() s = str(e) for checkArgName in expected: self.assertEquals(repr(getattr(e, checkArgName)), repr(expected[checkArgName]), 'exception "%s", attribute "%s"' % (repr(e), checkArgName)) # test for pickling support for p in pickle, cPickle: for protocol in range(p.HIGHEST_PROTOCOL + 1): new = p.loads(p.dumps(e, protocol)) for checkArgName in expected: got = repr(getattr(new, checkArgName)) want = repr(expected[checkArgName]) self.assertEquals(got, want, 'pickled "%r", attribute "%s"' % (e, checkArgName))
def testAttributes(self): # test that exception attributes are happy exceptionList = [ (BaseException, (), { 'message': '', 'args': () }), (BaseException, (1, ), { 'message': 1, 'args': (1, ) }), (BaseException, ('foo', ), { 'message': 'foo', 'args': ('foo', ) }), (BaseException, ('foo', 1), { 'message': '', 'args': ('foo', 1) }), (SystemExit, ('foo', ), { 'message': 'foo', 'args': ('foo', ), 'code': 'foo' }), (IOError, ('foo', ), { 'message': 'foo', 'args': ('foo', ), 'filename': None, 'errno': None, 'strerror': None }), (IOError, ('foo', 'bar'), { 'message': '', 'args': ('foo', 'bar'), 'filename': None, 'errno': 'foo', 'strerror': 'bar' }), (IOError, ('foo', 'bar', 'baz'), { 'message': '', 'args': ('foo', 'bar'), 'filename': 'baz', 'errno': 'foo', 'strerror': 'bar' }), (IOError, ('foo', 'bar', 'baz', 'quux'), { 'message': '', 'args': ('foo', 'bar', 'baz', 'quux') }), (EnvironmentError, ('errnoStr', 'strErrorStr', 'filenameStr'), { 'message': '', 'args': ('errnoStr', 'strErrorStr'), 'strerror': 'strErrorStr', 'errno': 'errnoStr', 'filename': 'filenameStr' }), (EnvironmentError, (1, 'strErrorStr', 'filenameStr'), { 'message': '', 'args': (1, 'strErrorStr'), 'errno': 1, 'strerror': 'strErrorStr', 'filename': 'filenameStr' }), (SyntaxError, (), { 'message': '', 'msg': None, 'text': None, 'filename': None, 'lineno': None, 'offset': None, 'print_file_and_line': None }), (SyntaxError, ('msgStr', ), { 'message': 'msgStr', 'args': ('msgStr', ), 'text': None, 'print_file_and_line': None, 'msg': 'msgStr', 'filename': None, 'lineno': None, 'offset': None }), (SyntaxError, ('msgStr', ('filenameStr', 'linenoStr', 'offsetStr', 'textStr')), { 'message': '', 'offset': 'offsetStr', 'text': 'textStr', 'args': ('msgStr', ('filenameStr', 'linenoStr', 'offsetStr', 'textStr')), 'print_file_and_line': None, 'msg': 'msgStr', 'filename': 'filenameStr', 'lineno': 'linenoStr' }), (SyntaxError, ('msgStr', 'filenameStr', 'linenoStr', 'offsetStr', 'textStr', 'print_file_and_lineStr'), { 'message': '', 'text': None, 'args': ('msgStr', 'filenameStr', 'linenoStr', 'offsetStr', 'textStr', 'print_file_and_lineStr'), 'print_file_and_line': None, 'msg': 'msgStr', 'filename': None, 'lineno': None, 'offset': None }), (UnicodeError, (), { 'message': '', 'args': (), }), (UnicodeEncodeError, ('ascii', u'a', 0, 1, 'ordinal not in range'), { 'message': '', 'args': ('ascii', u'a', 0, 1, 'ordinal not in range'), 'encoding': 'ascii', 'object': u'a', 'start': 0, 'reason': 'ordinal not in range' }), (UnicodeDecodeError, ('ascii', '\xff', 0, 1, 'ordinal not in range'), { 'message': '', 'args': ('ascii', '\xff', 0, 1, 'ordinal not in range'), 'encoding': 'ascii', 'object': '\xff', 'start': 0, 'reason': 'ordinal not in range' }), (UnicodeTranslateError, (u"\u3042", 0, 1, "ouch"), { 'message': '', 'args': (u'\u3042', 0, 1, 'ouch'), 'object': u'\u3042', 'reason': 'ouch', 'start': 0, 'end': 1 }), ] try: exceptionList.append( (WindowsError, (1, 'strErrorStr', 'filenameStr'), { 'message': '', 'args': (1, 'strErrorStr'), 'strerror': 'strErrorStr', 'winerror': 1, 'errno': 22, 'filename': 'filenameStr' })) except NameError: pass with warnings.catch_warnings(): ignore_message_warning() for exc, args, expected in exceptionList: try: raise exc(*args) except BaseException, e: if type(e) is not exc: raise # Verify module name self.assertEquals(type(e).__module__, 'exceptions') # Verify no ref leaks in Exc_str() s = str(e) for checkArgName in expected: self.assertEquals( repr(getattr(e, checkArgName)), repr(expected[checkArgName]), 'exception "%s", attribute "%s"' % (repr(e), checkArgName)) # test for pickling support for p in pickle, cPickle: for protocol in range(p.HIGHEST_PROTOCOL + 1): new = p.loads(p.dumps(e, protocol)) for checkArgName in expected: got = repr(getattr(new, checkArgName)) want = repr(expected[checkArgName]) self.assertEquals( got, want, 'pickled "%r", attribute "%s"' % (e, checkArgName))