| 1 | n/a | from test import support |
|---|
| 2 | n/a | import array |
|---|
| 3 | n/a | import io |
|---|
| 4 | n/a | import marshal |
|---|
| 5 | n/a | import sys |
|---|
| 6 | n/a | import unittest |
|---|
| 7 | n/a | import os |
|---|
| 8 | n/a | import types |
|---|
| 9 | n/a | |
|---|
| 10 | n/a | try: |
|---|
| 11 | n/a | import _testcapi |
|---|
| 12 | n/a | except ImportError: |
|---|
| 13 | n/a | _testcapi = None |
|---|
| 14 | n/a | |
|---|
| 15 | n/a | class HelperMixin: |
|---|
| 16 | n/a | def helper(self, sample, *extra): |
|---|
| 17 | n/a | new = marshal.loads(marshal.dumps(sample, *extra)) |
|---|
| 18 | n/a | self.assertEqual(sample, new) |
|---|
| 19 | n/a | try: |
|---|
| 20 | n/a | with open(support.TESTFN, "wb") as f: |
|---|
| 21 | n/a | marshal.dump(sample, f, *extra) |
|---|
| 22 | n/a | with open(support.TESTFN, "rb") as f: |
|---|
| 23 | n/a | new = marshal.load(f) |
|---|
| 24 | n/a | self.assertEqual(sample, new) |
|---|
| 25 | n/a | finally: |
|---|
| 26 | n/a | support.unlink(support.TESTFN) |
|---|
| 27 | n/a | |
|---|
| 28 | n/a | class IntTestCase(unittest.TestCase, HelperMixin): |
|---|
| 29 | n/a | def test_ints(self): |
|---|
| 30 | n/a | # Test a range of Python ints larger than the machine word size. |
|---|
| 31 | n/a | n = sys.maxsize ** 2 |
|---|
| 32 | n/a | while n: |
|---|
| 33 | n/a | for expected in (-n, n): |
|---|
| 34 | n/a | self.helper(expected) |
|---|
| 35 | n/a | n = n >> 1 |
|---|
| 36 | n/a | |
|---|
| 37 | n/a | def test_bool(self): |
|---|
| 38 | n/a | for b in (True, False): |
|---|
| 39 | n/a | self.helper(b) |
|---|
| 40 | n/a | |
|---|
| 41 | n/a | class FloatTestCase(unittest.TestCase, HelperMixin): |
|---|
| 42 | n/a | def test_floats(self): |
|---|
| 43 | n/a | # Test a few floats |
|---|
| 44 | n/a | small = 1e-25 |
|---|
| 45 | n/a | n = sys.maxsize * 3.7e250 |
|---|
| 46 | n/a | while n > small: |
|---|
| 47 | n/a | for expected in (-n, n): |
|---|
| 48 | n/a | self.helper(float(expected)) |
|---|
| 49 | n/a | n /= 123.4567 |
|---|
| 50 | n/a | |
|---|
| 51 | n/a | f = 0.0 |
|---|
| 52 | n/a | s = marshal.dumps(f, 2) |
|---|
| 53 | n/a | got = marshal.loads(s) |
|---|
| 54 | n/a | self.assertEqual(f, got) |
|---|
| 55 | n/a | # and with version <= 1 (floats marshalled differently then) |
|---|
| 56 | n/a | s = marshal.dumps(f, 1) |
|---|
| 57 | n/a | got = marshal.loads(s) |
|---|
| 58 | n/a | self.assertEqual(f, got) |
|---|
| 59 | n/a | |
|---|
| 60 | n/a | n = sys.maxsize * 3.7e-250 |
|---|
| 61 | n/a | while n < small: |
|---|
| 62 | n/a | for expected in (-n, n): |
|---|
| 63 | n/a | f = float(expected) |
|---|
| 64 | n/a | self.helper(f) |
|---|
| 65 | n/a | self.helper(f, 1) |
|---|
| 66 | n/a | n *= 123.4567 |
|---|
| 67 | n/a | |
|---|
| 68 | n/a | class StringTestCase(unittest.TestCase, HelperMixin): |
|---|
| 69 | n/a | def test_unicode(self): |
|---|
| 70 | n/a | for s in ["", "Andr\xe8 Previn", "abc", " "*10000]: |
|---|
| 71 | n/a | self.helper(marshal.loads(marshal.dumps(s))) |
|---|
| 72 | n/a | |
|---|
| 73 | n/a | def test_string(self): |
|---|
| 74 | n/a | for s in ["", "Andr\xe8 Previn", "abc", " "*10000]: |
|---|
| 75 | n/a | self.helper(s) |
|---|
| 76 | n/a | |
|---|
| 77 | n/a | def test_bytes(self): |
|---|
| 78 | n/a | for s in [b"", b"Andr\xe8 Previn", b"abc", b" "*10000]: |
|---|
| 79 | n/a | self.helper(s) |
|---|
| 80 | n/a | |
|---|
| 81 | n/a | class ExceptionTestCase(unittest.TestCase): |
|---|
| 82 | n/a | def test_exceptions(self): |
|---|
| 83 | n/a | new = marshal.loads(marshal.dumps(StopIteration)) |
|---|
| 84 | n/a | self.assertEqual(StopIteration, new) |
|---|
| 85 | n/a | |
|---|
| 86 | n/a | class CodeTestCase(unittest.TestCase): |
|---|
| 87 | n/a | def test_code(self): |
|---|
| 88 | n/a | co = ExceptionTestCase.test_exceptions.__code__ |
|---|
| 89 | n/a | new = marshal.loads(marshal.dumps(co)) |
|---|
| 90 | n/a | self.assertEqual(co, new) |
|---|
| 91 | n/a | |
|---|
| 92 | n/a | def test_many_codeobjects(self): |
|---|
| 93 | n/a | # Issue2957: bad recursion count on code objects |
|---|
| 94 | n/a | count = 5000 # more than MAX_MARSHAL_STACK_DEPTH |
|---|
| 95 | n/a | codes = (ExceptionTestCase.test_exceptions.__code__,) * count |
|---|
| 96 | n/a | marshal.loads(marshal.dumps(codes)) |
|---|
| 97 | n/a | |
|---|
| 98 | n/a | def test_different_filenames(self): |
|---|
| 99 | n/a | co1 = compile("x", "f1", "exec") |
|---|
| 100 | n/a | co2 = compile("y", "f2", "exec") |
|---|
| 101 | n/a | co1, co2 = marshal.loads(marshal.dumps((co1, co2))) |
|---|
| 102 | n/a | self.assertEqual(co1.co_filename, "f1") |
|---|
| 103 | n/a | self.assertEqual(co2.co_filename, "f2") |
|---|
| 104 | n/a | |
|---|
| 105 | n/a | @support.cpython_only |
|---|
| 106 | n/a | def test_same_filename_used(self): |
|---|
| 107 | n/a | s = """def f(): pass\ndef g(): pass""" |
|---|
| 108 | n/a | co = compile(s, "myfile", "exec") |
|---|
| 109 | n/a | co = marshal.loads(marshal.dumps(co)) |
|---|
| 110 | n/a | for obj in co.co_consts: |
|---|
| 111 | n/a | if isinstance(obj, types.CodeType): |
|---|
| 112 | n/a | self.assertIs(co.co_filename, obj.co_filename) |
|---|
| 113 | n/a | |
|---|
| 114 | n/a | class ContainerTestCase(unittest.TestCase, HelperMixin): |
|---|
| 115 | n/a | d = {'astring': 'foo@bar.baz.spam', |
|---|
| 116 | n/a | 'afloat': 7283.43, |
|---|
| 117 | n/a | 'anint': 2**20, |
|---|
| 118 | n/a | 'ashortlong': 2, |
|---|
| 119 | n/a | 'alist': ['.zyx.41'], |
|---|
| 120 | n/a | 'atuple': ('.zyx.41',)*10, |
|---|
| 121 | n/a | 'aboolean': False, |
|---|
| 122 | n/a | 'aunicode': "Andr\xe8 Previn" |
|---|
| 123 | n/a | } |
|---|
| 124 | n/a | |
|---|
| 125 | n/a | def test_dict(self): |
|---|
| 126 | n/a | self.helper(self.d) |
|---|
| 127 | n/a | |
|---|
| 128 | n/a | def test_list(self): |
|---|
| 129 | n/a | self.helper(list(self.d.items())) |
|---|
| 130 | n/a | |
|---|
| 131 | n/a | def test_tuple(self): |
|---|
| 132 | n/a | self.helper(tuple(self.d.keys())) |
|---|
| 133 | n/a | |
|---|
| 134 | n/a | def test_sets(self): |
|---|
| 135 | n/a | for constructor in (set, frozenset): |
|---|
| 136 | n/a | self.helper(constructor(self.d.keys())) |
|---|
| 137 | n/a | |
|---|
| 138 | n/a | @support.cpython_only |
|---|
| 139 | n/a | def test_empty_frozenset_singleton(self): |
|---|
| 140 | n/a | # marshal.loads() must reuse the empty frozenset singleton |
|---|
| 141 | n/a | obj = frozenset() |
|---|
| 142 | n/a | obj2 = marshal.loads(marshal.dumps(obj)) |
|---|
| 143 | n/a | self.assertIs(obj2, obj) |
|---|
| 144 | n/a | |
|---|
| 145 | n/a | |
|---|
| 146 | n/a | class BufferTestCase(unittest.TestCase, HelperMixin): |
|---|
| 147 | n/a | |
|---|
| 148 | n/a | def test_bytearray(self): |
|---|
| 149 | n/a | b = bytearray(b"abc") |
|---|
| 150 | n/a | self.helper(b) |
|---|
| 151 | n/a | new = marshal.loads(marshal.dumps(b)) |
|---|
| 152 | n/a | self.assertEqual(type(new), bytes) |
|---|
| 153 | n/a | |
|---|
| 154 | n/a | def test_memoryview(self): |
|---|
| 155 | n/a | b = memoryview(b"abc") |
|---|
| 156 | n/a | self.helper(b) |
|---|
| 157 | n/a | new = marshal.loads(marshal.dumps(b)) |
|---|
| 158 | n/a | self.assertEqual(type(new), bytes) |
|---|
| 159 | n/a | |
|---|
| 160 | n/a | def test_array(self): |
|---|
| 161 | n/a | a = array.array('B', b"abc") |
|---|
| 162 | n/a | new = marshal.loads(marshal.dumps(a)) |
|---|
| 163 | n/a | self.assertEqual(new, b"abc") |
|---|
| 164 | n/a | |
|---|
| 165 | n/a | |
|---|
| 166 | n/a | class BugsTestCase(unittest.TestCase): |
|---|
| 167 | n/a | def test_bug_5888452(self): |
|---|
| 168 | n/a | # Simple-minded check for SF 588452: Debug build crashes |
|---|
| 169 | n/a | marshal.dumps([128] * 1000) |
|---|
| 170 | n/a | |
|---|
| 171 | n/a | def test_patch_873224(self): |
|---|
| 172 | n/a | self.assertRaises(Exception, marshal.loads, '0') |
|---|
| 173 | n/a | self.assertRaises(Exception, marshal.loads, 'f') |
|---|
| 174 | n/a | self.assertRaises(Exception, marshal.loads, marshal.dumps(2**65)[:-1]) |
|---|
| 175 | n/a | |
|---|
| 176 | n/a | def test_version_argument(self): |
|---|
| 177 | n/a | # Python 2.4.0 crashes for any call to marshal.dumps(x, y) |
|---|
| 178 | n/a | self.assertEqual(marshal.loads(marshal.dumps(5, 0)), 5) |
|---|
| 179 | n/a | self.assertEqual(marshal.loads(marshal.dumps(5, 1)), 5) |
|---|
| 180 | n/a | |
|---|
| 181 | n/a | def test_fuzz(self): |
|---|
| 182 | n/a | # simple test that it's at least not *totally* trivial to |
|---|
| 183 | n/a | # crash from bad marshal data |
|---|
| 184 | n/a | for c in [chr(i) for i in range(256)]: |
|---|
| 185 | n/a | try: |
|---|
| 186 | n/a | marshal.loads(c) |
|---|
| 187 | n/a | except Exception: |
|---|
| 188 | n/a | pass |
|---|
| 189 | n/a | |
|---|
| 190 | n/a | def test_loads_2x_code(self): |
|---|
| 191 | n/a | s = b'c' + (b'X' * 4*4) + b'{' * 2**20 |
|---|
| 192 | n/a | self.assertRaises(ValueError, marshal.loads, s) |
|---|
| 193 | n/a | |
|---|
| 194 | n/a | def test_loads_recursion(self): |
|---|
| 195 | n/a | s = b'c' + (b'X' * 4*5) + b'{' * 2**20 |
|---|
| 196 | n/a | self.assertRaises(ValueError, marshal.loads, s) |
|---|
| 197 | n/a | |
|---|
| 198 | n/a | def test_recursion_limit(self): |
|---|
| 199 | n/a | # Create a deeply nested structure. |
|---|
| 200 | n/a | head = last = [] |
|---|
| 201 | n/a | # The max stack depth should match the value in Python/marshal.c. |
|---|
| 202 | n/a | if os.name == 'nt' and hasattr(sys, 'gettotalrefcount'): |
|---|
| 203 | n/a | MAX_MARSHAL_STACK_DEPTH = 1000 |
|---|
| 204 | n/a | else: |
|---|
| 205 | n/a | MAX_MARSHAL_STACK_DEPTH = 2000 |
|---|
| 206 | n/a | for i in range(MAX_MARSHAL_STACK_DEPTH - 2): |
|---|
| 207 | n/a | last.append([0]) |
|---|
| 208 | n/a | last = last[-1] |
|---|
| 209 | n/a | |
|---|
| 210 | n/a | # Verify we don't blow out the stack with dumps/load. |
|---|
| 211 | n/a | data = marshal.dumps(head) |
|---|
| 212 | n/a | new_head = marshal.loads(data) |
|---|
| 213 | n/a | # Don't use == to compare objects, it can exceed the recursion limit. |
|---|
| 214 | n/a | self.assertEqual(len(new_head), len(head)) |
|---|
| 215 | n/a | self.assertEqual(len(new_head[0]), len(head[0])) |
|---|
| 216 | n/a | self.assertEqual(len(new_head[-1]), len(head[-1])) |
|---|
| 217 | n/a | |
|---|
| 218 | n/a | last.append([0]) |
|---|
| 219 | n/a | self.assertRaises(ValueError, marshal.dumps, head) |
|---|
| 220 | n/a | |
|---|
| 221 | n/a | def test_exact_type_match(self): |
|---|
| 222 | n/a | # Former bug: |
|---|
| 223 | n/a | # >>> class Int(int): pass |
|---|
| 224 | n/a | # >>> type(loads(dumps(Int()))) |
|---|
| 225 | n/a | # <type 'int'> |
|---|
| 226 | n/a | for typ in (int, float, complex, tuple, list, dict, set, frozenset): |
|---|
| 227 | n/a | # Note: str subclasses are not tested because they get handled |
|---|
| 228 | n/a | # by marshal's routines for objects supporting the buffer API. |
|---|
| 229 | n/a | subtyp = type('subtyp', (typ,), {}) |
|---|
| 230 | n/a | self.assertRaises(ValueError, marshal.dumps, subtyp()) |
|---|
| 231 | n/a | |
|---|
| 232 | n/a | # Issue #1792 introduced a change in how marshal increases the size of its |
|---|
| 233 | n/a | # internal buffer; this test ensures that the new code is exercised. |
|---|
| 234 | n/a | def test_large_marshal(self): |
|---|
| 235 | n/a | size = int(1e6) |
|---|
| 236 | n/a | testString = 'abc' * size |
|---|
| 237 | n/a | marshal.dumps(testString) |
|---|
| 238 | n/a | |
|---|
| 239 | n/a | def test_invalid_longs(self): |
|---|
| 240 | n/a | # Issue #7019: marshal.loads shouldn't produce unnormalized PyLongs |
|---|
| 241 | n/a | invalid_string = b'l\x02\x00\x00\x00\x00\x00\x00\x00' |
|---|
| 242 | n/a | self.assertRaises(ValueError, marshal.loads, invalid_string) |
|---|
| 243 | n/a | |
|---|
| 244 | n/a | def test_multiple_dumps_and_loads(self): |
|---|
| 245 | n/a | # Issue 12291: marshal.load() should be callable multiple times |
|---|
| 246 | n/a | # with interleaved data written by non-marshal code |
|---|
| 247 | n/a | # Adapted from a patch by Engelbert Gruber. |
|---|
| 248 | n/a | data = (1, 'abc', b'def', 1.0, (2, 'a', ['b', b'c'])) |
|---|
| 249 | n/a | for interleaved in (b'', b'0123'): |
|---|
| 250 | n/a | ilen = len(interleaved) |
|---|
| 251 | n/a | positions = [] |
|---|
| 252 | n/a | try: |
|---|
| 253 | n/a | with open(support.TESTFN, 'wb') as f: |
|---|
| 254 | n/a | for d in data: |
|---|
| 255 | n/a | marshal.dump(d, f) |
|---|
| 256 | n/a | if ilen: |
|---|
| 257 | n/a | f.write(interleaved) |
|---|
| 258 | n/a | positions.append(f.tell()) |
|---|
| 259 | n/a | with open(support.TESTFN, 'rb') as f: |
|---|
| 260 | n/a | for i, d in enumerate(data): |
|---|
| 261 | n/a | self.assertEqual(d, marshal.load(f)) |
|---|
| 262 | n/a | if ilen: |
|---|
| 263 | n/a | f.read(ilen) |
|---|
| 264 | n/a | self.assertEqual(positions[i], f.tell()) |
|---|
| 265 | n/a | finally: |
|---|
| 266 | n/a | support.unlink(support.TESTFN) |
|---|
| 267 | n/a | |
|---|
| 268 | n/a | def test_loads_reject_unicode_strings(self): |
|---|
| 269 | n/a | # Issue #14177: marshal.loads() should not accept unicode strings |
|---|
| 270 | n/a | unicode_string = 'T' |
|---|
| 271 | n/a | self.assertRaises(TypeError, marshal.loads, unicode_string) |
|---|
| 272 | n/a | |
|---|
| 273 | n/a | def test_bad_reader(self): |
|---|
| 274 | n/a | class BadReader(io.BytesIO): |
|---|
| 275 | n/a | def readinto(self, buf): |
|---|
| 276 | n/a | n = super().readinto(buf) |
|---|
| 277 | n/a | if n is not None and n > 4: |
|---|
| 278 | n/a | n += 10**6 |
|---|
| 279 | n/a | return n |
|---|
| 280 | n/a | for value in (1.0, 1j, b'0123456789', '0123456789'): |
|---|
| 281 | n/a | self.assertRaises(ValueError, marshal.load, |
|---|
| 282 | n/a | BadReader(marshal.dumps(value))) |
|---|
| 283 | n/a | |
|---|
| 284 | n/a | def _test_eof(self): |
|---|
| 285 | n/a | data = marshal.dumps(("hello", "dolly", None)) |
|---|
| 286 | n/a | for i in range(len(data)): |
|---|
| 287 | n/a | self.assertRaises(EOFError, marshal.loads, data[0: i]) |
|---|
| 288 | n/a | |
|---|
| 289 | n/a | LARGE_SIZE = 2**31 |
|---|
| 290 | n/a | pointer_size = 8 if sys.maxsize > 0xFFFFFFFF else 4 |
|---|
| 291 | n/a | |
|---|
| 292 | n/a | class NullWriter: |
|---|
| 293 | n/a | def write(self, s): |
|---|
| 294 | n/a | pass |
|---|
| 295 | n/a | |
|---|
| 296 | n/a | @unittest.skipIf(LARGE_SIZE > sys.maxsize, "test cannot run on 32-bit systems") |
|---|
| 297 | n/a | class LargeValuesTestCase(unittest.TestCase): |
|---|
| 298 | n/a | def check_unmarshallable(self, data): |
|---|
| 299 | n/a | self.assertRaises(ValueError, marshal.dump, data, NullWriter()) |
|---|
| 300 | n/a | |
|---|
| 301 | n/a | @support.bigmemtest(size=LARGE_SIZE, memuse=2, dry_run=False) |
|---|
| 302 | n/a | def test_bytes(self, size): |
|---|
| 303 | n/a | self.check_unmarshallable(b'x' * size) |
|---|
| 304 | n/a | |
|---|
| 305 | n/a | @support.bigmemtest(size=LARGE_SIZE, memuse=2, dry_run=False) |
|---|
| 306 | n/a | def test_str(self, size): |
|---|
| 307 | n/a | self.check_unmarshallable('x' * size) |
|---|
| 308 | n/a | |
|---|
| 309 | n/a | @support.bigmemtest(size=LARGE_SIZE, memuse=pointer_size + 1, dry_run=False) |
|---|
| 310 | n/a | def test_tuple(self, size): |
|---|
| 311 | n/a | self.check_unmarshallable((None,) * size) |
|---|
| 312 | n/a | |
|---|
| 313 | n/a | @support.bigmemtest(size=LARGE_SIZE, memuse=pointer_size + 1, dry_run=False) |
|---|
| 314 | n/a | def test_list(self, size): |
|---|
| 315 | n/a | self.check_unmarshallable([None] * size) |
|---|
| 316 | n/a | |
|---|
| 317 | n/a | @support.bigmemtest(size=LARGE_SIZE, |
|---|
| 318 | n/a | memuse=pointer_size*12 + sys.getsizeof(LARGE_SIZE-1), |
|---|
| 319 | n/a | dry_run=False) |
|---|
| 320 | n/a | def test_set(self, size): |
|---|
| 321 | n/a | self.check_unmarshallable(set(range(size))) |
|---|
| 322 | n/a | |
|---|
| 323 | n/a | @support.bigmemtest(size=LARGE_SIZE, |
|---|
| 324 | n/a | memuse=pointer_size*12 + sys.getsizeof(LARGE_SIZE-1), |
|---|
| 325 | n/a | dry_run=False) |
|---|
| 326 | n/a | def test_frozenset(self, size): |
|---|
| 327 | n/a | self.check_unmarshallable(frozenset(range(size))) |
|---|
| 328 | n/a | |
|---|
| 329 | n/a | @support.bigmemtest(size=LARGE_SIZE, memuse=2, dry_run=False) |
|---|
| 330 | n/a | def test_bytearray(self, size): |
|---|
| 331 | n/a | self.check_unmarshallable(bytearray(size)) |
|---|
| 332 | n/a | |
|---|
| 333 | n/a | def CollectObjectIDs(ids, obj): |
|---|
| 334 | n/a | """Collect object ids seen in a structure""" |
|---|
| 335 | n/a | if id(obj) in ids: |
|---|
| 336 | n/a | return |
|---|
| 337 | n/a | ids.add(id(obj)) |
|---|
| 338 | n/a | if isinstance(obj, (list, tuple, set, frozenset)): |
|---|
| 339 | n/a | for e in obj: |
|---|
| 340 | n/a | CollectObjectIDs(ids, e) |
|---|
| 341 | n/a | elif isinstance(obj, dict): |
|---|
| 342 | n/a | for k, v in obj.items(): |
|---|
| 343 | n/a | CollectObjectIDs(ids, k) |
|---|
| 344 | n/a | CollectObjectIDs(ids, v) |
|---|
| 345 | n/a | return len(ids) |
|---|
| 346 | n/a | |
|---|
| 347 | n/a | class InstancingTestCase(unittest.TestCase, HelperMixin): |
|---|
| 348 | n/a | intobj = 123321 |
|---|
| 349 | n/a | floatobj = 1.2345 |
|---|
| 350 | n/a | strobj = "abcde"*3 |
|---|
| 351 | n/a | dictobj = {"hello":floatobj, "goodbye":floatobj, floatobj:"hello"} |
|---|
| 352 | n/a | |
|---|
| 353 | n/a | def helper3(self, rsample, recursive=False, simple=False): |
|---|
| 354 | n/a | #we have two instances |
|---|
| 355 | n/a | sample = (rsample, rsample) |
|---|
| 356 | n/a | |
|---|
| 357 | n/a | n0 = CollectObjectIDs(set(), sample) |
|---|
| 358 | n/a | |
|---|
| 359 | n/a | s3 = marshal.dumps(sample, 3) |
|---|
| 360 | n/a | n3 = CollectObjectIDs(set(), marshal.loads(s3)) |
|---|
| 361 | n/a | |
|---|
| 362 | n/a | #same number of instances generated |
|---|
| 363 | n/a | self.assertEqual(n3, n0) |
|---|
| 364 | n/a | |
|---|
| 365 | n/a | if not recursive: |
|---|
| 366 | n/a | #can compare with version 2 |
|---|
| 367 | n/a | s2 = marshal.dumps(sample, 2) |
|---|
| 368 | n/a | n2 = CollectObjectIDs(set(), marshal.loads(s2)) |
|---|
| 369 | n/a | #old format generated more instances |
|---|
| 370 | n/a | self.assertGreater(n2, n0) |
|---|
| 371 | n/a | |
|---|
| 372 | n/a | #if complex objects are in there, old format is larger |
|---|
| 373 | n/a | if not simple: |
|---|
| 374 | n/a | self.assertGreater(len(s2), len(s3)) |
|---|
| 375 | n/a | else: |
|---|
| 376 | n/a | self.assertGreaterEqual(len(s2), len(s3)) |
|---|
| 377 | n/a | |
|---|
| 378 | n/a | def testInt(self): |
|---|
| 379 | n/a | self.helper(self.intobj) |
|---|
| 380 | n/a | self.helper3(self.intobj, simple=True) |
|---|
| 381 | n/a | |
|---|
| 382 | n/a | def testFloat(self): |
|---|
| 383 | n/a | self.helper(self.floatobj) |
|---|
| 384 | n/a | self.helper3(self.floatobj) |
|---|
| 385 | n/a | |
|---|
| 386 | n/a | def testStr(self): |
|---|
| 387 | n/a | self.helper(self.strobj) |
|---|
| 388 | n/a | self.helper3(self.strobj) |
|---|
| 389 | n/a | |
|---|
| 390 | n/a | def testDict(self): |
|---|
| 391 | n/a | self.helper(self.dictobj) |
|---|
| 392 | n/a | self.helper3(self.dictobj) |
|---|
| 393 | n/a | |
|---|
| 394 | n/a | def testModule(self): |
|---|
| 395 | n/a | with open(__file__, "rb") as f: |
|---|
| 396 | n/a | code = f.read() |
|---|
| 397 | n/a | if __file__.endswith(".py"): |
|---|
| 398 | n/a | code = compile(code, __file__, "exec") |
|---|
| 399 | n/a | self.helper(code) |
|---|
| 400 | n/a | self.helper3(code) |
|---|
| 401 | n/a | |
|---|
| 402 | n/a | def testRecursion(self): |
|---|
| 403 | n/a | d = dict(self.dictobj) |
|---|
| 404 | n/a | d["self"] = d |
|---|
| 405 | n/a | self.helper3(d, recursive=True) |
|---|
| 406 | n/a | l = [self.dictobj] |
|---|
| 407 | n/a | l.append(l) |
|---|
| 408 | n/a | self.helper3(l, recursive=True) |
|---|
| 409 | n/a | |
|---|
| 410 | n/a | class CompatibilityTestCase(unittest.TestCase): |
|---|
| 411 | n/a | def _test(self, version): |
|---|
| 412 | n/a | with open(__file__, "rb") as f: |
|---|
| 413 | n/a | code = f.read() |
|---|
| 414 | n/a | if __file__.endswith(".py"): |
|---|
| 415 | n/a | code = compile(code, __file__, "exec") |
|---|
| 416 | n/a | data = marshal.dumps(code, version) |
|---|
| 417 | n/a | marshal.loads(data) |
|---|
| 418 | n/a | |
|---|
| 419 | n/a | def test0To3(self): |
|---|
| 420 | n/a | self._test(0) |
|---|
| 421 | n/a | |
|---|
| 422 | n/a | def test1To3(self): |
|---|
| 423 | n/a | self._test(1) |
|---|
| 424 | n/a | |
|---|
| 425 | n/a | def test2To3(self): |
|---|
| 426 | n/a | self._test(2) |
|---|
| 427 | n/a | |
|---|
| 428 | n/a | def test3To3(self): |
|---|
| 429 | n/a | self._test(3) |
|---|
| 430 | n/a | |
|---|
| 431 | n/a | class InterningTestCase(unittest.TestCase, HelperMixin): |
|---|
| 432 | n/a | strobj = "this is an interned string" |
|---|
| 433 | n/a | strobj = sys.intern(strobj) |
|---|
| 434 | n/a | |
|---|
| 435 | n/a | def testIntern(self): |
|---|
| 436 | n/a | s = marshal.loads(marshal.dumps(self.strobj)) |
|---|
| 437 | n/a | self.assertEqual(s, self.strobj) |
|---|
| 438 | n/a | self.assertEqual(id(s), id(self.strobj)) |
|---|
| 439 | n/a | s2 = sys.intern(s) |
|---|
| 440 | n/a | self.assertEqual(id(s2), id(s)) |
|---|
| 441 | n/a | |
|---|
| 442 | n/a | def testNoIntern(self): |
|---|
| 443 | n/a | s = marshal.loads(marshal.dumps(self.strobj, 2)) |
|---|
| 444 | n/a | self.assertEqual(s, self.strobj) |
|---|
| 445 | n/a | self.assertNotEqual(id(s), id(self.strobj)) |
|---|
| 446 | n/a | s2 = sys.intern(s) |
|---|
| 447 | n/a | self.assertNotEqual(id(s2), id(s)) |
|---|
| 448 | n/a | |
|---|
| 449 | n/a | @support.cpython_only |
|---|
| 450 | n/a | @unittest.skipUnless(_testcapi, 'requires _testcapi') |
|---|
| 451 | n/a | class CAPI_TestCase(unittest.TestCase, HelperMixin): |
|---|
| 452 | n/a | |
|---|
| 453 | n/a | def test_write_long_to_file(self): |
|---|
| 454 | n/a | for v in range(marshal.version + 1): |
|---|
| 455 | n/a | _testcapi.pymarshal_write_long_to_file(0x12345678, support.TESTFN, v) |
|---|
| 456 | n/a | with open(support.TESTFN, 'rb') as f: |
|---|
| 457 | n/a | data = f.read() |
|---|
| 458 | n/a | support.unlink(support.TESTFN) |
|---|
| 459 | n/a | self.assertEqual(data, b'\x78\x56\x34\x12') |
|---|
| 460 | n/a | |
|---|
| 461 | n/a | def test_write_object_to_file(self): |
|---|
| 462 | n/a | obj = ('\u20ac', b'abc', 123, 45.6, 7+8j, 'long line '*1000) |
|---|
| 463 | n/a | for v in range(marshal.version + 1): |
|---|
| 464 | n/a | _testcapi.pymarshal_write_object_to_file(obj, support.TESTFN, v) |
|---|
| 465 | n/a | with open(support.TESTFN, 'rb') as f: |
|---|
| 466 | n/a | data = f.read() |
|---|
| 467 | n/a | support.unlink(support.TESTFN) |
|---|
| 468 | n/a | self.assertEqual(marshal.loads(data), obj) |
|---|
| 469 | n/a | |
|---|
| 470 | n/a | def test_read_short_from_file(self): |
|---|
| 471 | n/a | with open(support.TESTFN, 'wb') as f: |
|---|
| 472 | n/a | f.write(b'\x34\x12xxxx') |
|---|
| 473 | n/a | r, p = _testcapi.pymarshal_read_short_from_file(support.TESTFN) |
|---|
| 474 | n/a | support.unlink(support.TESTFN) |
|---|
| 475 | n/a | self.assertEqual(r, 0x1234) |
|---|
| 476 | n/a | self.assertEqual(p, 2) |
|---|
| 477 | n/a | |
|---|
| 478 | n/a | with open(support.TESTFN, 'wb') as f: |
|---|
| 479 | n/a | f.write(b'\x12') |
|---|
| 480 | n/a | with self.assertRaises(EOFError): |
|---|
| 481 | n/a | _testcapi.pymarshal_read_short_from_file(support.TESTFN) |
|---|
| 482 | n/a | support.unlink(support.TESTFN) |
|---|
| 483 | n/a | |
|---|
| 484 | n/a | def test_read_long_from_file(self): |
|---|
| 485 | n/a | with open(support.TESTFN, 'wb') as f: |
|---|
| 486 | n/a | f.write(b'\x78\x56\x34\x12xxxx') |
|---|
| 487 | n/a | r, p = _testcapi.pymarshal_read_long_from_file(support.TESTFN) |
|---|
| 488 | n/a | support.unlink(support.TESTFN) |
|---|
| 489 | n/a | self.assertEqual(r, 0x12345678) |
|---|
| 490 | n/a | self.assertEqual(p, 4) |
|---|
| 491 | n/a | |
|---|
| 492 | n/a | with open(support.TESTFN, 'wb') as f: |
|---|
| 493 | n/a | f.write(b'\x56\x34\x12') |
|---|
| 494 | n/a | with self.assertRaises(EOFError): |
|---|
| 495 | n/a | _testcapi.pymarshal_read_long_from_file(support.TESTFN) |
|---|
| 496 | n/a | support.unlink(support.TESTFN) |
|---|
| 497 | n/a | |
|---|
| 498 | n/a | def test_read_last_object_from_file(self): |
|---|
| 499 | n/a | obj = ('\u20ac', b'abc', 123, 45.6, 7+8j) |
|---|
| 500 | n/a | for v in range(marshal.version + 1): |
|---|
| 501 | n/a | data = marshal.dumps(obj, v) |
|---|
| 502 | n/a | with open(support.TESTFN, 'wb') as f: |
|---|
| 503 | n/a | f.write(data + b'xxxx') |
|---|
| 504 | n/a | r, p = _testcapi.pymarshal_read_last_object_from_file(support.TESTFN) |
|---|
| 505 | n/a | support.unlink(support.TESTFN) |
|---|
| 506 | n/a | self.assertEqual(r, obj) |
|---|
| 507 | n/a | |
|---|
| 508 | n/a | with open(support.TESTFN, 'wb') as f: |
|---|
| 509 | n/a | f.write(data[:1]) |
|---|
| 510 | n/a | with self.assertRaises(EOFError): |
|---|
| 511 | n/a | _testcapi.pymarshal_read_last_object_from_file(support.TESTFN) |
|---|
| 512 | n/a | support.unlink(support.TESTFN) |
|---|
| 513 | n/a | |
|---|
| 514 | n/a | def test_read_object_from_file(self): |
|---|
| 515 | n/a | obj = ('\u20ac', b'abc', 123, 45.6, 7+8j) |
|---|
| 516 | n/a | for v in range(marshal.version + 1): |
|---|
| 517 | n/a | data = marshal.dumps(obj, v) |
|---|
| 518 | n/a | with open(support.TESTFN, 'wb') as f: |
|---|
| 519 | n/a | f.write(data + b'xxxx') |
|---|
| 520 | n/a | r, p = _testcapi.pymarshal_read_object_from_file(support.TESTFN) |
|---|
| 521 | n/a | support.unlink(support.TESTFN) |
|---|
| 522 | n/a | self.assertEqual(r, obj) |
|---|
| 523 | n/a | self.assertEqual(p, len(data)) |
|---|
| 524 | n/a | |
|---|
| 525 | n/a | with open(support.TESTFN, 'wb') as f: |
|---|
| 526 | n/a | f.write(data[:1]) |
|---|
| 527 | n/a | with self.assertRaises(EOFError): |
|---|
| 528 | n/a | _testcapi.pymarshal_read_object_from_file(support.TESTFN) |
|---|
| 529 | n/a | support.unlink(support.TESTFN) |
|---|
| 530 | n/a | |
|---|
| 531 | n/a | |
|---|
| 532 | n/a | if __name__ == "__main__": |
|---|
| 533 | n/a | unittest.main() |
|---|