Return a bytestring or bytes-like representation of the value
(self, value)
| 22 | |
| 23 | class Encoder(_Encoder): |
| 24 | def encode(self, value): |
| 25 | "Return a bytestring or bytes-like representation of the value" |
| 26 | if isinstance(value, (bytes, memoryview)): |
| 27 | return value |
| 28 | # elif isinstance(value, bool): |
| 29 | # # special case bool since it is a subclass of int |
| 30 | # raise DataError( |
| 31 | # "Invalid input of type: 'bool'. Convert to a " |
| 32 | # "bytes, string, int or float first." |
| 33 | # ) |
| 34 | elif isinstance(value, float): |
| 35 | value = repr(value).encode() |
| 36 | elif isinstance(value, int): |
| 37 | # python 2 repr() on longs is '123L', so use str() instead |
| 38 | value = str(value).encode() |
| 39 | elif isinstance(value, (list, dict, tuple)): |
| 40 | value = str(value) |
| 41 | elif not isinstance(value, str): |
| 42 | # a value we don't know how to deal with. throw an error |
| 43 | typename = type(value).__name__ |
| 44 | raise DataError( |
| 45 | "Invalid input of type: '%s'. Convert to a " |
| 46 | "bytes, string, int or float first." % typename |
| 47 | ) |
| 48 | if isinstance(value, str): |
| 49 | value = value.encode(self.encoding, self.encoding_errors) |
| 50 | return value |
| 51 | |
| 52 | |
| 53 | redis.connection.Encoder = Encoder |
no outgoing calls
no test coverage detected