diff --git a/Lib/pickle.py b/Lib/pickle.py index 25a5a55626e..b812e675c5a 100644 --- a/Lib/pickle.py +++ b/Lib/pickle.py @@ -45,7 +45,6 @@ compatible_formats = ["1.0", # Original protocol 0 "2.0", # Protocol 2 ] # Old format versions we can read -mdumps = marshal.dumps mloads = marshal.loads class PickleError(Exception): @@ -220,25 +219,22 @@ class Pickler: self.memo[id(obj)] = memo_len, obj # Return a PUT (BINPUT, LONG_BINPUT) opcode string, with argument i. - def put(self, i): + def put(self, i, pack=struct.pack): if self.bin: - s = mdumps(i)[1:] if i < 256: - return BINPUT + s[0] - - return LONG_BINPUT + s + return BINPUT + chr(i) + else: + return LONG_BINPUT + pack("= 0: + if object < 0xff: + self.write(BININT1 + chr(object)) + return + if object < 0xffff: + self.write(BININT2 + chr(object&0xff) + chr(object>>8)) + return + # Next check for 4-byte signed ints: high_bits = object >> 31 # note that Python shift sign-extends if high_bits == 0 or high_bits == -1: # All high bits are copies of bit 2**31, so the value # fits in a 4-byte signed int. - i = mdumps(object)[1:] - assert len(i) == 4 - if i[-2:] == '\000\000': # fits in 2-byte unsigned int - if i[-3] == '\000': # fits in 1-byte unsigned int - self.write(BININT1 + i[0]) - else: - self.write(BININT2 + i[:2]) - else: - self.write(BININT + i) + self.write(BININT + pack("