| 940 | dispatch[PickleBuffer] = save_picklebuffer |
| 941 | |
| 942 | def save_str(self, obj): |
| 943 | if self.bin: |
| 944 | encoded = obj.encode('utf-8', 'surrogatepass') |
| 945 | n = len(encoded) |
| 946 | if n <= 0xff and self.proto >= 4: |
| 947 | self.write(SHORT_BINUNICODE + pack("<B", n) + encoded) |
| 948 | elif n > 0xffffffff and self.proto >= 4: |
| 949 | self._write_large_bytes(BINUNICODE8 + pack("<Q", n), encoded) |
| 950 | elif n >= self.framer._FRAME_SIZE_TARGET: |
| 951 | self._write_large_bytes(BINUNICODE + pack("<I", n), encoded) |
| 952 | else: |
| 953 | self.write(BINUNICODE + pack("<I", n) + encoded) |
| 954 | else: |
| 955 | # Escape what raw-unicode-escape doesn't, but memoize the original. |
| 956 | tmp = obj.replace("\\", "\\u005c") |
| 957 | tmp = tmp.replace("\0", "\\u0000") |
| 958 | tmp = tmp.replace("\n", "\\u000a") |
| 959 | tmp = tmp.replace("\r", "\\u000d") |
| 960 | tmp = tmp.replace("\x1a", "\\u001a") # EOF on DOS |
| 961 | self.write(UNICODE + tmp.encode('raw-unicode-escape') + b'\n') |
| 962 | self.memoize(obj) |
| 963 | dispatch[str] = save_str |
| 964 | |
| 965 | def save_tuple(self, obj): |