Iterate over the instructions in a bytecode string. Generates a sequence of Instruction namedtuples giving the details of each opcode. Additional information about the code's runtime environment (e.g. variable names, co_consts) can be specified using optional arguments.
(code, varname_from_oparg=None,
names=None, co_consts=None,
linestarts=None, line_offset=0,
exception_entries=(), co_positions=None,
show_caches=False)
| 421 | return 'JUMP_BACKWARD' in opname[op] |
| 422 | |
| 423 | def _get_instructions_bytes(code, varname_from_oparg=None, |
| 424 | names=None, co_consts=None, |
| 425 | linestarts=None, line_offset=0, |
| 426 | exception_entries=(), co_positions=None, |
| 427 | show_caches=False): |
| 428 | """Iterate over the instructions in a bytecode string. |
| 429 | |
| 430 | Generates a sequence of Instruction namedtuples giving the details of each |
| 431 | opcode. Additional information about the code's runtime environment |
| 432 | (e.g. variable names, co_consts) can be specified using optional |
| 433 | arguments. |
| 434 | |
| 435 | """ |
| 436 | co_positions = co_positions or iter(()) |
| 437 | get_name = None if names is None else names.__getitem__ |
| 438 | labels = set(findlabels(code)) |
| 439 | for start, end, target, _, _ in exception_entries: |
| 440 | for i in range(start, end): |
| 441 | labels.add(target) |
| 442 | starts_line = None |
| 443 | for offset, op, arg in _unpack_opargs(code): |
| 444 | if linestarts is not None: |
| 445 | starts_line = linestarts.get(offset, None) |
| 446 | if starts_line is not None: |
| 447 | starts_line += line_offset |
| 448 | is_jump_target = offset in labels |
| 449 | argval = None |
| 450 | argrepr = '' |
| 451 | positions = Positions(*next(co_positions, ())) |
| 452 | deop = _deoptop(op) |
| 453 | if arg is not None: |
| 454 | # Set argval to the dereferenced value of the argument when |
| 455 | # available, and argrepr to the string representation of argval. |
| 456 | # _disassemble_bytes needs the string repr of the |
| 457 | # raw name index for LOAD_GLOBAL, LOAD_CONST, etc. |
| 458 | argval = arg |
| 459 | if deop in hasconst: |
| 460 | argval, argrepr = _get_const_info(deop, arg, co_consts) |
| 461 | elif deop in hasname: |
| 462 | if deop == LOAD_GLOBAL: |
| 463 | argval, argrepr = _get_name_info(arg//2, get_name) |
| 464 | if (arg & 1) and argrepr: |
| 465 | argrepr = "NULL + " + argrepr |
| 466 | else: |
| 467 | argval, argrepr = _get_name_info(arg, get_name) |
| 468 | elif deop in hasjabs: |
| 469 | argval = arg*2 |
| 470 | argrepr = "to " + repr(argval) |
| 471 | elif deop in hasjrel: |
| 472 | signed_arg = -arg if _is_backward_jump(deop) else arg |
| 473 | argval = offset + 2 + signed_arg*2 |
| 474 | argrepr = "to " + repr(argval) |
| 475 | elif deop in haslocal or deop in hasfree: |
| 476 | argval, argrepr = _get_name_info(arg, varname_from_oparg) |
| 477 | elif deop in hascompare: |
| 478 | argval = cmp_op[arg] |
| 479 | argrepr = argval |
| 480 | elif deop == FORMAT_VALUE: |
no test coverage detected