Benjamin Peterson | 90f5ba5 | 2010-03-11 22:53:45 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 2 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 3 | from test import support |
Antoine Pitrou | 679e9d3 | 2012-03-02 18:12:43 +0100 | [diff] [blame] | 4 | import array |
Serhiy Storchaka | 3641a74 | 2013-07-11 22:20:47 +0300 | [diff] [blame] | 5 | import io |
Tim Peters | 8211237 | 2001-08-29 02:28:42 +0000 | [diff] [blame] | 6 | import marshal |
| 7 | import sys |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 8 | import unittest |
| 9 | import os |
Benjamin Peterson | 43b0686 | 2011-05-27 09:08:01 -0500 | [diff] [blame] | 10 | import types |
Tim Peters | 8211237 | 2001-08-29 02:28:42 +0000 | [diff] [blame] | 11 | |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 12 | class HelperMixin: |
| 13 | def helper(self, sample, *extra): |
| 14 | new = marshal.loads(marshal.dumps(sample, *extra)) |
| 15 | self.assertEqual(sample, new) |
| 16 | try: |
Brian Curtin | 2c3563f | 2010-10-13 02:40:26 +0000 | [diff] [blame] | 17 | with open(support.TESTFN, "wb") as f: |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 18 | marshal.dump(sample, f, *extra) |
Brian Curtin | 2c3563f | 2010-10-13 02:40:26 +0000 | [diff] [blame] | 19 | with open(support.TESTFN, "rb") as f: |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 20 | new = marshal.load(f) |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 21 | self.assertEqual(sample, new) |
| 22 | finally: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 23 | support.unlink(support.TESTFN) |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 24 | |
| 25 | class IntTestCase(unittest.TestCase, HelperMixin): |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 26 | def test_ints(self): |
| 27 | # Test the full range of Python ints. |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 28 | n = sys.maxsize |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 29 | while n: |
| 30 | for expected in (-n, n): |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 31 | self.helper(expected) |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 32 | n = n >> 1 |
Tim Peters | 8211237 | 2001-08-29 02:28:42 +0000 | [diff] [blame] | 33 | |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 34 | def test_int64(self): |
| 35 | # Simulate int marshaling on a 64-bit box. This is most interesting if |
| 36 | # we're running the test on a 32-bit box, of course. |
| 37 | |
| 38 | def to_little_endian_string(value, nbytes): |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 39 | b = bytearray() |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 40 | for i in range(nbytes): |
Guido van Rossum | e6d3904 | 2007-05-09 00:01:30 +0000 | [diff] [blame] | 41 | b.append(value & 0xff) |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 42 | value >>= 8 |
Guido van Rossum | e6d3904 | 2007-05-09 00:01:30 +0000 | [diff] [blame] | 43 | return b |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 44 | |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 45 | maxint64 = (1 << 63) - 1 |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 46 | minint64 = -maxint64-1 |
| 47 | |
| 48 | for base in maxint64, minint64, -maxint64, -(minint64 >> 1): |
| 49 | while base: |
Guido van Rossum | e6d3904 | 2007-05-09 00:01:30 +0000 | [diff] [blame] | 50 | s = b'I' + to_little_endian_string(base, 8) |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 51 | got = marshal.loads(s) |
| 52 | self.assertEqual(base, got) |
| 53 | if base == -1: # a fixed-point for shifting right 1 |
| 54 | base = 0 |
| 55 | else: |
| 56 | base >>= 1 |
| 57 | |
| 58 | def test_bool(self): |
| 59 | for b in (True, False): |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 60 | self.helper(b) |
Tim Peters | 58eb11c | 2004-01-18 20:29:55 +0000 | [diff] [blame] | 61 | |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 62 | class FloatTestCase(unittest.TestCase, HelperMixin): |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 63 | def test_floats(self): |
| 64 | # Test a few floats |
| 65 | small = 1e-25 |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 66 | n = sys.maxsize * 3.7e250 |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 67 | while n > small: |
| 68 | for expected in (-n, n): |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 69 | self.helper(float(expected)) |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 70 | n /= 123.4567 |
| 71 | |
| 72 | f = 0.0 |
Michael W. Hudson | df88846 | 2005-06-03 14:41:55 +0000 | [diff] [blame] | 73 | s = marshal.dumps(f, 2) |
Tim Peters | 8211237 | 2001-08-29 02:28:42 +0000 | [diff] [blame] | 74 | got = marshal.loads(s) |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 75 | self.assertEqual(f, got) |
Michael W. Hudson | df88846 | 2005-06-03 14:41:55 +0000 | [diff] [blame] | 76 | # and with version <= 1 (floats marshalled differently then) |
| 77 | s = marshal.dumps(f, 1) |
Tim Peters | 5d36a55 | 2005-06-03 22:40:27 +0000 | [diff] [blame] | 78 | got = marshal.loads(s) |
| 79 | self.assertEqual(f, got) |
Tim Peters | 8211237 | 2001-08-29 02:28:42 +0000 | [diff] [blame] | 80 | |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 81 | n = sys.maxsize * 3.7e-250 |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 82 | while n < small: |
| 83 | for expected in (-n, n): |
| 84 | f = float(expected) |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 85 | self.helper(f) |
| 86 | self.helper(f, 1) |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 87 | n *= 123.4567 |
Tim Peters | 8211237 | 2001-08-29 02:28:42 +0000 | [diff] [blame] | 88 | |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 89 | class StringTestCase(unittest.TestCase, HelperMixin): |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 90 | def test_unicode(self): |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 91 | for s in ["", "Andr\xe8 Previn", "abc", " "*10000]: |
| 92 | self.helper(marshal.loads(marshal.dumps(s))) |
Tim Peters | 8211237 | 2001-08-29 02:28:42 +0000 | [diff] [blame] | 93 | |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 94 | def test_string(self): |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 95 | for s in ["", "Andr\xe8 Previn", "abc", " "*10000]: |
| 96 | self.helper(s) |
Tim Peters | 8211237 | 2001-08-29 02:28:42 +0000 | [diff] [blame] | 97 | |
Guido van Rossum | bae07c9 | 2007-10-08 02:46:15 +0000 | [diff] [blame] | 98 | def test_bytes(self): |
Guido van Rossum | e6d3904 | 2007-05-09 00:01:30 +0000 | [diff] [blame] | 99 | for s in [b"", b"Andr\xe8 Previn", b"abc", b" "*10000]: |
Guido van Rossum | bae07c9 | 2007-10-08 02:46:15 +0000 | [diff] [blame] | 100 | self.helper(s) |
Tim Peters | 58eb11c | 2004-01-18 20:29:55 +0000 | [diff] [blame] | 101 | |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 102 | class ExceptionTestCase(unittest.TestCase): |
| 103 | def test_exceptions(self): |
| 104 | new = marshal.loads(marshal.dumps(StopIteration)) |
| 105 | self.assertEqual(StopIteration, new) |
Thomas Heller | 3e1c18a | 2002-07-30 11:40:57 +0000 | [diff] [blame] | 106 | |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 107 | class CodeTestCase(unittest.TestCase): |
| 108 | def test_code(self): |
Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 109 | co = ExceptionTestCase.test_exceptions.__code__ |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 110 | new = marshal.loads(marshal.dumps(co)) |
| 111 | self.assertEqual(co, new) |
| 112 | |
Amaury Forgeot d'Arc | 74c71f5 | 2008-05-26 21:41:42 +0000 | [diff] [blame] | 113 | def test_many_codeobjects(self): |
| 114 | # Issue2957: bad recursion count on code objects |
| 115 | count = 5000 # more than MAX_MARSHAL_STACK_DEPTH |
| 116 | codes = (ExceptionTestCase.test_exceptions.__code__,) * count |
| 117 | marshal.loads(marshal.dumps(codes)) |
| 118 | |
Benjamin Peterson | 43b0686 | 2011-05-27 09:08:01 -0500 | [diff] [blame] | 119 | def test_different_filenames(self): |
| 120 | co1 = compile("x", "f1", "exec") |
| 121 | co2 = compile("y", "f2", "exec") |
| 122 | co1, co2 = marshal.loads(marshal.dumps((co1, co2))) |
| 123 | self.assertEqual(co1.co_filename, "f1") |
| 124 | self.assertEqual(co2.co_filename, "f2") |
| 125 | |
| 126 | @support.cpython_only |
| 127 | def test_same_filename_used(self): |
| 128 | s = """def f(): pass\ndef g(): pass""" |
| 129 | co = compile(s, "myfile", "exec") |
| 130 | co = marshal.loads(marshal.dumps(co)) |
| 131 | for obj in co.co_consts: |
| 132 | if isinstance(obj, types.CodeType): |
| 133 | self.assertIs(co.co_filename, obj.co_filename) |
| 134 | |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 135 | class ContainerTestCase(unittest.TestCase, HelperMixin): |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 136 | d = {'astring': 'foo@bar.baz.spam', |
| 137 | 'afloat': 7283.43, |
| 138 | 'anint': 2**20, |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 139 | 'ashortlong': 2, |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 140 | 'alist': ['.zyx.41'], |
| 141 | 'atuple': ('.zyx.41',)*10, |
| 142 | 'aboolean': False, |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 143 | 'aunicode': "Andr\xe8 Previn" |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 144 | } |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 145 | |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 146 | def test_dict(self): |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 147 | self.helper(self.d) |
Tim Peters | 58eb11c | 2004-01-18 20:29:55 +0000 | [diff] [blame] | 148 | |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 149 | def test_list(self): |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 150 | self.helper(list(self.d.items())) |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 151 | |
| 152 | def test_tuple(self): |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 153 | self.helper(tuple(self.d.keys())) |
Tim Peters | 58eb11c | 2004-01-18 20:29:55 +0000 | [diff] [blame] | 154 | |
Raymond Hettinger | a422c34 | 2005-01-11 03:03:27 +0000 | [diff] [blame] | 155 | def test_sets(self): |
| 156 | for constructor in (set, frozenset): |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 157 | self.helper(constructor(self.d.keys())) |
Raymond Hettinger | a422c34 | 2005-01-11 03:03:27 +0000 | [diff] [blame] | 158 | |
Antoine Pitrou | 679e9d3 | 2012-03-02 18:12:43 +0100 | [diff] [blame] | 159 | |
| 160 | class BufferTestCase(unittest.TestCase, HelperMixin): |
| 161 | |
| 162 | def test_bytearray(self): |
| 163 | b = bytearray(b"abc") |
| 164 | self.helper(b) |
| 165 | new = marshal.loads(marshal.dumps(b)) |
| 166 | self.assertEqual(type(new), bytes) |
| 167 | |
| 168 | def test_memoryview(self): |
| 169 | b = memoryview(b"abc") |
| 170 | self.helper(b) |
| 171 | new = marshal.loads(marshal.dumps(b)) |
| 172 | self.assertEqual(type(new), bytes) |
| 173 | |
| 174 | def test_array(self): |
| 175 | a = array.array('B', b"abc") |
| 176 | new = marshal.loads(marshal.dumps(a)) |
| 177 | self.assertEqual(new, b"abc") |
| 178 | |
| 179 | |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 180 | class BugsTestCase(unittest.TestCase): |
| 181 | def test_bug_5888452(self): |
| 182 | # Simple-minded check for SF 588452: Debug build crashes |
| 183 | marshal.dumps([128] * 1000) |
| 184 | |
Armin Rigo | 01ab279 | 2004-03-26 15:09:27 +0000 | [diff] [blame] | 185 | def test_patch_873224(self): |
| 186 | self.assertRaises(Exception, marshal.loads, '0') |
| 187 | self.assertRaises(Exception, marshal.loads, 'f') |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 188 | self.assertRaises(Exception, marshal.loads, marshal.dumps(2**65)[:-1]) |
Armin Rigo | 01ab279 | 2004-03-26 15:09:27 +0000 | [diff] [blame] | 189 | |
Armin Rigo | 2ccea17 | 2004-12-20 12:25:57 +0000 | [diff] [blame] | 190 | def test_version_argument(self): |
| 191 | # Python 2.4.0 crashes for any call to marshal.dumps(x, y) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 192 | self.assertEqual(marshal.loads(marshal.dumps(5, 0)), 5) |
| 193 | self.assertEqual(marshal.loads(marshal.dumps(5, 1)), 5) |
Armin Rigo | 2ccea17 | 2004-12-20 12:25:57 +0000 | [diff] [blame] | 194 | |
Michael W. Hudson | f2ca5af | 2005-06-13 18:28:46 +0000 | [diff] [blame] | 195 | def test_fuzz(self): |
| 196 | # simple test that it's at least not *totally* trivial to |
| 197 | # crash from bad marshal data |
| 198 | for c in [chr(i) for i in range(256)]: |
| 199 | try: |
| 200 | marshal.loads(c) |
| 201 | except Exception: |
| 202 | pass |
| 203 | |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 204 | def test_loads_recursion(self): |
Antoine Pitrou | 4a90ef0 | 2012-03-03 02:35:32 +0100 | [diff] [blame] | 205 | s = b'c' + (b'X' * 4*4) + b'{' * 2**20 |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 206 | self.assertRaises(ValueError, marshal.loads, s) |
| 207 | |
| 208 | def test_recursion_limit(self): |
| 209 | # Create a deeply nested structure. |
| 210 | head = last = [] |
| 211 | # The max stack depth should match the value in Python/marshal.c. |
Guido van Rossum | 991bf5d | 2007-08-29 18:44:54 +0000 | [diff] [blame] | 212 | if os.name == 'nt' and hasattr(sys, 'gettotalrefcount'): |
| 213 | MAX_MARSHAL_STACK_DEPTH = 1500 |
| 214 | else: |
| 215 | MAX_MARSHAL_STACK_DEPTH = 2000 |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 216 | for i in range(MAX_MARSHAL_STACK_DEPTH - 2): |
| 217 | last.append([0]) |
| 218 | last = last[-1] |
| 219 | |
| 220 | # Verify we don't blow out the stack with dumps/load. |
| 221 | data = marshal.dumps(head) |
| 222 | new_head = marshal.loads(data) |
| 223 | # Don't use == to compare objects, it can exceed the recursion limit. |
| 224 | self.assertEqual(len(new_head), len(head)) |
| 225 | self.assertEqual(len(new_head[0]), len(head[0])) |
| 226 | self.assertEqual(len(new_head[-1]), len(head[-1])) |
| 227 | |
| 228 | last.append([0]) |
| 229 | self.assertRaises(ValueError, marshal.dumps, head) |
| 230 | |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 231 | def test_exact_type_match(self): |
| 232 | # Former bug: |
| 233 | # >>> class Int(int): pass |
| 234 | # >>> type(loads(dumps(Int()))) |
| 235 | # <type 'int'> |
| 236 | for typ in (int, float, complex, tuple, list, dict, set, frozenset): |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 237 | # Note: str subclasses are not tested because they get handled |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 238 | # by marshal's routines for objects supporting the buffer API. |
| 239 | subtyp = type('subtyp', (typ,), {}) |
| 240 | self.assertRaises(ValueError, marshal.dumps, subtyp()) |
| 241 | |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 242 | # Issue #1792 introduced a change in how marshal increases the size of its |
| 243 | # internal buffer; this test ensures that the new code is exercised. |
| 244 | def test_large_marshal(self): |
| 245 | size = int(1e6) |
| 246 | testString = 'abc' * size |
| 247 | marshal.dumps(testString) |
| 248 | |
Mark Dickinson | 2683ab0 | 2009-09-29 19:21:35 +0000 | [diff] [blame] | 249 | def test_invalid_longs(self): |
| 250 | # Issue #7019: marshal.loads shouldn't produce unnormalized PyLongs |
| 251 | invalid_string = b'l\x02\x00\x00\x00\x00\x00\x00\x00' |
| 252 | self.assertRaises(ValueError, marshal.loads, invalid_string) |
| 253 | |
Vinay Sajip | 5bdae3b | 2011-07-02 16:42:47 +0100 | [diff] [blame] | 254 | def test_multiple_dumps_and_loads(self): |
| 255 | # Issue 12291: marshal.load() should be callable multiple times |
| 256 | # with interleaved data written by non-marshal code |
| 257 | # Adapted from a patch by Engelbert Gruber. |
| 258 | data = (1, 'abc', b'def', 1.0, (2, 'a', ['b', b'c'])) |
| 259 | for interleaved in (b'', b'0123'): |
| 260 | ilen = len(interleaved) |
| 261 | positions = [] |
| 262 | try: |
| 263 | with open(support.TESTFN, 'wb') as f: |
| 264 | for d in data: |
| 265 | marshal.dump(d, f) |
| 266 | if ilen: |
| 267 | f.write(interleaved) |
| 268 | positions.append(f.tell()) |
| 269 | with open(support.TESTFN, 'rb') as f: |
| 270 | for i, d in enumerate(data): |
| 271 | self.assertEqual(d, marshal.load(f)) |
| 272 | if ilen: |
| 273 | f.read(ilen) |
| 274 | self.assertEqual(positions[i], f.tell()) |
| 275 | finally: |
| 276 | support.unlink(support.TESTFN) |
| 277 | |
Antoine Pitrou | 4a90ef0 | 2012-03-03 02:35:32 +0100 | [diff] [blame] | 278 | def test_loads_reject_unicode_strings(self): |
| 279 | # Issue #14177: marshal.loads() should not accept unicode strings |
| 280 | unicode_string = 'T' |
| 281 | self.assertRaises(TypeError, marshal.loads, unicode_string) |
| 282 | |
Serhiy Storchaka | 3641a74 | 2013-07-11 22:20:47 +0300 | [diff] [blame] | 283 | def test_bad_reader(self): |
| 284 | class BadReader(io.BytesIO): |
| 285 | def read(self, n=-1): |
| 286 | b = super().read(n) |
| 287 | if n is not None and n > 4: |
| 288 | b += b' ' * 10**6 |
| 289 | return b |
| 290 | for value in (1.0, 1j, b'0123456789', '0123456789'): |
| 291 | self.assertRaises(ValueError, marshal.load, |
| 292 | BadReader(marshal.dumps(value))) |
| 293 | |
Serhiy Storchaka | 7e01911 | 2013-02-13 12:08:15 +0200 | [diff] [blame] | 294 | LARGE_SIZE = 2**31 |
Serhiy Storchaka | 7e01911 | 2013-02-13 12:08:15 +0200 | [diff] [blame] | 295 | pointer_size = 8 if sys.maxsize > 0xFFFFFFFF else 4 |
| 296 | |
| 297 | class NullWriter: |
| 298 | def write(self, s): |
| 299 | pass |
| 300 | |
| 301 | @unittest.skipIf(LARGE_SIZE > sys.maxsize, "test cannot run on 32-bit systems") |
| 302 | class LargeValuesTestCase(unittest.TestCase): |
| 303 | def check_unmarshallable(self, data): |
| 304 | self.assertRaises(ValueError, marshal.dump, data, NullWriter()) |
| 305 | |
| 306 | @support.bigmemtest(size=LARGE_SIZE, memuse=1, dry_run=False) |
| 307 | def test_bytes(self, size): |
| 308 | self.check_unmarshallable(b'x' * size) |
| 309 | |
Serhiy Storchaka | 40f42d9 | 2013-02-13 12:32:24 +0200 | [diff] [blame] | 310 | @support.bigmemtest(size=LARGE_SIZE, memuse=1, dry_run=False) |
Serhiy Storchaka | 7e01911 | 2013-02-13 12:08:15 +0200 | [diff] [blame] | 311 | def test_str(self, size): |
| 312 | self.check_unmarshallable('x' * size) |
| 313 | |
| 314 | @support.bigmemtest(size=LARGE_SIZE, memuse=pointer_size, dry_run=False) |
| 315 | def test_tuple(self, size): |
| 316 | self.check_unmarshallable((None,) * size) |
| 317 | |
| 318 | @support.bigmemtest(size=LARGE_SIZE, memuse=pointer_size, dry_run=False) |
| 319 | def test_list(self, size): |
| 320 | self.check_unmarshallable([None] * size) |
| 321 | |
| 322 | @support.bigmemtest(size=LARGE_SIZE, |
| 323 | memuse=pointer_size*12 + sys.getsizeof(LARGE_SIZE-1), |
| 324 | dry_run=False) |
| 325 | def test_set(self, size): |
| 326 | self.check_unmarshallable(set(range(size))) |
| 327 | |
| 328 | @support.bigmemtest(size=LARGE_SIZE, |
| 329 | memuse=pointer_size*12 + sys.getsizeof(LARGE_SIZE-1), |
| 330 | dry_run=False) |
| 331 | def test_frozenset(self, size): |
| 332 | self.check_unmarshallable(frozenset(range(size))) |
| 333 | |
| 334 | @support.bigmemtest(size=LARGE_SIZE, memuse=1, dry_run=False) |
| 335 | def test_bytearray(self, size): |
| 336 | self.check_unmarshallable(bytearray(size)) |
| 337 | |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 338 | |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 339 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 340 | support.run_unittest(IntTestCase, |
Serhiy Storchaka | 7e01911 | 2013-02-13 12:08:15 +0200 | [diff] [blame] | 341 | FloatTestCase, |
| 342 | StringTestCase, |
| 343 | CodeTestCase, |
| 344 | ContainerTestCase, |
| 345 | ExceptionTestCase, |
| 346 | BufferTestCase, |
| 347 | BugsTestCase, |
| 348 | LargeValuesTestCase, |
| 349 | ) |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 350 | |
| 351 | if __name__ == "__main__": |
| 352 | test_main() |