File: test_cPickle.py

package info (click to toggle)
pypy 7.0.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 107,216 kB
  • sloc: python: 1,201,787; ansic: 62,419; asm: 5,169; cpp: 3,017; sh: 2,534; makefile: 545; xml: 243; lisp: 45; awk: 4
file content (34 lines) | stat: -rw-r--r-- 1,055 bytes parent folder | download | duplicates (2)
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
import pytest
import cPickle

def test_stack_underflow():
    with pytest.raises(cPickle.UnpicklingError):
        cPickle.loads("a string")

def test_bad_key():
    with pytest.raises(cPickle.UnpicklingError) as excinfo:
        cPickle.loads("v")
    assert str(excinfo.value) == "invalid load key, 'v'."

def test_find_global():
    import time, cStringIO
    entry = time.strptime('Fri Mar 27 22:20:42 2017')
    f = cStringIO.StringIO()
    cPickle.Pickler(f).dump(entry)

    f = cStringIO.StringIO(f.getvalue())
    e = cPickle.Unpickler(f).load()
    assert e == entry

    f = cStringIO.StringIO(f.getvalue())
    up = cPickle.Unpickler(f)
    up.find_global = None
    with pytest.raises(cPickle.UnpicklingError) as e:
        up.load()
    assert str(e.value) == "Global and instance pickles are not supported."

    f = cStringIO.StringIO(f.getvalue())
    up = cPickle.Unpickler(f)
    up.find_global = lambda module, name: lambda a, b: (name, a, b)
    e = up.load()
    assert e == ('struct_time', (2017, 3, 27, 22, 20, 42, 4, 86, -1), {})