| 804 | dispatch[bool] = save_bool |
| 805 | |
| 806 | def save_long(self, obj): |
| 807 | if self.bin: |
| 808 | # If the int is small enough to fit in a signed 4-byte 2's-comp |
| 809 | # format, we can store it more efficiently than the general |
| 810 | # case. |
| 811 | # First one- and two-byte unsigned ints: |
| 812 | if obj >= 0: |
| 813 | if obj <= 0xff: |
| 814 | self.write(BININT1 + pack("<B", obj)) |
| 815 | return |
| 816 | if obj <= 0xffff: |
| 817 | self.write(BININT2 + pack("<H", obj)) |
| 818 | return |
| 819 | # Next check for 4-byte signed ints: |
| 820 | if -0x80000000 <= obj <= 0x7fffffff: |
| 821 | self.write(BININT + pack("<i", obj)) |
| 822 | return |
| 823 | if self.proto >= 2: |
| 824 | encoded = encode_long(obj) |
| 825 | n = len(encoded) |
| 826 | if n < 256: |
| 827 | self.write(LONG1 + pack("<B", n) + encoded) |
| 828 | else: |
| 829 | self.write(LONG4 + pack("<i", n) + encoded) |
| 830 | return |
| 831 | if -0x80000000 <= obj <= 0x7fffffff: |
| 832 | self.write(INT + repr(obj).encode("ascii") + b'\n') |
| 833 | else: |
| 834 | self.write(LONG + repr(obj).encode("ascii") + b'L\n') |
| 835 | dispatch[int] = save_long |
| 836 | |
| 837 | def save_float(self, obj): |