~ubuntu-branches/ubuntu/precise/python3.2/precise-proposed

« back to all changes in this revision

Viewing changes to Lib/test/pickletester.py

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2012-03-09 18:40:39 UTC
  • mfrom: (30.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20120309184039-j3yk2emxr1plyo21
Tags: 3.2.3~rc1-1
* Python 3.2.3 release candidate 1.
* Update to 20120309 from the 3.2 branch.
* Fix libpython.a symlink. Closes: #660146.
* Build-depend on xauth.
* Run the gdb tests for the debug build only.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
import unittest
3
3
import pickle
4
4
import pickletools
 
5
import sys
5
6
import copyreg
6
7
from http.cookies import SimpleCookie
7
8
 
8
 
from test.support import TestFailed, TESTFN, run_with_locale
 
9
from test.support import (
 
10
    TestFailed, TESTFN, run_with_locale,
 
11
    _2G, _4G, bigmemtest,
 
12
    )
9
13
 
10
14
from pickle import bytes_types
11
15
 
14
18
# kind of outer loop.
15
19
protocols = range(pickle.HIGHEST_PROTOCOL + 1)
16
20
 
 
21
character_size = 4 if sys.maxunicode > 0xFFFF else 2
 
22
 
17
23
 
18
24
# Return True if opcode code appears in the pickle, else False.
19
25
def opcode_in_pickle(code, pickle):
115
121
class use_metaclass(object, metaclass=metaclass):
116
122
    pass
117
123
 
 
124
class pickling_metaclass(type):
 
125
    def __eq__(self, other):
 
126
        return (type(self) == type(other) and
 
127
                self.reduce_args == other.reduce_args)
 
128
 
 
129
    def __reduce__(self):
 
130
        return (create_dynamic_class, self.reduce_args)
 
131
 
 
132
def create_dynamic_class(name, bases):
 
133
    result = pickling_metaclass(name, bases, dict())
 
134
    result.reduce_args = (name, bases)
 
135
    return result
 
136
 
118
137
# DATA0 .. DATA2 are the pickles we expect under the various protocols, for
119
138
# the object returned by create_data().
120
139
 
617
636
 
618
637
    def test_bytes(self):
619
638
        for proto in protocols:
620
 
            for u in b'', b'xyz', b'xyz'*100:
621
 
                p = self.dumps(u)
622
 
                self.assertEqual(self.loads(p), u)
 
639
            for s in b'', b'xyz', b'xyz'*100:
 
640
                p = self.dumps(s)
 
641
                self.assertEqual(self.loads(p), s)
 
642
            for s in [bytes([i]) for i in range(256)]:
 
643
                p = self.dumps(s)
 
644
                self.assertEqual(self.loads(p), s)
 
645
            for s in [bytes([i, i]) for i in range(256)]:
 
646
                p = self.dumps(s)
 
647
                self.assertEqual(self.loads(p), s)
623
648
 
624
649
    def test_ints(self):
625
650
        import sys
689
714
            b = self.loads(s)
690
715
            self.assertEqual(a.__class__, b.__class__)
691
716
 
 
717
    def test_dynamic_class(self):
 
718
        a = create_dynamic_class("my_dynamic_class", (object,))
 
719
        copyreg.pickle(pickling_metaclass, pickling_metaclass.__reduce__)
 
720
        for proto in protocols:
 
721
            s = self.dumps(a, proto)
 
722
            b = self.loads(s)
 
723
            self.assertEqual(a, b)
 
724
 
692
725
    def test_structseq(self):
693
726
        import time
694
727
        import os
1098
1131
        empty = self.loads(b'\x80\x03U\x00q\x00.', encoding='koi8-r')
1099
1132
        self.assertEqual(empty, '')
1100
1133
 
 
1134
    def check_negative_32b_binXXX(self, dumped):
 
1135
        if sys.maxsize > 2**32:
 
1136
            self.skipTest("test is only meaningful on 32-bit builds")
 
1137
        # XXX Pure Python pickle reads lengths as signed and passes
 
1138
        # them directly to read() (hence the EOFError)
 
1139
        with self.assertRaises((pickle.UnpicklingError, EOFError,
 
1140
                                ValueError, OverflowError)):
 
1141
            self.loads(dumped)
 
1142
 
 
1143
    def test_negative_32b_binbytes(self):
 
1144
        # On 32-bit builds, a BINBYTES of 2**31 or more is refused
 
1145
        self.check_negative_32b_binXXX(b'\x80\x03B\xff\xff\xff\xffxyzq\x00.')
 
1146
 
 
1147
    def test_negative_32b_binunicode(self):
 
1148
        # On 32-bit builds, a BINUNICODE of 2**31 or more is refused
 
1149
        self.check_negative_32b_binXXX(b'\x80\x03X\xff\xff\xff\xffxyzq\x00.')
 
1150
 
 
1151
    def test_negative_put(self):
 
1152
        # Issue #12847
 
1153
        dumped = b'Va\np-1\n.'
 
1154
        self.assertRaises(ValueError, self.loads, dumped)
 
1155
 
 
1156
    def test_negative_32b_binput(self):
 
1157
        # Issue #12847
 
1158
        if sys.maxsize > 2**32:
 
1159
            self.skipTest("test is only meaningful on 32-bit builds")
 
1160
        dumped = b'\x80\x03X\x01\x00\x00\x00ar\xff\xff\xff\xff.'
 
1161
        self.assertRaises(ValueError, self.loads, dumped)
 
1162
 
 
1163
 
 
1164
class BigmemPickleTests(unittest.TestCase):
 
1165
 
 
1166
    # Binary protocols can serialize longs of up to 2GB-1
 
1167
 
 
1168
    @bigmemtest(size=_2G, memuse=1 + 1, dry_run=False)
 
1169
    def test_huge_long_32b(self, size):
 
1170
        data = 1 << (8 * size)
 
1171
        try:
 
1172
            for proto in protocols:
 
1173
                if proto < 2:
 
1174
                    continue
 
1175
                with self.assertRaises((ValueError, OverflowError)):
 
1176
                    self.dumps(data, protocol=proto)
 
1177
        finally:
 
1178
            data = None
 
1179
 
 
1180
    # Protocol 3 can serialize up to 4GB-1 as a bytes object
 
1181
    # (older protocols don't have a dedicated opcode for bytes and are
 
1182
    # too inefficient)
 
1183
 
 
1184
    @bigmemtest(size=_2G, memuse=1 + 1, dry_run=False)
 
1185
    def test_huge_bytes_32b(self, size):
 
1186
        data = b"abcd" * (size // 4)
 
1187
        try:
 
1188
            for proto in protocols:
 
1189
                if proto < 3:
 
1190
                    continue
 
1191
                try:
 
1192
                    pickled = self.dumps(data, protocol=proto)
 
1193
                    self.assertTrue(b"abcd" in pickled[:15])
 
1194
                    self.assertTrue(b"abcd" in pickled[-15:])
 
1195
                finally:
 
1196
                    pickled = None
 
1197
        finally:
 
1198
            data = None
 
1199
 
 
1200
    @bigmemtest(size=_4G, memuse=1 + 1, dry_run=False)
 
1201
    def test_huge_bytes_64b(self, size):
 
1202
        data = b"a" * size
 
1203
        try:
 
1204
            for proto in protocols:
 
1205
                if proto < 3:
 
1206
                    continue
 
1207
                with self.assertRaises((ValueError, OverflowError)):
 
1208
                    self.dumps(data, protocol=proto)
 
1209
        finally:
 
1210
            data = None
 
1211
 
 
1212
    # All protocols use 1-byte per printable ASCII character; we add another
 
1213
    # byte because the encoded form has to be copied into the internal buffer.
 
1214
 
 
1215
    @bigmemtest(size=_2G, memuse=2 + character_size, dry_run=False)
 
1216
    def test_huge_str_32b(self, size):
 
1217
        data = "abcd" * (size // 4)
 
1218
        try:
 
1219
            for proto in protocols:
 
1220
                try:
 
1221
                    pickled = self.dumps(data, protocol=proto)
 
1222
                    self.assertTrue(b"abcd" in pickled[:15])
 
1223
                    self.assertTrue(b"abcd" in pickled[-15:])
 
1224
                finally:
 
1225
                    pickled = None
 
1226
        finally:
 
1227
            data = None
 
1228
 
 
1229
    # BINUNICODE (protocols 1, 2 and 3) cannot carry more than
 
1230
    # 2**32 - 1 bytes of utf-8 encoded unicode.
 
1231
 
 
1232
    @bigmemtest(size=_4G, memuse=1 + character_size, dry_run=False)
 
1233
    def test_huge_str_64b(self, size):
 
1234
        data = "a" * size
 
1235
        try:
 
1236
            for proto in protocols:
 
1237
                if proto == 0:
 
1238
                    continue
 
1239
                with self.assertRaises((ValueError, OverflowError)):
 
1240
                    self.dumps(data, protocol=proto)
 
1241
        finally:
 
1242
            data = None
 
1243
 
 
1244
 
1101
1245
# Test classes for reduce_ex
1102
1246
 
1103
1247
class REX_one(object):