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