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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
from pypy.module.marshal import interp_marshal
from pypy.interpreter.error import OperationError
from pypy.objspace.std.intobject import W_IntObject
import sys
class AppTestMarshalMore:
spaceconfig = dict(usemodules=('array',))
def test_marshal_bufferlike_object(self):
import marshal, array
s = marshal.dumps(array.array('b', b'asd'))
t = marshal.loads(s)
assert type(t) is bytes and t == b'asd'
s = marshal.dumps(memoryview(b'asd'))
t = marshal.loads(s)
assert type(t) is bytes and t == b'asd'
def test_unmarshal_evil_long(self):
import marshal
raises(ValueError, marshal.loads, b'l\x02\x00\x00\x00\x00\x00\x00\x00')
def test_marshal_code_object(self):
def foo(a, b):
pass
import marshal
s = marshal.dumps(foo.__code__)
code2 = marshal.loads(s)
for attr_name in dir(code2):
if attr_name.startswith("co_"):
if callable(getattr(code2, attr_name)): # co_lines, co_positions
assert list(getattr(code2, attr_name)()) == list(getattr(foo.__code__, attr_name)())
else:
assert getattr(code2, attr_name) == getattr(foo.__code__, attr_name)
def test_marshal_code_positions(self):
def foo(a, b):
return (
a.x +
b.y
)
import marshal
s = marshal.dumps(foo.__code__)
code2 = marshal.loads(s)
assert code2.co_positions() == foo.__code__.co_positions()
def test_unmarshal_ascii(self):
import marshal
s = marshal.loads(b"a\x04\x00\x00\x00abcd")
assert s == u"abcd"
def test_marshal_ascii(self):
import marshal
s = marshal.dumps("a")
assert s.endswith(b"\x01a")
s = marshal.dumps("a" * 1000)
assert s == b"\xe1\xe8\x03\x00\x00" + b"a" * 1000
for x in ("?" * 255, "a" * 1000, "xyza"):
s = marshal.dumps(x)
s1 = marshal.dumps((x, x)) # check that sharing works
assert s1 == b")\x02" + s + b"r\x00\x00\x00\x00"
def test_shared_string(self):
import marshal
x = "hello, "
x += "world"
xl = 256
xl **= 100
for version in [2, 3]:
s = marshal.dumps((x, x), version)
assert s.count(b'hello, world') == 2 if version < 3 else 1
y = marshal.loads(s)
assert y == (x, x)
#
s = marshal.dumps((xl, xl), version)
if version < 3:
assert 200 < len(s) < 250
else:
assert 100 < len(s) < 125
yl = marshal.loads(s)
assert yl == (xl, xl)
class AppTestMarshalSmallLong(AppTestMarshalMore):
spaceconfig = dict(usemodules=('array',),
**{"objspace.std.withsmalllong": True})
def test_long_more(space):
import marshal, struct
class FakeM:
# NOTE: marshal is platform independent, running this test must assume
# that self.seen gets values from the endianess of the marshal module.
# (which is little endian!)
version = 2
def __init__(self):
self.seen = []
def start(self, code):
self.seen.append(code)
def put_int(self, value):
self.seen.append(struct.pack("<i", value))
def put_short(self, value):
self.seen.append(struct.pack("<h", value))
def _marshal_check(x):
expected = marshal.dumps(long(x))
w_obj = space.wraplong(x)
m = FakeM()
interp_marshal.marshal(space, w_obj, m)
assert ''.join(m.seen) == expected
#
u = interp_marshal.StringUnmarshaller(space, space.newbytes(expected))
w_long = u.load_w_obj()
assert space.eq_w(w_long, w_obj)
for sign in [1L, -1L]:
for i in range(100):
_marshal_check(sign * ((1L << i) - 1L))
_marshal_check(sign * (1L << i))
def test_int_roundtrip(space):
a = 0xffffffff
w_a = space.newint(a)
m = interp_marshal.StringMarshaller(space, 4)
interp_marshal.marshal(space, w_a, m)
s = m.get_value()
u = interp_marshal.StringUnmarshaller(space, space.newbytes(s))
w_res = u.load_w_obj()
assert type(w_res) is W_IntObject
assert w_res.intval == w_a.intval == a
def test_hidden_applevel(space):
w_s = interp_marshal.dumps(space, space.appdef('''(): pass''').code)
w_c = interp_marshal._loads(space, w_s)
assert w_c.hidden_applevel == False
w_c = interp_marshal._loads(space, w_s, hidden_applevel=True)
assert w_c.hidden_applevel == True
|