| 1 |
from pypy.module.marshal import interp_marshal |
| 2 |
from pypy.interpreter.error import OperationError |
| 3 |
from pypy.objspace.std.intobject import W_IntObject |
| 4 |
import sys |
| 5 |
|
| 6 |
|
| 7 |
def test_long_more(space): |
| 8 |
import marshal, struct |
| 9 |
|
| 10 |
class FakeM: |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
version = 2 |
| 15 |
def __init__(self): |
| 16 |
self.seen = [] |
| 17 |
def start(self, code): |
| 18 |
self.seen.append(code) |
| 19 |
def put_int(self, value): |
| 20 |
self.seen.append(struct.pack("<i", value)) |
| 21 |
def put_short(self, value): |
| 22 |
self.seen.append(struct.pack("<h", value)) |
| 23 |
|
| 24 |
def _marshal_check(x): |
| 25 |
expected = marshal.dumps(long(x)) |
| 26 |
w_obj = space.wraplong(x) |
| 27 |
m = FakeM() |
| 28 |
interp_marshal.marshal(space, w_obj, m) |
| 29 |
assert ''.join(m.seen) == expected |
| 30 |
|
| 31 |
u = interp_marshal.StringUnmarshaller(space, space.newbytes(expected)) |
| 32 |
w_long = u.load_w_obj() |
| 33 |
assert space.eq_w(w_long, w_obj) |
| 34 |
|
| 35 |
for sign in [1L, -1L]: |
| 36 |
for i in range(100): |
| 37 |
_marshal_check(sign * ((1L << i) - 1L)) |
| 38 |
_marshal_check(sign * (1L << i)) |
| 39 |
|
| 40 |
def test_int_roundtrip(space): |
| 41 |
a = sys.maxsize |
| 42 |
w_a = space.newint(a) |
| 43 |
m = interp_marshal.StringMarshaller(space, 4) |
| 44 |
interp_marshal.marshal(space, w_a, m) |
| 45 |
s = m.get_value() |
| 46 |
u = interp_marshal.StringUnmarshaller(space, space.newbytes(s)) |
| 47 |
w_res = u.load_w_obj() |
| 48 |
|
| 49 |
assert type(w_res) is W_IntObject |
| 50 |
assert w_res.intval == w_a.intval == a |
| 51 |
|
| 52 |
def test_hidden_applevel(space): |
| 53 |
w_s = interp_marshal.dumps(space, space.appdef('''(): pass''').code) |
| 54 |
w_c = interp_marshal._loads(space, w_s) |
| 55 |
assert w_c.hidden_applevel == False |
| 56 |
w_c = interp_marshal._loads(space, w_s, hidden_applevel=True) |
| 57 |
assert w_c.hidden_applevel == True |