| 1 |
import py |
| 2 |
import marshal |
| 3 |
from rpython.rlib.rmarshal import * |
| 4 |
from rpython.annotator import model as annmodel |
| 5 |
from rpython.rlib.rarithmetic import LONG_BIT |
| 6 |
from rpython.rlib.rfloat import formatd |
| 7 |
|
| 8 |
types_that_can_be_none = [ |
| 9 |
[int], |
| 10 |
annmodel.SomeString(can_be_None=True), |
| 11 |
annmodel.s_None, |
| 12 |
{int: int}, |
| 13 |
] |
| 14 |
|
| 15 |
|
| 16 |
def test_marshaller(): |
| 17 |
buf = [] |
| 18 |
get_marshaller(int)(buf, 5) |
| 19 |
assert marshal.loads(''.join(buf)) == 5 |
| 20 |
|
| 21 |
buf = [] |
| 22 |
get_marshaller(int)(buf, -555) |
| 23 |
assert marshal.loads(''.join(buf)) == -555 |
| 24 |
|
| 25 |
buf = [] |
| 26 |
get_marshaller(float)(buf, 3.25) |
| 27 |
assert marshal.loads(''.join(buf)) == 3.25 |
| 28 |
|
| 29 |
buf = [] |
| 30 |
get_marshaller(str)(buf, "hello, world") |
| 31 |
assert marshal.loads(''.join(buf)) == "hello, world" |
| 32 |
|
| 33 |
buf = [] |
| 34 |
get_marshaller(bool)(buf, False) |
| 35 |
assert marshal.loads(''.join(buf)) is False |
| 36 |
|
| 37 |
buf = [] |
| 38 |
get_marshaller(bool)(buf, True) |
| 39 |
assert marshal.loads(''.join(buf)) is True |
| 40 |
|
| 41 |
buf = [] |
| 42 |
get_marshaller(r_longlong)(buf, r_longlong(0x12380000007)) |
| 43 |
assert marshal.loads(''.join(buf)) == 0x12380000007 |
| 44 |
|
| 45 |
buf = [] |
| 46 |
get_marshaller(r_longlong)(buf, r_longlong(-0x12380000007)) |
| 47 |
assert marshal.loads(''.join(buf)) == -0x12380000007 |
| 48 |
|
| 49 |
if LONG_BIT > 32: |
| 50 |
buf = [] |
| 51 |
get_marshaller(int)(buf, -0x12340000007) |
| 52 |
assert marshal.loads(''.join(buf)) == -0x12340000007 |
| 53 |
|
| 54 |
buf = [] |
| 55 |
get_marshaller([int])(buf, [2, 5, -7]) |
| 56 |
assert marshal.loads(''.join(buf)) == [2, 5, -7] |
| 57 |
|
| 58 |
buf = [] |
| 59 |
get_marshaller((int, float, (str, ())))(buf, (7, -1.5, ("foo", ()))) |
| 60 |
assert marshal.loads(''.join(buf)) == (7, -1.5, ("foo", ())) |
| 61 |
|
| 62 |
buf = [] |
| 63 |
get_marshaller({int: str})(buf, {2: "foo", -3: "bar"}) |
| 64 |
assert marshal.loads(''.join(buf)) == {2: "foo", -3: "bar"} |
| 65 |
|
| 66 |
for typ in types_that_can_be_none: |
| 67 |
buf = [] |
| 68 |
get_marshaller(typ)(buf, None) |
| 69 |
assert marshal.loads(''.join(buf)) is None |
| 70 |
|
| 71 |
|
| 72 |
def test_unmarshaller(): |
| 73 |
buf = 'i\x05\x00\x00\x00' |
| 74 |
assert get_unmarshaller(int)(buf) == 5 |
| 75 |
|
| 76 |
buf = 'i\x00\xf0\xff\xff' |
| 77 |
assert get_unmarshaller(int)(buf) == -4096 |
| 78 |
|
| 79 |
buf = 'f\x043.25' |
| 80 |
assert get_unmarshaller(float)(buf) == 3.25 |
| 81 |
|
| 82 |
buf = 's\x0c\x00\x00\x00hello, world' |
| 83 |
assert get_unmarshaller(str)(buf) == "hello, world" |
| 84 |
|
| 85 |
buf = 's\x01\x00\x00\x00X' |
| 86 |
assert get_unmarshaller(annmodel.SomeChar())(buf) == "X" |
| 87 |
|
| 88 |
buf = 'i\x05\x00\x00\x00' |
| 89 |
py.test.raises(ValueError, get_unmarshaller(str), buf) |
| 90 |
|
| 91 |
buf = 'F' |
| 92 |
assert get_unmarshaller(bool)(buf) is False |
| 93 |
|
| 94 |
buf = 'T' |
| 95 |
assert get_unmarshaller(bool)(buf) is True |
| 96 |
|
| 97 |
buf = 'I\x07\x00\x00\x80\x23\x01\x00\x00' |
| 98 |
assert get_unmarshaller(r_longlong)(buf) == 0x12380000007 |
| 99 |
|
| 100 |
buf = 'I\x00\x00\x01\x83\x80\x00\x00\x97' |
| 101 |
assert get_unmarshaller(r_longlong)(buf) == -7566046822028738560L |
| 102 |
|
| 103 |
if LONG_BIT > 32: |
| 104 |
buf = 'I\x07\x00\x00\x80\x23\x01\x00\x00' |
| 105 |
assert get_unmarshaller(int)(buf) == 0x12380000007 |
| 106 |
|
| 107 |
buf = 'I\x00\x00\x01\x83\x80\x00\x00\x97' |
| 108 |
assert get_unmarshaller(int)(buf) == -7566046822028738560 |
| 109 |
|
| 110 |
buf = ('[\x03\x00\x00\x00i\x02\x00\x00\x00i\x05\x00\x00\x00' |
| 111 |
'i\xf9\xff\xff\xff') |
| 112 |
assert get_unmarshaller([int])(buf) == [2, 5, -7] |
| 113 |
|
| 114 |
buf = ('(\x02\x00\x00\x00i\x07\x00\x00\x00(\x02\x00\x00\x00' |
| 115 |
's\x03\x00\x00\x00foo(\x00\x00\x00\x00') |
| 116 |
res = get_unmarshaller((int, (str, ())))(buf) |
| 117 |
assert res == (7, ("foo", ())) |
| 118 |
|
| 119 |
buf = ('{i\xfb\xff\xff\xffs\x03\x00\x00\x00bar' |
| 120 |
'i\x06\x00\x00\x00s\x00\x00\x00\x000') |
| 121 |
res = get_unmarshaller({int: str})(buf) |
| 122 |
assert res == {-5: "bar", 6: ""} |
| 123 |
|
| 124 |
for typ in types_that_can_be_none: |
| 125 |
buf = 'N' |
| 126 |
assert get_unmarshaller(typ)(buf) is None |
| 127 |
|
| 128 |
|
| 129 |
def test_llinterp_marshal(): |
| 130 |
from rpython.rtyper.test.test_llinterp import interpret |
| 131 |
marshaller1 = get_marshaller([(int, str, float)]) |
| 132 |
marshaller2 = get_marshaller([(int, str, int)]) |
| 133 |
def f(): |
| 134 |
buf = [] |
| 135 |
marshaller1(buf, [(5, "hello", -0.5), (7, "world", 1E100)]) |
| 136 |
marshaller2(buf, [(5, "hello", 1)]) |
| 137 |
return ''.join(buf) |
| 138 |
res = interpret(f, []) |
| 139 |
res = ''.join(res.chars) |
| 140 |
if LONG_BIT == 32: |
| 141 |
assert res == ('[\x02\x00\x00\x00(\x03\x00\x00\x00i\x05\x00\x00\x00' |
| 142 |
's\x05\x00\x00\x00hellof\x04-0.5(\x03\x00\x00\x00' |
| 143 |
'i\x07\x00\x00\x00s\x05\x00\x00\x00world' |
| 144 |
'f\x061e+100' |
| 145 |
'[\x01\x00\x00\x00(\x03\x00\x00\x00i\x05\x00\x00\x00' |
| 146 |
's\x05\x00\x00\x00helloi\x01\x00\x00\x00') |
| 147 |
else: |
| 148 |
assert res == ('[\x02\x00\x00\x00(\x03\x00\x00\x00' |
| 149 |
'I\x05\x00\x00\x00\x00\x00\x00\x00' |
| 150 |
's\x05\x00\x00\x00hellof\x04-0.5(\x03\x00\x00\x00' |
| 151 |
'I\x07\x00\x00\x00\x00\x00\x00\x00' |
| 152 |
's\x05\x00\x00\x00world' |
| 153 |
'f\x061e+100' |
| 154 |
'[\x01\x00\x00\x00(\x03\x00\x00\x00' |
| 155 |
'I\x05\x00\x00\x00\x00\x00\x00\x00' |
| 156 |
's\x05\x00\x00\x00hello' |
| 157 |
'I\x01\x00\x00\x00\x00\x00\x00\x00') |
| 158 |
|
| 159 |
def test_llinterp_unmarshal(): |
| 160 |
from rpython.rtyper.test.test_llinterp import interpret |
| 161 |
unmarshaller = get_unmarshaller([(int, str, float)]) |
| 162 |
buf = ('[\x02\x00\x00\x00(\x03\x00\x00\x00i\x05\x00\x00\x00' |
| 163 |
's\x05\x00\x00\x00hellof\x04-0.5(\x03\x00\x00\x00' |
| 164 |
'i\x07\x00\x00\x00s\x05\x00\x00\x00world' |
| 165 |
'f\x061e+100') |
| 166 |
def f(): |
| 167 |
result = '' |
| 168 |
for num, string, fval in unmarshaller(buf): |
| 169 |
result += '%d=%s/%s;' % (num, string, formatd(fval, 'g', 17)) |
| 170 |
return result |
| 171 |
res = interpret(f, []) |
| 172 |
res = ''.join(res.chars) |
| 173 |
assert res == '5=hello/-0.5;7=world/1e+100;' |
| 174 |
|
| 175 |
def test_stat_result(): |
| 176 |
import os |
| 177 |
from rpython.translator.c.test.test_genc import compile |
| 178 |
from rpython.rlib.rposix_stat import s_StatResult |
| 179 |
marshal_stat_result = get_marshaller(s_StatResult) |
| 180 |
unmarshal_stat_result = get_unmarshaller(s_StatResult) |
| 181 |
def f(path): |
| 182 |
st = os.stat(path) |
| 183 |
buf = [] |
| 184 |
marshal_stat_result(buf, st) |
| 185 |
buf = ''.join(buf) |
| 186 |
st2 = unmarshal_stat_result(buf) |
| 187 |
assert st2.st_mode == st.st_mode |
| 188 |
assert st2[9] == st[9] |
| 189 |
return buf |
| 190 |
fn = compile(f, [str]) |
| 191 |
res = fn('.') |
| 192 |
st = os.stat('.') |
| 193 |
sttuple = marshal.loads(res) |
| 194 |
assert sttuple[0] == st[0] |
| 195 |
assert sttuple[1] == st[1] |
| 196 |
assert sttuple[2] == st[2] |
| 197 |
assert sttuple[3] == st[3] |
| 198 |
assert sttuple[4] == st[4] |
| 199 |
assert sttuple[5] == st[5] |
| 200 |
assert len(sttuple) in (10, 12) |
| 201 |
|
| 202 |
def test_longlong(): |
| 203 |
|
| 204 |
|
| 205 |
for nonneg in [True, False]: |
| 206 |
s_longlong = annmodel.SomeInteger(knowntype=r_longlong, nonneg=nonneg) |
| 207 |
load = get_loader(s_longlong) |
| 208 |
loader = Loader("I\x01\x23\x45\x67\x89\xab\xcd\x0e") |
| 209 |
res = load(loader) |
| 210 |
assert res == 0x0ecdab8967452301 |