| 1 |
"""Tests for PyPy-specific pickle extensions (pypy_extensions flag). |
| 2 |
|
| 3 |
These test the compact ASCII-list pickle format emitted by Pickler when |
| 4 |
pypy_extensions=True, and the corresponding fast unpickle path. |
| 5 |
""" |
| 6 |
import io |
| 7 |
import pytest |
| 8 |
from _pickle import Pickler, dumps, loads |
| 9 |
|
| 10 |
|
| 11 |
def _pypy_dumps(obj, protocol=4): |
| 12 |
"""Pickle obj using the PyPy-specific ASCII-list optimisation.""" |
| 13 |
buf = io.BytesIO() |
| 14 |
p = Pickler(buf, protocol) |
| 15 |
p.pypy_extensions = True |
| 16 |
p.dump(obj) |
| 17 |
return buf.getvalue() |
| 18 |
|
| 19 |
|
| 20 |
def test_attribute_default(): |
| 21 |
buf = io.BytesIO() |
| 22 |
p = Pickler(buf, 4) |
| 23 |
assert p.pypy_extensions == False |
| 24 |
p.pypy_extensions = True |
| 25 |
assert p.pypy_extensions == True |
| 26 |
|
| 27 |
|
| 28 |
def test_ascii_list_roundtrip(): |
| 29 |
data = ['hello', 'world', 'foo', 'bar'] |
| 30 |
assert loads(_pypy_dumps(data)) == data |
| 31 |
|
| 32 |
|
| 33 |
def test_ascii_list_large(): |
| 34 |
data = ['%032x' % i for i in range(10000)] |
| 35 |
assert loads(_pypy_dumps(data)) == data |
| 36 |
|
| 37 |
|
| 38 |
def test_uses_global_marker(): |
| 39 |
blob = _pypy_dumps(['a', 'b', 'c']) |
| 40 |
assert b'pypy._builtin' in blob |
| 41 |
assert b'_ascii_list_unpickle' in blob |
| 42 |
|
| 43 |
|
| 44 |
def test_flag_off_no_marker(): |
| 45 |
assert b'pypy._builtin' not in dumps(['a', 'b', 'c'], 4) |
| 46 |
|
| 47 |
|
| 48 |
def test_empty_list(): |
| 49 |
assert loads(_pypy_dumps([])) == [] |
| 50 |
|
| 51 |
|
| 52 |
def test_single_item(): |
| 53 |
assert loads(_pypy_dumps(['only'])) == ['only'] |
| 54 |
|
| 55 |
|
| 56 |
def test_empty_strings(): |
| 57 |
data = ['', '', 'hello', ''] |
| 58 |
assert loads(_pypy_dumps(data)) == data |
| 59 |
|
| 60 |
|
| 61 |
def test_max_length_string(): |
| 62 |
|
| 63 |
data = ['a' * 255, 'b' * 128] |
| 64 |
blob = _pypy_dumps(data) |
| 65 |
assert b'pypy._builtin' in blob |
| 66 |
assert loads(blob) == data |
| 67 |
|
| 68 |
|
| 69 |
def test_non_ascii_falls_back(): |
| 70 |
|
| 71 |
data = ['hello', 'wörld'] |
| 72 |
blob = _pypy_dumps(data) |
| 73 |
assert b'pypy._builtin' not in blob |
| 74 |
assert loads(blob) == data |
| 75 |
|
| 76 |
|
| 77 |
def test_mixed_types_falls_back(): |
| 78 |
|
| 79 |
data = ['hello', 42, 'world'] |
| 80 |
blob = _pypy_dumps(data) |
| 81 |
assert b'pypy._builtin' not in blob |
| 82 |
assert loads(blob) == data |
| 83 |
|
| 84 |
|
| 85 |
def test_proto_less_than_2_falls_back(): |
| 86 |
|
| 87 |
data = ['hello', 'world'] |
| 88 |
for proto in (0, 1): |
| 89 |
blob = _pypy_dumps(data, protocol=proto) |
| 90 |
assert b'pypy._builtin' not in blob |
| 91 |
assert loads(blob) == data |
| 92 |
|
| 93 |
|
| 94 |
def test_long_string_falls_back(): |
| 95 |
|
| 96 |
data = ['x' * 256, 'short'] |
| 97 |
blob = _pypy_dumps(data) |
| 98 |
assert b'pypy._builtin' not in blob |
| 99 |
assert loads(blob) == data |
| 100 |
|
| 101 |
|
| 102 |
def test_shared_reference(): |
| 103 |
|
| 104 |
data = ['a', 'b', 'c'] |
| 105 |
obj = [data, data] |
| 106 |
result = loads(_pypy_dumps(obj)) |
| 107 |
assert result[0] == data |
| 108 |
assert result[1] == data |
| 109 |
assert result[0] is result[1] |
| 110 |
|
| 111 |
|
| 112 |
def test_nested(): |
| 113 |
|
| 114 |
data = {'key': ['foo', 'bar', 'baz'], 'other': 42} |
| 115 |
assert loads(_pypy_dumps(data)) == data |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
def test_bytes_list_roundtrip(): |
| 121 |
data = [b'hello', b'world', b'foo', b'bar'] |
| 122 |
assert loads(_pypy_dumps(data)) == data |
| 123 |
|
| 124 |
|
| 125 |
def test_bytes_list_uses_global_marker(): |
| 126 |
blob = _pypy_dumps([b'a', b'b', b'c']) |
| 127 |
assert b'pypy._builtin' in blob |
| 128 |
assert b'_bytes_list_unpickle' in blob |
| 129 |
|
| 130 |
|
| 131 |
def test_bytes_list_empty(): |
| 132 |
assert loads(_pypy_dumps([])) == [] |
| 133 |
|
| 134 |
|
| 135 |
def test_bytes_list_single_item(): |
| 136 |
assert loads(_pypy_dumps([b'only'])) == [b'only'] |
| 137 |
|
| 138 |
|
| 139 |
def test_bytes_list_empty_bytes(): |
| 140 |
data = [b'', b'', b'hello', b''] |
| 141 |
assert loads(_pypy_dumps(data)) == data |
| 142 |
|
| 143 |
|
| 144 |
def test_bytes_list_max_length(): |
| 145 |
data = [b'a' * 255, b'b' * 128] |
| 146 |
blob = _pypy_dumps(data) |
| 147 |
assert b'pypy._builtin' in blob |
| 148 |
assert loads(blob) == data |
| 149 |
|
| 150 |
|
| 151 |
def test_bytes_list_long_item_falls_back(): |
| 152 |
data = [b'x' * 256, b'short'] |
| 153 |
blob = _pypy_dumps(data) |
| 154 |
assert b'_bytes_list_unpickle' not in blob |
| 155 |
assert loads(blob) == data |
| 156 |
|
| 157 |
|
| 158 |
def test_bytes_list_flag_off_no_marker(): |
| 159 |
assert b'_bytes_list_unpickle' not in dumps([b'a', b'b', b'c'], 4) |
| 160 |
|
| 161 |
|
| 162 |
def test_bytes_list_proto_less_than_2_falls_back(): |
| 163 |
data = [b'hello', b'world'] |
| 164 |
for proto in (0, 1): |
| 165 |
blob = _pypy_dumps(data, protocol=proto) |
| 166 |
assert b'pypy._builtin' not in blob |
| 167 |
assert loads(blob) == data |
| 168 |
|
| 169 |
|
| 170 |
def test_bytes_list_large(): |
| 171 |
data = [bytes([i % 256]) * 16 for i in range(10000)] |
| 172 |
assert loads(_pypy_dumps(data)) == data |
| 173 |
|
| 174 |
|
| 175 |
def test_bytes_list_shared_reference(): |
| 176 |
data = [b'a', b'b', b'c'] |
| 177 |
obj = [data, data] |
| 178 |
result = loads(_pypy_dumps(obj)) |
| 179 |
assert result[0] == data |
| 180 |
assert result[1] == data |
| 181 |
assert result[0] is result[1] |
| 182 |
|
| 183 |
|
| 184 |
def test_bytes_list_mixed_types_falls_back(): |
| 185 |
data = [b'hello', 42, b'world'] |
| 186 |
blob = _pypy_dumps(data) |
| 187 |
assert b'_bytes_list_unpickle' not in blob |
| 188 |
assert loads(blob) == data |