def __init__(self, *arguments, **keywords): if isinstance(arguments[0], (str, unicode)): # Old API: raise UnicodeException("error: %s", err) # deprecationWarning( # "UnicodeException(format, arguments): " # "missing application, component and error code", # 3) self.application = 0 self.component = 0 self.error_code = 0 message = arguments[0] arguments = arguments[1:] else: # New API: UnicodeException(app, component, ...) self.application = arguments[0] self.component = arguments[1] if isinstance(arguments[2], (str, unicode)): # UnicodeException(app, component, "format", ...): missing code # deprecationWarning( # "UnicodeException(application, component, format, arguments): " # "missing error code", # 4) self.error_code = 0 message = arguments[2] arguments = arguments[3:] else: # UnicodeException(app, component, errcode, "format", ...): correct self.error_code = arguments[2] message = arguments[3] arguments = arguments[4:] self.additionals = [] # Check arguments / keywords if arguments and keywords: self.additionals.append( u"You can not use arguments *and* keywords with UnicodeException!") arguments = None keywords = None # Convert message to Unicode self.unicode_message = toUnicode(message) self.format = self.unicode_message # Make sure that arguments are serialisable, if no convert them to # serialisable type if keywords: for key, value in keywords.iteritems(): try: checkSimpleType(value) except TransportError, err: keywords[key] = self.formatArgument(value) self.additionals.append( u"The keyword argument %r is unserializable: %s" % (key, err))
def formatArgument(self, value): # Try as unicode characters try: text = unicode(value) checkSimpleType(text) return text except (UnicodeError, TransportError): pass # Try as byte string and then reconvert to Unicode try: text = str(value) text = toUnicode(text) checkSimpleType(text) return text except TransportError: pass # Last chance: replace the value return u"[ERROR]"