| 497 | |
| 498 | |
| 499 | class _PyCodeToSource(object): |
| 500 | def __init__(self, co, memo=None): |
| 501 | if memo is None: |
| 502 | memo = {} |
| 503 | self.memo = memo |
| 504 | self.co = co |
| 505 | self.instructions = list(iter_instructions(co)) |
| 506 | self.stack = _Stack() |
| 507 | self.writer = _Writer() |
| 508 | |
| 509 | def _process_next(self, i_line): |
| 510 | instruction = self.instructions.pop(0) |
| 511 | handler_class = _op_name_to_handler.get(instruction.opname) |
| 512 | if handler_class is not None: |
| 513 | s = handler_class(i_line, instruction, self.stack, self.writer, self) |
| 514 | if DEBUG: |
| 515 | print(s) |
| 516 | |
| 517 | else: |
| 518 | if DEBUG: |
| 519 | print("UNHANDLED", instruction) |
| 520 | |
| 521 | def build_line_to_contents(self): |
| 522 | co = self.co |
| 523 | |
| 524 | op_offset_to_line = dict(dis.findlinestarts(co)) |
| 525 | curr_line_index = 0 |
| 526 | |
| 527 | instructions = self.instructions |
| 528 | while instructions: |
| 529 | instruction = instructions[0] |
| 530 | new_line_index = op_offset_to_line.get(instruction.offset) |
| 531 | if new_line_index is not None: |
| 532 | curr_line_index = new_line_index |
| 533 | |
| 534 | self._process_next(curr_line_index) |
| 535 | return self.writer.line_to_contents |
| 536 | |
| 537 | def merge_code(self, code): |
| 538 | if DEBUG: |
| 539 | print("merge code ----") |
| 540 | # for d in dir(code): |
| 541 | # if not d.startswith('_'): |
| 542 | # print(d, getattr(code, d)) |
| 543 | line_to_contents = _PyCodeToSource(code, self.memo).build_line_to_contents() |
| 544 | lines = [] |
| 545 | for line, contents in sorted(line_to_contents.items()): |
| 546 | lines.append(line) |
| 547 | self.writer.get_line(line).extend(contents) |
| 548 | if DEBUG: |
| 549 | print("end merge code ----") |
| 550 | return lines |
| 551 | |
| 552 | def disassemble(self): |
| 553 | show_lines = False |
| 554 | line_to_contents = self.build_line_to_contents() |
| 555 | stream = StringIO() |
| 556 | last_line = 0 |
no outgoing calls
no test coverage detected