File: test_marshal_extra.py

package info (click to toggle)
pypy3 7.3.19%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 212,236 kB
  • sloc: python: 2,098,316; ansic: 540,565; sh: 21,462; asm: 14,419; cpp: 4,451; makefile: 4,209; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 12; awk: 4
file content (99 lines) | stat: -rw-r--r-- 2,255 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import pytest
import sys
import marshal as cpy_marshal
from lib_pypy import _marshal as marshal

hello = "he"
hello += "llo"
def func(x):
    return lambda y: x+y
scopefunc = func(42)

SUBCLASSABLE = [
    42,
    sys.maxsize,
    -1.25,
    2+5j,
    42,
    -1234567890123456789012345678901234567890,
    hello,   # not interned
    "hello",
    (),
    (1, 2),
    [],
    [3, 4],
    {},
    {5: 6, 7: 8},
    u'hello',
    set(),
    set([1, 2]),
    frozenset(),
    frozenset([3, 4]),
]

TESTCASES = SUBCLASSABLE + [
    None,
    False,
    True,
    StopIteration,
    Ellipsis,
    func.__code__,
    scopefunc.__code__,
]


@pytest.mark.parametrize('case', TESTCASES)
def test_dumps_and_reload(case):
    s = marshal.dumps(case)
    obj = marshal.loads(s)
    assert obj == case

@pytest.mark.parametrize('case', TESTCASES)
def test_loads_from_cpython(case):
    s = cpy_marshal.dumps(case, 1)  # XXX: fails with version 2
    obj = marshal.loads(s)
    assert obj == case

@pytest.mark.parametrize('case', TESTCASES)
def test_dumps_to_cpython(case):
    s = marshal.dumps(case)
    obj = cpy_marshal.loads(s)
    assert obj == case

@pytest.mark.parametrize('case', SUBCLASSABLE)
def test_dumps_subclass(case):
    class Subclass(type(case)):
        pass
    case = Subclass(case)
    s = marshal.dumps(case)
    obj = marshal.loads(s)
    assert obj == case

@pytest.mark.parametrize('case', TESTCASES)
def test_load_from_cpython(tmpdir, case):
    p = str(tmpdir.join('test.dat'))

    with open(p, "w") as f1:
        s = cpy_marshal.dump(case, f1, 1)  # XXX: fails with version 2
    with open(p, "r") as f2:
        obj = marshal.load(f2)
    assert obj == case

@pytest.mark.parametrize('case', TESTCASES)
def test_dump_to_cpython(tmpdir, case):
    p = str(tmpdir.join('test.dat'))
    with open(p, "w") as f1:
        s = marshal.dump(case, f1)
    with open(p, "r") as f2:
        obj = cpy_marshal.load(f2)
    assert obj == case

def test_load_truncated_string():
    s = '(\x02\x00\x00\x00i\x03\x00\x00\x00sB\xf9\x00\x00\nabcd'
    with pytest.raises(EOFError):
        marshal.loads(s)

def test_dump_unicode_length():
    s = b'123\xe9'.decode('latin-1')
    r = marshal.dumps(s)
    assert r == b'u\x05\x00\x00\x00123\xc3\xa9'