# -*- coding: iso-8859-1 -*-
from test
import test_support
class IntTestCase(unittest
.TestCase
):
# Test the full range of Python ints.
s
= marshal
.dumps(expected
)
self
.assertEqual(expected
, got
)
marshal
.dump(expected
, file(test_support
.TESTFN
, "wb"))
got
= marshal
.load(file(test_support
.TESTFN
, "rb"))
self
.assertEqual(expected
, got
)
os
.unlink(test_support
.TESTFN
)
# Simulate int marshaling on a 64-bit box. This is most interesting if
# we're running the test on a 32-bit box, of course.
def to_little_endian_string(value
, nbytes
):
bytes
.append(chr(value
& 0xff))
maxint64
= (1L << 63) - 1
for base
in maxint64
, minint64
, -maxint64
, -(minint64
>> 1):
s
= 'I' + to_little_endian_string(base
, 8)
self
.assertEqual(base
, got
)
if base
== -1: # a fixed-point for shifting right 1
new
= marshal
.loads(marshal
.dumps(b
))
self
.assertEqual(type(b
), type(new
))
marshal
.dump(b
, file(test_support
.TESTFN
, "wb"))
new
= marshal
.load(file(test_support
.TESTFN
, "rb"))
self
.assertEqual(type(b
), type(new
))
class FloatTestCase(unittest
.TestCase
):
marshal
.dump(f
, file(test_support
.TESTFN
, "wb"))
got
= marshal
.load(file(test_support
.TESTFN
, "rb"))
n
= sys
.maxint
* 3.7e-250
marshal
.dump(f
, file(test_support
.TESTFN
, "wb"))
got
= marshal
.load(file(test_support
.TESTFN
, "rb"))
os
.unlink(test_support
.TESTFN
)
class StringTestCase(unittest
.TestCase
):
for s
in [u
"", u
"Andrè Previn", u
"abc", u
" "*10000]:
new
= marshal
.loads(marshal
.dumps(s
))
self
.assertEqual(type(s
), type(new
))
marshal
.dump(s
, file(test_support
.TESTFN
, "wb"))
marshal
.load(file(test_support
.TESTFN
, "rb"))
self
.assertEqual(type(s
), type(new
))
os
.unlink(test_support
.TESTFN
)
for s
in ["", "Andrè Previn", "abc", " "*10000]:
new
= marshal
.loads(marshal
.dumps(s
))
self
.assertEqual(type(s
), type(new
))
marshal
.dump(s
, file(test_support
.TESTFN
, "wb"))
marshal
.load(file(test_support
.TESTFN
, "rb"))
self
.assertEqual(type(s
), type(new
))
os
.unlink(test_support
.TESTFN
)
for s
in ["", "Andrè Previn", "abc", " "*10000]:
new
= marshal
.loads(marshal
.dumps(b
))
marshal
.dump(b
, file(test_support
.TESTFN
, "wb"))
marshal
.load(file(test_support
.TESTFN
, "rb"))
os
.unlink(test_support
.TESTFN
)
class ExceptionTestCase(unittest
.TestCase
):
def test_exceptions(self
):
new
= marshal
.loads(marshal
.dumps(StopIteration))
self
.assertEqual(StopIteration, new
)
class CodeTestCase(unittest
.TestCase
):
co
= ExceptionTestCase
.test_exceptions
.func_code
new
= marshal
.loads(marshal
.dumps(co
))
self
.assertEqual(co
, new
)
class ContainerTestCase(unittest
.TestCase
):
d
= {'astring': 'foo@bar.baz.spam',
'atuple': ('.zyx.41',)*10,
'aunicode': u
"Andrè Previn"
new
= marshal
.loads(marshal
.dumps(self
.d
))
self
.assertEqual(self
.d
, new
)
marshal
.dump(self
.d
, file(test_support
.TESTFN
, "wb"))
marshal
.load(file(test_support
.TESTFN
, "rb"))
self
.assertEqual(self
.d
, new
)
os
.unlink(test_support
.TESTFN
)
new
= marshal
.loads(marshal
.dumps(lst
))
self
.assertEqual(lst
, new
)
marshal
.dump(lst
, file(test_support
.TESTFN
, "wb"))
marshal
.load(file(test_support
.TESTFN
, "rb"))
self
.assertEqual(lst
, new
)
os
.unlink(test_support
.TESTFN
)
new
= marshal
.loads(marshal
.dumps(t
))
marshal
.dump(t
, file(test_support
.TESTFN
, "wb"))
marshal
.load(file(test_support
.TESTFN
, "rb"))
os
.unlink(test_support
.TESTFN
)
class BugsTestCase(unittest
.TestCase
):
def test_bug_5888452(self
):
# Simple-minded check for SF 588452: Debug build crashes
marshal
.dumps([128] * 1000)
def test_patch_873224(self
):
self
.assertRaises(Exception, marshal
.loads
, '0')
self
.assertRaises(Exception, marshal
.loads
, 'f')
self
.assertRaises(Exception, marshal
.loads
, marshal
.dumps(5L)[:-1])
def test_version_argument(self
):
# Python 2.4.0 crashes for any call to marshal.dumps(x, y)
self
.assertEquals(marshal
.loads(marshal
.dumps(5, 0)), 5)
self
.assertEquals(marshal
.loads(marshal
.dumps(5, 1)), 5)
test_support
.run_unittest(IntTestCase
,
if __name__
== "__main__":