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