+++ /dev/null
-#!/usr/bin/env python\r
-# -*- coding: iso-8859-1 -*-\r
-\r
-from test import test_support\r
-import marshal\r
-import sys\r
-import unittest\r
-import os\r
-\r
-class IntTestCase(unittest.TestCase):\r
- def test_ints(self):\r
- # Test the full range of Python ints.\r
- n = sys.maxint\r
- while n:\r
- for expected in (-n, n):\r
- s = marshal.dumps(expected)\r
- got = marshal.loads(s)\r
- self.assertEqual(expected, got)\r
- marshal.dump(expected, file(test_support.TESTFN, "wb"))\r
- got = marshal.load(file(test_support.TESTFN, "rb"))\r
- self.assertEqual(expected, got)\r
- n = n >> 1\r
- os.unlink(test_support.TESTFN)\r
-\r
- def test_int64(self):\r
- # Simulate int marshaling on a 64-bit box. This is most interesting if\r
- # we're running the test on a 32-bit box, of course.\r
-\r
- def to_little_endian_string(value, nbytes):\r
- bytes = []\r
- for i in range(nbytes):\r
- bytes.append(chr(value & 0xff))\r
- value >>= 8\r
- return ''.join(bytes)\r
-\r
- maxint64 = (1L << 63) - 1\r
- minint64 = -maxint64-1\r
-\r
- for base in maxint64, minint64, -maxint64, -(minint64 >> 1):\r
- while base:\r
- s = 'I' + to_little_endian_string(base, 8)\r
- got = marshal.loads(s)\r
- self.assertEqual(base, got)\r
- if base == -1: # a fixed-point for shifting right 1\r
- base = 0\r
- else:\r
- base >>= 1\r
-\r
- def test_bool(self):\r
- for b in (True, False):\r
- new = marshal.loads(marshal.dumps(b))\r
- self.assertEqual(b, new)\r
- self.assertEqual(type(b), type(new))\r
- marshal.dump(b, file(test_support.TESTFN, "wb"))\r
- new = marshal.load(file(test_support.TESTFN, "rb"))\r
- self.assertEqual(b, new)\r
- self.assertEqual(type(b), type(new))\r
-\r
-class FloatTestCase(unittest.TestCase):\r
- def test_floats(self):\r
- # Test a few floats\r
- small = 1e-25\r
- n = sys.maxint * 3.7e250\r
- while n > small:\r
- for expected in (-n, n):\r
- f = float(expected)\r
- s = marshal.dumps(f)\r
- got = marshal.loads(s)\r
- self.assertEqual(f, got)\r
- marshal.dump(f, file(test_support.TESTFN, "wb"))\r
- got = marshal.load(file(test_support.TESTFN, "rb"))\r
- self.assertEqual(f, got)\r
- n /= 123.4567\r
-\r
- f = 0.0\r
- s = marshal.dumps(f, 2)\r
- got = marshal.loads(s)\r
- self.assertEqual(f, got)\r
- # and with version <= 1 (floats marshalled differently then)\r
- s = marshal.dumps(f, 1)\r
- got = marshal.loads(s)\r
- self.assertEqual(f, got)\r
-\r
- n = sys.maxint * 3.7e-250\r
- while n < small:\r
- for expected in (-n, n):\r
- f = float(expected)\r
-\r
- s = marshal.dumps(f)\r
- got = marshal.loads(s)\r
- self.assertEqual(f, got)\r
-\r
- s = marshal.dumps(f, 1)\r
- got = marshal.loads(s)\r
- self.assertEqual(f, got)\r
-\r
- marshal.dump(f, file(test_support.TESTFN, "wb"))\r
- got = marshal.load(file(test_support.TESTFN, "rb"))\r
- self.assertEqual(f, got)\r
-\r
- marshal.dump(f, file(test_support.TESTFN, "wb"), 1)\r
- got = marshal.load(file(test_support.TESTFN, "rb"))\r
- self.assertEqual(f, got)\r
- n *= 123.4567\r
- os.unlink(test_support.TESTFN)\r
-\r
-class StringTestCase(unittest.TestCase):\r
- def test_unicode(self):\r
- for s in [u"", u"Andrè Previn", u"abc", u" "*10000]:\r
- new = marshal.loads(marshal.dumps(s))\r
- self.assertEqual(s, new)\r
- self.assertEqual(type(s), type(new))\r
- marshal.dump(s, file(test_support.TESTFN, "wb"))\r
- new = marshal.load(file(test_support.TESTFN, "rb"))\r
- self.assertEqual(s, new)\r
- self.assertEqual(type(s), type(new))\r
- os.unlink(test_support.TESTFN)\r
-\r
- def test_string(self):\r
- for s in ["", "Andrè Previn", "abc", " "*10000]:\r
- new = marshal.loads(marshal.dumps(s))\r
- self.assertEqual(s, new)\r
- self.assertEqual(type(s), type(new))\r
- marshal.dump(s, file(test_support.TESTFN, "wb"))\r
- new = marshal.load(file(test_support.TESTFN, "rb"))\r
- self.assertEqual(s, new)\r
- self.assertEqual(type(s), type(new))\r
- os.unlink(test_support.TESTFN)\r
-\r
- def test_buffer(self):\r
- for s in ["", "Andrè Previn", "abc", " "*10000]:\r
- with test_support.check_py3k_warnings(("buffer.. not supported",\r
- DeprecationWarning)):\r
- b = buffer(s)\r
- new = marshal.loads(marshal.dumps(b))\r
- self.assertEqual(s, new)\r
- marshal.dump(b, file(test_support.TESTFN, "wb"))\r
- new = marshal.load(file(test_support.TESTFN, "rb"))\r
- self.assertEqual(s, new)\r
- os.unlink(test_support.TESTFN)\r
-\r
-class ExceptionTestCase(unittest.TestCase):\r
- def test_exceptions(self):\r
- new = marshal.loads(marshal.dumps(StopIteration))\r
- self.assertEqual(StopIteration, new)\r
-\r
-class CodeTestCase(unittest.TestCase):\r
- def test_code(self):\r
- co = ExceptionTestCase.test_exceptions.func_code\r
- new = marshal.loads(marshal.dumps(co))\r
- self.assertEqual(co, new)\r
-\r
-class ContainerTestCase(unittest.TestCase):\r
- d = {'astring': 'foo@bar.baz.spam',\r
- 'afloat': 7283.43,\r
- 'anint': 2**20,\r
- 'ashortlong': 2L,\r
- 'alist': ['.zyx.41'],\r
- 'atuple': ('.zyx.41',)*10,\r
- 'aboolean': False,\r
- 'aunicode': u"Andrè Previn"\r
- }\r
- def test_dict(self):\r
- new = marshal.loads(marshal.dumps(self.d))\r
- self.assertEqual(self.d, new)\r
- marshal.dump(self.d, file(test_support.TESTFN, "wb"))\r
- new = marshal.load(file(test_support.TESTFN, "rb"))\r
- self.assertEqual(self.d, new)\r
- os.unlink(test_support.TESTFN)\r
-\r
- def test_list(self):\r
- lst = self.d.items()\r
- new = marshal.loads(marshal.dumps(lst))\r
- self.assertEqual(lst, new)\r
- marshal.dump(lst, file(test_support.TESTFN, "wb"))\r
- new = marshal.load(file(test_support.TESTFN, "rb"))\r
- self.assertEqual(lst, new)\r
- os.unlink(test_support.TESTFN)\r
-\r
- def test_tuple(self):\r
- t = tuple(self.d.keys())\r
- new = marshal.loads(marshal.dumps(t))\r
- self.assertEqual(t, new)\r
- marshal.dump(t, file(test_support.TESTFN, "wb"))\r
- new = marshal.load(file(test_support.TESTFN, "rb"))\r
- self.assertEqual(t, new)\r
- os.unlink(test_support.TESTFN)\r
-\r
- def test_sets(self):\r
- for constructor in (set, frozenset):\r
- t = constructor(self.d.keys())\r
- new = marshal.loads(marshal.dumps(t))\r
- self.assertEqual(t, new)\r
- self.assertTrue(isinstance(new, constructor))\r
- self.assertNotEqual(id(t), id(new))\r
- marshal.dump(t, file(test_support.TESTFN, "wb"))\r
- new = marshal.load(file(test_support.TESTFN, "rb"))\r
- self.assertEqual(t, new)\r
- os.unlink(test_support.TESTFN)\r
-\r
-class BugsTestCase(unittest.TestCase):\r
- def test_bug_5888452(self):\r
- # Simple-minded check for SF 588452: Debug build crashes\r
- marshal.dumps([128] * 1000)\r
-\r
- def test_patch_873224(self):\r
- self.assertRaises(Exception, marshal.loads, '0')\r
- self.assertRaises(Exception, marshal.loads, 'f')\r
- self.assertRaises(Exception, marshal.loads, marshal.dumps(5L)[:-1])\r
-\r
- def test_version_argument(self):\r
- # Python 2.4.0 crashes for any call to marshal.dumps(x, y)\r
- self.assertEqual(marshal.loads(marshal.dumps(5, 0)), 5)\r
- self.assertEqual(marshal.loads(marshal.dumps(5, 1)), 5)\r
-\r
- def test_fuzz(self):\r
- # simple test that it's at least not *totally* trivial to\r
- # crash from bad marshal data\r
- for c in [chr(i) for i in range(256)]:\r
- try:\r
- marshal.loads(c)\r
- except Exception:\r
- pass\r
-\r
- def test_loads_recursion(self):\r
- s = 'c' + ('X' * 4*4) + '{' * 2**20\r
- self.assertRaises(ValueError, marshal.loads, s)\r
-\r
- def test_recursion_limit(self):\r
- # Create a deeply nested structure.\r
- head = last = []\r
- # The max stack depth should match the value in Python/marshal.c.\r
- MAX_MARSHAL_STACK_DEPTH = 2000\r
- for i in range(MAX_MARSHAL_STACK_DEPTH - 2):\r
- last.append([0])\r
- last = last[-1]\r
-\r
- # Verify we don't blow out the stack with dumps/load.\r
- data = marshal.dumps(head)\r
- new_head = marshal.loads(data)\r
- # Don't use == to compare objects, it can exceed the recursion limit.\r
- self.assertEqual(len(new_head), len(head))\r
- self.assertEqual(len(new_head[0]), len(head[0]))\r
- self.assertEqual(len(new_head[-1]), len(head[-1]))\r
-\r
- last.append([0])\r
- self.assertRaises(ValueError, marshal.dumps, head)\r
-\r
- def test_exact_type_match(self):\r
- # Former bug:\r
- # >>> class Int(int): pass\r
- # >>> type(loads(dumps(Int())))\r
- # <type 'int'>\r
- for typ in (int, long, float, complex, tuple, list, dict, set, frozenset):\r
- # Note: str and unicode subclasses are not tested because they get handled\r
- # by marshal's routines for objects supporting the buffer API.\r
- subtyp = type('subtyp', (typ,), {})\r
- self.assertRaises(ValueError, marshal.dumps, subtyp())\r
-\r
- # Issue #1792 introduced a change in how marshal increases the size of its\r
- # internal buffer; this test ensures that the new code is exercised.\r
- def test_large_marshal(self):\r
- size = int(1e6)\r
- testString = 'abc' * size\r
- marshal.dumps(testString)\r
-\r
- def test_invalid_longs(self):\r
- # Issue #7019: marshal.loads shouldn't produce unnormalized PyLongs\r
- invalid_string = 'l\x02\x00\x00\x00\x00\x00\x00\x00'\r
- self.assertRaises(ValueError, marshal.loads, invalid_string)\r
-\r
-\r
-def test_main():\r
- test_support.run_unittest(IntTestCase,\r
- FloatTestCase,\r
- StringTestCase,\r
- CodeTestCase,\r
- ContainerTestCase,\r
- ExceptionTestCase,\r
- BugsTestCase)\r
-\r
-if __name__ == "__main__":\r
- test_main()\r