import sys from pypy.module.pypyjit.test_pypy_c.test_00_model import BaseTestPyPyC if sys.maxsize == 2 ** 63 - 1: extra = """ i8 = int_ge(i4, -2147483648) guard_true(i8, descr=...) i9 = int_le(i4, 2147483647) guard_true(i9, descr=...) """ else: extra = "" class TestStruct(BaseTestPyPyC): def test_struct_function(self): def main(n): import struct i = 1 while i < n: buf = struct.pack(") """) def test_unpack_bytearray(self): def main(n): import struct buf = struct.pack('H', 0x1234) buf = bytearray(buf) i = 1 res = 0 while i < n: val = struct.unpack("h", buf)[0] # ID: unpack res += val i += 1 return res log = self.run(main, [1000]) assert log.result == main(1000) loop, = log.loops_by_filename(self.filepath) # the offset of gc_load_indexed_i used to be the constant 0. However, # now it is 'i46' because we need to add 0 to # W_BytearrayObject._offset assert loop.match_by_id('unpack', """ guard_not_invalidated(descr=...) i70 = gc_load_indexed_i(p48, i46, 1, _, -2) """) def test_pack_into_raw_buffer(self): def main(n): import array import struct buf = array.array('b', b'\x00'*8) i = 1 while i < n: struct.pack_into("h", buf, 4, i) # ID: pack_into i += 1 return i log = self.run(main, [1000]) assert log.result == main(1000) loop, = log.loops_by_filename(self.filepath) assert loop.match_by_id('pack_into', """\ guard_not_invalidated(descr=...) i65 = int_le(i58, 32767) guard_true(i65, descr=...) raw_store(i55, 4, i58, descr=) """) def test_pack_into_bytearray(self): def main(n): import struct buf = bytearray(8) i = 1 while i < n: struct.pack_into("h", buf, 4, i) # ID: pack_into i += 1 return i log = self.run(main, [1000]) assert log.result == main(1000) loop, = log.loops_by_filename(self.filepath) assert loop.match_by_id('pack_into', """\ dummy_get_utf8? guard_not_invalidated(descr=...) dummy_get_utf8? p68 = getfield_gc_r(p14, descr=) i69 = getfield_gc_i(p68, descr=) i70 = getfield_gc_i(p14, descr=) i71 = int_sub(i69, i70) i72 = int_sub(i71, 1) i73 = int_sub(i71, 5) i75 = int_lt(i73, 2) guard_false(i75, descr=...) i77 = int_le(i62, 32767) guard_true(i77, descr=...) p78 = getfield_gc_r(p68, descr=) i81 = int_add(4, i70) gc_store_indexed(p78, i81, i62, 1, _, 2, descr=) """)