summaryrefslogtreecommitdiffstats
path: root/Lib/pickle.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2007-01-14 03:31:43 (GMT)
committerGuido van Rossum <guido@python.org>2007-01-14 03:31:43 (GMT)
commitddefaf31b366ea84250fc5090837c2b764a04102 (patch)
treeab3d7b5172f4e6a064165468fc70beb41bdca1d3 /Lib/pickle.py
parent5b787e8bc2dbda5583eee039cb6a6e47c8d8a034 (diff)
downloadcpython-ddefaf31b366ea84250fc5090837c2b764a04102.zip
cpython-ddefaf31b366ea84250fc5090837c2b764a04102.tar.gz
cpython-ddefaf31b366ea84250fc5090837c2b764a04102.tar.bz2
Merged the int/long unification branch, by very crude means (sorry Thomas!).
I banged on the code (beyond what's in that branch) to make fewer tests fail; the only tests that fail now are: test_descr -- can't pickle ints?! test_pickletools -- ??? test_socket -- See python.org/sf/1619659 test_sqlite -- ??? I'll deal with those later.
Diffstat (limited to 'Lib/pickle.py')
-rw-r--r--Lib/pickle.py22
1 files changed, 21 insertions, 1 deletions
diff --git a/Lib/pickle.py b/Lib/pickle.py
index b32ee83..85c79d3 100644
--- a/Lib/pickle.py
+++ b/Lib/pickle.py
@@ -456,9 +456,29 @@ class Pickler:
return
# Text pickle, or int too big to fit in signed 4-byte format.
self.write(INT + repr(obj) + '\n')
- dispatch[IntType] = save_int
+ # XXX save_int is merged into save_long
+ # dispatch[IntType] = save_int
def save_long(self, obj, pack=struct.pack):
+ if self.bin:
+ # If the int is small enough to fit in a signed 4-byte 2's-comp
+ # format, we can store it more efficiently than the general
+ # case.
+ # First one- and two-byte unsigned ints:
+ if obj >= 0:
+ if obj <= 0xff:
+ self.write(BININT1 + chr(obj))
+ return
+ if obj <= 0xffff:
+ self.write("%c%c%c" % (BININT2, obj&0xff, obj>>8))
+ return
+ # Next check for 4-byte signed ints:
+ high_bits = obj >> 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.
+ self.write(BININT + pack("<i", obj))
+ return
if self.proto >= 2:
bytes = encode_long(obj)
n = len(bytes)