diff options
Diffstat (limited to 'Lib/test/test_marshal.py')
-rw-r--r-- | Lib/test/test_marshal.py | 42 |
1 files changed, 34 insertions, 8 deletions
diff --git a/Lib/test/test_marshal.py b/Lib/test/test_marshal.py index 02ef62a983..0b1413715d 100644 --- a/Lib/test/test_marshal.py +++ b/Lib/test/test_marshal.py @@ -1,11 +1,11 @@ -#!/usr/bin/env python3 - from test import support import array +import io import marshal import sys import unittest import os +import types class HelperMixin: def helper(self, sample, *extra): @@ -114,6 +114,22 @@ class CodeTestCase(unittest.TestCase): codes = (ExceptionTestCase.test_exceptions.__code__,) * count marshal.loads(marshal.dumps(codes)) + def test_different_filenames(self): + co1 = compile("x", "f1", "exec") + co2 = compile("y", "f2", "exec") + co1, co2 = marshal.loads(marshal.dumps((co1, co2))) + self.assertEqual(co1.co_filename, "f1") + self.assertEqual(co2.co_filename, "f2") + + @support.cpython_only + def test_same_filename_used(self): + s = """def f(): pass\ndef g(): pass""" + co = compile(s, "myfile", "exec") + co = marshal.loads(marshal.dumps(co)) + for obj in co.co_consts: + if isinstance(obj, types.CodeType): + self.assertIs(co.co_filename, obj.co_filename) + class ContainerTestCase(unittest.TestCase, HelperMixin): d = {'astring': 'foo@bar.baz.spam', 'afloat': 7283.43, @@ -262,8 +278,18 @@ class BugsTestCase(unittest.TestCase): unicode_string = 'T' self.assertRaises(TypeError, marshal.loads, unicode_string) + def test_bad_reader(self): + class BadReader(io.BytesIO): + def read(self, n=-1): + b = super().read(n) + if n is not None and n > 4: + b += b' ' * 10**6 + return b + for value in (1.0, 1j, b'0123456789', '0123456789'): + self.assertRaises(ValueError, marshal.load, + BadReader(marshal.dumps(value))) + LARGE_SIZE = 2**31 -character_size = 4 if sys.maxunicode > 0xFFFF else 2 pointer_size = 8 if sys.maxsize > 0xFFFFFFFF else 4 class NullWriter: @@ -275,19 +301,19 @@ class LargeValuesTestCase(unittest.TestCase): def check_unmarshallable(self, data): self.assertRaises(ValueError, marshal.dump, data, NullWriter()) - @support.bigmemtest(size=LARGE_SIZE, memuse=1, dry_run=False) + @support.bigmemtest(size=LARGE_SIZE, memuse=2, dry_run=False) def test_bytes(self, size): self.check_unmarshallable(b'x' * size) - @support.bigmemtest(size=LARGE_SIZE, memuse=character_size, dry_run=False) + @support.bigmemtest(size=LARGE_SIZE, memuse=2, dry_run=False) def test_str(self, size): self.check_unmarshallable('x' * size) - @support.bigmemtest(size=LARGE_SIZE, memuse=pointer_size, dry_run=False) + @support.bigmemtest(size=LARGE_SIZE, memuse=pointer_size + 1, dry_run=False) def test_tuple(self, size): self.check_unmarshallable((None,) * size) - @support.bigmemtest(size=LARGE_SIZE, memuse=pointer_size, dry_run=False) + @support.bigmemtest(size=LARGE_SIZE, memuse=pointer_size + 1, dry_run=False) def test_list(self, size): self.check_unmarshallable([None] * size) @@ -303,7 +329,7 @@ class LargeValuesTestCase(unittest.TestCase): def test_frozenset(self, size): self.check_unmarshallable(frozenset(range(size))) - @support.bigmemtest(size=LARGE_SIZE, memuse=1, dry_run=False) + @support.bigmemtest(size=LARGE_SIZE, memuse=2, dry_run=False) def test_bytearray(self, size): self.check_unmarshallable(bytearray(size)) |