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 iteritems(arg)]) elif isinstance(arg, bytes): arg = self.decode(arg) # If UnicodeDecodeError, binary data is expected. Return value # as is. if not isinstance(arg, bytes): arg = self.encode(arg) elif isinstance(arg, UNICODE_TYPE): arg = arg.encode(self._encoding) elif INode.providedBy(arg): arg = dict([self.encode(t) for t in iteritems(arg)]) return arg
def update(self, *args, **kw): if len(args) > 1: msg = 'At most one positional argument, not: {}.'.format(len(args)) raise TypeError(msg) if args: data = args[0] if hasattr(data, ITER_FUNC): data = iteritems(data) for key, val in data: self[key] = val for key, val in iteritems(kw): self[key] = val
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 iteritems(arg)]) elif isinstance(arg, bytes): 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 iteritems(arg)]) return arg
def update(self, data=(), **kw): for key, value in data: self[key] = value for key, value in iteritems(kw): self[key] = value
def update(self, data=(), **kw): for key, _ in data: self[key] = object() for key, _ in iteritems(kw): self[key] = object()