Debian Codegraph @

pypy/module/test_lib_pypy/test_marshal_extra.py

raw symbols SCIP
1 import pytest
2 import sys
3 import marshal as cpy_marshal
4 from lib_pypy import _marshal as marshal
5
6 hello = "he"
7 hello += "llo"
8 def func(x):
9 return lambda y: x+y
10 scopefunc = func(42)
11
12 SUBCLASSABLE = [
13 42,
14 sys.maxsize,
15 -1.25,
16 2+5j,
17 42,
18 -1234567890123456789012345678901234567890,
19 hello, # not interned
20 "hello",
21 (),
22 (1, 2),
23 [],
24 [3, 4],
25 {},
26 {5: 6, 7: 8},
27 u'hello',
28 set(),
29 set([1, 2]),
30 frozenset(),
31 frozenset([3, 4]),
32 ]
33
34 TESTCASES = SUBCLASSABLE + [
35 None,
36 False,
37 True,
38 StopIteration,
39 Ellipsis,
40 func.__code__,
41 scopefunc.__code__,
42 ]
43
44
45 @pytest.mark.parametrize('case', TESTCASES)
46 def test_dumps_and_reload(case):
47 s = marshal.dumps(case)
48 obj = marshal.loads(s)
49 assert obj == case
50
51 @pytest.mark.parametrize('case', TESTCASES)
52 def test_loads_from_cpython(case):
53 s = cpy_marshal.dumps(case, 1) # XXX: fails with version 2
54 obj = marshal.loads(s)
55 assert obj == case
56
57 @pytest.mark.parametrize('case', TESTCASES)
58 def test_dumps_to_cpython(case):
59 s = marshal.dumps(case)
60 obj = cpy_marshal.loads(s)
61 assert obj == case
62
63 @pytest.mark.parametrize('case', SUBCLASSABLE)
64 def test_dumps_subclass(case):
65 class Subclass(type(case)):
66 pass
67 case = Subclass(case)
68 s = marshal.dumps(case)
69 obj = marshal.loads(s)
70 assert obj == case
71
72 @pytest.mark.parametrize('case', TESTCASES)
73 def test_load_from_cpython(tmpdir, case):
74 p = str(tmpdir.join('test.dat'))
75
76 with open(p, "w") as f1:
77 s = cpy_marshal.dump(case, f1, 1) # XXX: fails with version 2
78 with open(p, "r") as f2:
79 obj = marshal.load(f2)
80 assert obj == case
81
82 @pytest.mark.parametrize('case', TESTCASES)
83 def test_dump_to_cpython(tmpdir, case):
84 p = str(tmpdir.join('test.dat'))
85 with open(p, "w") as f1:
86 s = marshal.dump(case, f1)
87 with open(p, "r") as f2:
88 obj = cpy_marshal.load(f2)
89 assert obj == case
90
91 def test_load_truncated_string():
92 s = '(\x02\x00\x00\x00i\x03\x00\x00\x00sB\xf9\x00\x00\nabcd'
93 with pytest.raises(EOFError):
94 marshal.loads(s)
95
96 def test_dump_unicode_length():
97 s = b'123\xe9'.decode('latin-1')
98 r = marshal.dumps(s)
99 assert r == b'u\x05\x00\x00\x00123\xc3\xa9'