| 849 | dispatch[PickleBuffer] = save_picklebuffer |
| 850 | |
| 851 | def save_str(self, obj): |
| 852 | if self.bin: |
| 853 | encoded = obj.encode('utf-8', 'surrogatepass') |
| 854 | n = len(encoded) |
| 855 | if n <= 0xff and self.proto >= 4: |
| 856 | self.write(SHORT_BINUNICODE + pack("<B", n) + encoded) |
| 857 | elif n > 0xffffffff and self.proto >= 4: |
| 858 | self._write_large_bytes(BINUNICODE8 + pack("<Q", n), encoded) |
| 859 | elif n >= self.framer._FRAME_SIZE_TARGET: |
| 860 | self._write_large_bytes(BINUNICODE + pack("<I", n), encoded) |
| 861 | else: |
| 862 | self.write(BINUNICODE + pack("<I", n) + encoded) |
| 863 | else: |
| 864 | # Escape what raw-unicode-escape doesn't, but memoize the original. |
| 865 | tmp = obj.replace("\\", "\\u005c") |
| 866 | tmp = tmp.replace("\0", "\\u0000") |
| 867 | tmp = tmp.replace("\n", "\\u000a") |
| 868 | tmp = tmp.replace("\r", "\\u000d") |
| 869 | tmp = tmp.replace("\x1a", "\\u001a") # EOF on DOS |
| 870 | self.write(UNICODE + tmp.encode('raw-unicode-escape') + b'\n') |
| 871 | self.memoize(obj) |
| 872 | dispatch[str] = save_str |
| 873 | |
| 874 | def save_tuple(self, obj): |