MCPcopy Index your code
hub / github.com/python/cpython / test_start_offset

Method test_start_offset

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

Source from the content-addressed store, hash-verified

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