Debian Codegraph @

rpython/rlib/rstruct/test/test_pack.py

raw symbols SCIP
1 import pytest
2 from rpython.rlib.rarithmetic import r_uint, r_longlong, r_ulonglong
3 from rpython.rlib.rstruct import standardfmttable, nativefmttable
4 from rpython.rlib.rstruct.error import StructOverflowError
5 from rpython.rlib import buffer
6 from rpython.rlib.buffer import SubBuffer
7 from rpython.rlib.mutbuffer import MutableStringBuffer
8 from rpython.rlib import rawstorage
9 import struct
10
11 class FakeFormatIter(object):
12
13 def __init__(self, bigendian, wbuf, value):
14 self.value = value
15 self.bigendian = bigendian
16 self.wbuf = wbuf
17 self.pos = 0
18
19 def advance(self, count):
20 self.pos += count
21
22 def _accept_arg(self):
23 return self.value
24
25 def __getattr__(self, name):
26 if name.startswith('accept_'):
27 return self._accept_arg
28 raise AttributeError(name)
29
30
31 class PackSupport(object):
32 """
33 These test tests only the various pack_* functions, individually. There
34 is no RPython interface to them, as for now they are used only to
35 implement struct.pack in pypy/module/struct
36 """
37
38 bigendian = None
39 fmt_prefix = None
40 fmttable = None
41
42 USE_FASTPATH = True
43 ALLOW_SLOWPATH = True
44 ALLOW_FASTPATH = True
45 ALLOW_UNALIGNED_ACCESS = rawstorage.misaligned_is_fine
46
47 def setup_method(self, meth):
48 standardfmttable.USE_FASTPATH = self.USE_FASTPATH
49 standardfmttable.ALLOW_SLOWPATH = self.ALLOW_SLOWPATH
50 standardfmttable.ALLOW_FASTPATH = self.ALLOW_FASTPATH
51 buffer.ALLOW_UNALIGNED_ACCESS = self.ALLOW_UNALIGNED_ACCESS
52
53 def teardown_method(self, meth):
54 standardfmttable.USE_FASTPATH = True
55 standardfmttable.ALLOW_SLOWPATH = True
56 standardfmttable.ALLOW_FASTPATH = True
57 buffer.ALLOW_UNALIGNED_ACCESS = rawstorage.misaligned_is_fine
58
59 def mypack(self, fmt, value):
60 size = struct.calcsize(fmt)
61 wbuf = MutableStringBuffer(size)
62 fake_fmtiter = self.mypack_into(fmt, wbuf, value)
63 # check that we called advance() the right number of times
64 assert fake_fmtiter.pos == wbuf.getlength()
65 return wbuf.finish()
66
67 def mypack_into(self, fmt, wbuf, value, advance=None):
68 fake_fmtiter = FakeFormatIter(self.bigendian, wbuf, value)
69 if advance:
70 fake_fmtiter.advance(advance)
71 attrs = self.fmttable[fmt]
72 pack = attrs['pack']
73 pack(fake_fmtiter)
74 return fake_fmtiter
75
76 def mypack_fn(self, func, size, arg, value):
77 wbuf = MutableStringBuffer(size)
78 fake_fmtiter = FakeFormatIter(self.bigendian, wbuf, value)
79 func(fake_fmtiter, arg)
80 assert fake_fmtiter.pos == wbuf.getlength()
81 return wbuf.finish()
82
83 def check(self, fmt, value):
84 expected = struct.pack(self.fmt_prefix+fmt, value)
85 got = self.mypack(fmt, value)
86 assert got == expected
87
88
89 class TestAllowSlowpath(PackSupport):
90 ALLOW_SLOWPATH = False
91 bigendian = not nativefmttable.native_is_bigendian
92 fmttable = standardfmttable.standard_fmttable
93
94 def test_slowpath_not_allowed(self):
95 # we are using a non-native endianess and ALLOW_SLOWPATH is False, so
96 # the following MUST raise
97 pytest.raises(ValueError, "self.mypack('i', 42)")
98
99
100 class TestUseFastpath(PackSupport):
101 ALLOW_SLOWPATH = False
102 bigendian = nativefmttable.native_is_bigendian
103 fmttable = standardfmttable.standard_fmttable
104
105 def test_fastpath_taken(self):
106 # we are using native endianess and slowpath is not allowed, so the
107 # following MUST succeed
108 expected = struct.pack('i', 42)
109 assert self.mypack('i', 42) == expected
110
111 class TestAllowFastPath(PackSupport):
112 ALLOW_FASTPATH = False
113 bigendian = nativefmttable.native_is_bigendian
114 fmttable = standardfmttable.standard_fmttable
115
116 def test_fastpath_not_allowed(self):
117 # we are using a native endianess but ALLOW_FASTPATH is False, so
118 # the following MUST raise
119 pytest.raises(ValueError, "self.mypack('i', 42)")
120
121
122 class BaseTestPack(PackSupport):
123
124 def test_pack_int(self):
125 self.check('b', 42)
126 self.check('B', 242)
127 self.check('h', 32767)
128 self.check('H', 32768)
129 self.check("i", 0x41424344)
130 self.check("i", -3)
131 self.check("i", -2147483648)
132 self.check("I", r_uint(0x81424344))
133 self.check("q", r_longlong(0x4142434445464748))
134 self.check("q", r_longlong(-0x41B2B3B4B5B6B7B8))
135 self.check("Q", r_ulonglong(0x8142434445464748))
136
137 def test_pack_ieee(self):
138 self.check('f', 123.456)
139 self.check('d', 123.456789)
140
141 def test_pack_halffloat(self):
142 size = 2
143 wbuf = MutableStringBuffer(size)
144 self.mypack_into('e', wbuf, 6.5e+04)
145 got = wbuf.finish()
146 if self.bigendian:
147 assert got == b'\x7b\xef'
148 else:
149 assert got == b'\xef\x7b'
150
151 def test_float_overflow(self):
152 if self.fmt_prefix == '@':
153 # native packing, no overflow
154 self.check('f', 10e100)
155 else:
156 # non-native packing, should raise
157 pytest.raises(StructOverflowError, "self.mypack('f', 10e100)")
158
159 def test_pack_char(self):
160 self.check('c', 'a')
161
162 def test_pack_bool(self):
163 self.check('?', True)
164 self.check('?', False)
165
166 def test_pack_pad(self):
167 s = self.mypack_fn(standardfmttable.pack_pad,
168 arg=4, value=None, size=4)
169 assert s == '\x00'*4
170
171 def test_pack_string(self):
172 s = self.mypack_fn(standardfmttable.pack_string,
173 arg=8, value='hello', size=8)
174 assert s == 'hello\x00\x00\x00'
175 #
176 s = self.mypack_fn(standardfmttable.pack_string,
177 arg=8, value='hello world', size=8)
178 assert s == 'hello wo'
179
180 def test_pack_pascal(self):
181 s = self.mypack_fn(standardfmttable.pack_pascal,
182 arg=8, value='hello', size=8)
183 assert s == '\x05hello\x00\x00'
184
185
186 class TestPackLittleEndian(BaseTestPack):
187 bigendian = False
188 fmt_prefix = '<'
189 fmttable = standardfmttable.standard_fmttable
190
191 class TestPackLittleEndianSlowPath(TestPackLittleEndian):
192 USE_FASTPATH = False
193
194 class TestPackBigEndian(BaseTestPack):
195 bigendian = True
196 fmt_prefix = '>'
197 fmttable = standardfmttable.standard_fmttable
198
199 class TestPackBigEndianSlowPath(TestPackBigEndian):
200 USE_FASTPATH = False
201
202
203 class TestNative(BaseTestPack):
204 # native packing automatically use the proper endianess, so it should
205 # always take the fast path
206 ALLOW_SLOWPATH = False
207 bigendian = nativefmttable.native_is_bigendian
208 fmt_prefix = '@'
209 fmttable = nativefmttable.native_fmttable
210
211 class TestNativeSlowPath(BaseTestPack):
212 USE_FASTPATH = False
213 bigendian = nativefmttable.native_is_bigendian
214 fmt_prefix = '@'
215 fmttable = nativefmttable.native_fmttable
216
217
218 class TestUnaligned(PackSupport):
219 ALLOW_FASTPATH = False
220 ALLOW_UNALIGNED_ACCESS = False
221 bigendian = nativefmttable.native_is_bigendian
222 fmttable = nativefmttable.native_fmttable
223
224 def test_unaligned(self):
225 # to force a non-aligned 'i'
226 expected = struct.pack('=BBi', 0xAB, 0xCD, 0x1234)
227 #
228 wbuf = MutableStringBuffer(len(expected))
229 wbuf.setitem(0, chr(0xAB))
230 wbuf.setitem(1, chr(0xCD))
231 fake_fmtiter = self.mypack_into('i', wbuf, 0x1234, advance=2)
232 assert fake_fmtiter.pos == wbuf.getlength()
233 got = wbuf.finish()
234 assert got == expected
235
236 def test_subbuffer(self):
237 # to force a non-aligned 'i'
238 expected = struct.pack('=BBi', 0xAB, 0xCD, 0x1234)
239 size = len(expected)
240 #
241 wbuf = MutableStringBuffer(size)
242 wsubbuf = SubBuffer(wbuf, 2, size-4)
243 wbuf.setitem(0, chr(0xAB))
244 wbuf.setitem(1, chr(0xCD))
245 fake_fmtiter = self.mypack_into('i', wsubbuf, 0x1234)
246 assert fake_fmtiter.pos == wbuf.getlength()-2 # -2 since it's a SubBuffer
247 got = wbuf.finish()
248 assert got == expected