MCPcopy Index your code
hub / github.com/RustPython/RustPython / test_start_offset

Method test_start_offset

Lib/test/test_dis.py:2235–2285  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

2233 return dis._get_instructions_bytes(code)
2234
2235 def test_start_offset(self):
2236 # When no extended args are present,
2237 # start_offset should be equal to offset
2238
2239 instructions = list(dis.Bytecode(_f))
2240 for instruction in instructions:
2241 self.assertEqual(instruction.offset, instruction.start_offset)
2242
2243 def last_item(iterable):
2244 return functools.reduce(lambda a, b : b, iterable)
2245
2246 code = bytes([
2247 opcode.opmap["LOAD_FAST"], 0x00,
2248 opcode.opmap["EXTENDED_ARG"], 0x01,
2249 opcode.opmap["POP_JUMP_IF_TRUE"], 0xFF,
2250 ])
2251 labels_map = dis._make_labels_map(code)
2252 jump = last_item(self.get_instructions(code))
2253 self.assertEqual(4, jump.offset)
2254 self.assertEqual(2, jump.start_offset)
2255
2256 code = bytes([
2257 opcode.opmap["LOAD_FAST"], 0x00,
2258 opcode.opmap["EXTENDED_ARG"], 0x01,
2259 opcode.opmap["EXTENDED_ARG"], 0x01,
2260 opcode.opmap["EXTENDED_ARG"], 0x01,
2261 opcode.opmap["POP_JUMP_IF_TRUE"], 0xFF,
2262 opcode.opmap["CACHE"], 0x00,
2263 ])
2264 jump = last_item(self.get_instructions(code))
2265 self.assertEqual(8, jump.offset)
2266 self.assertEqual(2, jump.start_offset)
2267
2268 code = bytes([
2269 opcode.opmap["LOAD_FAST"], 0x00,
2270 opcode.opmap["EXTENDED_ARG"], 0x01,
2271 opcode.opmap["POP_JUMP_IF_TRUE"], 0xFF,
2272 opcode.opmap["CACHE"], 0x00,
2273 opcode.opmap["EXTENDED_ARG"], 0x01,
2274 opcode.opmap["EXTENDED_ARG"], 0x01,
2275 opcode.opmap["EXTENDED_ARG"], 0x01,
2276 opcode.opmap["POP_JUMP_IF_TRUE"], 0xFF,
2277 opcode.opmap["CACHE"], 0x00,
2278 ])
2279 instructions = list(self.get_instructions(code))
2280 # 1st jump
2281 self.assertEqual(4, instructions[2].offset)
2282 self.assertEqual(2, instructions[2].start_offset)
2283 # 2nd jump
2284 self.assertEqual(14, instructions[6].offset)
2285 self.assertEqual(8, instructions[6].start_offset)
2286
2287 def test_cache_offset_and_end_offset(self):
2288 code = bytes([

Callers

nothing calls this directly

Calls 3

get_instructionsMethod · 0.95
listClass · 0.85
assertEqualMethod · 0.45

Tested by

no test coverage detected