summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_pickletools.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2014-12-16 16:00:56 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2014-12-16 16:00:56 (GMT)
commit05dadcfb28b815c9558fe2a6a74cd3ce7df62577 (patch)
tree16b06d95b5343b02e758d64d8b7ad13ca57fd145 /Lib/test/test_pickletools.py
parentdf9386940a0104ba4dce1beee38848bc44a17036 (diff)
downloadcpython-05dadcfb28b815c9558fe2a6a74cd3ce7df62577.zip
cpython-05dadcfb28b815c9558fe2a6a74cd3ce7df62577.tar.gz
cpython-05dadcfb28b815c9558fe2a6a74cd3ce7df62577.tar.bz2
Issue #19858: pickletools.optimize() now aware of the MEMOIZE opcode, can
produce more compact result and no longer produces invalid output if input data contains MEMOIZE opcodes together with PUT or BINPUT opcodes.
Diffstat (limited to 'Lib/test/test_pickletools.py')
-rw-r--r--Lib/test/test_pickletools.py43
1 files changed, 43 insertions, 0 deletions
diff --git a/Lib/test/test_pickletools.py b/Lib/test/test_pickletools.py
index d37ac26..bbe6875 100644
--- a/Lib/test/test_pickletools.py
+++ b/Lib/test/test_pickletools.py
@@ -1,3 +1,4 @@
+import struct
import pickle
import pickletools
from test import support
@@ -15,6 +16,48 @@ class OptimizedPickleTests(AbstractPickleTests, AbstractPickleModuleTests):
# Test relies on precise output of dumps()
test_pickle_to_2x = None
+ def test_optimize_long_binget(self):
+ data = [str(i) for i in range(257)]
+ data.append(data[-1])
+ for proto in range(pickle.HIGHEST_PROTOCOL + 1):
+ pickled = pickle.dumps(data, proto)
+ unpickled = pickle.loads(pickled)
+ self.assertEqual(unpickled, data)
+ self.assertIs(unpickled[-1], unpickled[-2])
+
+ pickled2 = pickletools.optimize(pickled)
+ unpickled2 = pickle.loads(pickled2)
+ self.assertEqual(unpickled2, data)
+ self.assertIs(unpickled2[-1], unpickled2[-2])
+ self.assertNotIn(pickle.LONG_BINGET, pickled2)
+ self.assertNotIn(pickle.LONG_BINPUT, pickled2)
+
+ def test_optimize_binput_and_memoize(self):
+ pickled = (b'\x80\x04\x95\x15\x00\x00\x00\x00\x00\x00\x00'
+ b']\x94(\x8c\x04spamq\x01\x8c\x03ham\x94h\x02e.')
+ # 0: \x80 PROTO 4
+ # 2: \x95 FRAME 21
+ # 11: ] EMPTY_LIST
+ # 12: \x94 MEMOIZE
+ # 13: ( MARK
+ # 14: \x8c SHORT_BINUNICODE 'spam'
+ # 20: q BINPUT 1
+ # 22: \x8c SHORT_BINUNICODE 'ham'
+ # 27: \x94 MEMOIZE
+ # 28: h BINGET 2
+ # 30: e APPENDS (MARK at 13)
+ # 31: . STOP
+ self.assertIn(pickle.BINPUT, pickled)
+ unpickled = pickle.loads(pickled)
+ self.assertEqual(unpickled, ['spam', 'ham', 'ham'])
+ self.assertIs(unpickled[1], unpickled[2])
+
+ pickled2 = pickletools.optimize(pickled)
+ unpickled2 = pickle.loads(pickled2)
+ self.assertEqual(unpickled2, ['spam', 'ham', 'ham'])
+ self.assertIs(unpickled2[1], unpickled2[2])
+ self.assertNotIn(pickle.BINPUT, pickled2)
+
def test_main():
support.run_unittest(OptimizedPickleTests)