def encode(self, arg): """Return an encoded copy of the argument - strs are decoded and reencode to make sure they conform to the encoding. XXX: makes no sence, especially because a UnicodeDecodeError ends up in a recursion error due to re-trying to encode. See below. Added condition to return if str is still str after decoding. This behavior should be removed completely. - unicodes are encoded as str according to encoding - lists/tuples/dicts are recursively worked on - everything else is left untouched """ if isinstance(arg, (list, tuple)): arg = arg.__class__(map(self.encode, arg)) elif isinstance(arg, dict): arg = dict([self.encode(t) for t in arg.iteritems()]) elif isinstance(arg, str): arg = self.decode(arg) # If UnicodeDecodeError, binary data is expected. Return value # as is. if not isinstance(arg, str): arg = self.encode(arg) elif isinstance(arg, unicode): arg = arg.encode(self._encoding) elif INode.providedBy(arg): arg = dict([self.encode(t) for t in arg.iteritems()]) return arg
def decode(self, arg): if isinstance(arg, (list, tuple)): arg = arg.__class__(map(self.decode, arg)) elif isinstance(arg, dict): arg = dict([self.decode(t) for t in arg.iteritems()]) elif isinstance(arg, str): try: arg = arg.decode(self._encoding) except UnicodeDecodeError: # in soft mode we leave the string, otherwise we raise the # exception if not self._soft: raise elif INode.providedBy(arg): arg = dict([self.decode(t) for t in arg.iteritems()]) return arg
def encode(self, arg): """Return an encoded copy of the argument - strs are decoded and reencode to make sure they conform to the encoding - unicodes are encoded as str according to encoding - lists/tuples/dicts are recursively worked on - everything else is left untouched """ if isinstance(arg, (list, tuple)): arg = arg.__class__(map(self.encode, arg)) elif isinstance(arg, dict): arg = dict([self.encode(t) for t in arg.iteritems()]) elif isinstance(arg, str): arg = self.encode(self.decode(arg)) elif isinstance(arg, unicode): arg = arg.encode(self._encoding) elif INode.providedBy(arg): arg = dict([self.encode(t) for t in arg.iteritems()]) return arg