|
| 1 | +# future |
| 2 | +from __future__ import print_function |
| 3 | +# std |
| 4 | +import os |
| 5 | +import dis |
| 6 | +import difflib |
| 7 | +import subprocess |
| 8 | +import tempfile |
| 9 | +# compatability |
| 10 | +import six |
| 11 | +# uncompyle6 / xdis |
| 12 | +from uncompyle6 import PYTHON_VERSION, deparse_code |
| 13 | + |
| 14 | + |
| 15 | +def _dis_to_text(co): |
| 16 | + return dis.Bytecode(co).dis() |
| 17 | + |
| 18 | + |
| 19 | +def print_diff(original, uncompyled): |
| 20 | + """ |
| 21 | + Try and display a pretty html line difference between the original and |
| 22 | + uncompyled code and bytecode if elinks and BeautifulSoup are installed |
| 23 | + otherwise just show the diff. |
| 24 | +
|
| 25 | + :param original: Text describing the original code object. |
| 26 | + :param uncompyled: Text describing the uncompyled code object. |
| 27 | + """ |
| 28 | + original_lines = original.split('\n') |
| 29 | + uncompyled_lines = uncompyled.split('\n') |
| 30 | + args = original_lines, uncompyled_lines, 'original', 'uncompyled' |
| 31 | + try: |
| 32 | + from bs4 import BeautifulSoup |
| 33 | + diff = difflib.HtmlDiff().make_file(*args) |
| 34 | + diff = BeautifulSoup(diff, "html.parser") |
| 35 | + diff.select_one('table[summary="Legends"]').extract() |
| 36 | + except ImportError: |
| 37 | + print('\nTo display diff highlighting run:\n pip install BeautifulSoup4') |
| 38 | + diff = difflib.HtmlDiff().make_table(*args) |
| 39 | + |
| 40 | + with tempfile.NamedTemporaryFile(delete=False) as f: |
| 41 | + f.write(str(diff).encode('utf-8')) |
| 42 | + |
| 43 | + try: |
| 44 | + print() |
| 45 | + html = subprocess.check_output([ |
| 46 | + 'elinks', |
| 47 | + '-dump', |
| 48 | + '-no-references', |
| 49 | + '-dump-color-mode', |
| 50 | + '1', |
| 51 | + f.name, |
| 52 | + ]).decode('utf-8') |
| 53 | + print(html) |
| 54 | + except: |
| 55 | + print('\nFor side by side diff install elinks') |
| 56 | + diff = difflib.Differ().compare(original_lines, uncompyled_lines) |
| 57 | + print('\n'.join(diff)) |
| 58 | + finally: |
| 59 | + os.unlink(f.name) |
| 60 | + |
| 61 | + |
| 62 | +def are_instructions_equal(i1, i2): |
| 63 | + """ |
| 64 | + Determine if two instructions are approximately equal, |
| 65 | + ignoring certain fields which we allow to differ, namely: |
| 66 | +
|
| 67 | + * code objects are ignore (should probaby be checked) due to address |
| 68 | + * line numbers |
| 69 | +
|
| 70 | + :param i1: left instruction to compare |
| 71 | + :param i2: right instruction to compare |
| 72 | +
|
| 73 | + :return: True if the two instructions are approximately equal, otherwise False. |
| 74 | + """ |
| 75 | + result = (1==1 |
| 76 | + and i1.opname == i2.opname |
| 77 | + and i1.opcode == i2.opcode |
| 78 | + and i1.arg == i2.arg |
| 79 | + # ignore differences due to code objects |
| 80 | + # TODO : Better way of ignoring address |
| 81 | + and (i1.argval == i2.argval or '<code object' in str(i1.argval)) |
| 82 | + # TODO : Should probably recurse to check code objects |
| 83 | + and (i1.argrepr == i2.argrepr or '<code object' in i1.argrepr) |
| 84 | + and i1.offset == i2.offset |
| 85 | + # ignore differences in line numbers |
| 86 | + #and i1.starts_line |
| 87 | + and i1.is_jump_target == i2.is_jump_target |
| 88 | + ) |
| 89 | + return result |
| 90 | + |
| 91 | + |
| 92 | +def are_code_objects_equal(co1, co2): |
| 93 | + """ |
| 94 | + Determine if two code objects are approximately equal, |
| 95 | + see are_instructions_equal for more information. |
| 96 | +
|
| 97 | + :param i1: left code object to compare |
| 98 | + :param i2: right code object to compare |
| 99 | +
|
| 100 | + :return: True if the two code objects are approximately equal, otherwise False. |
| 101 | + """ |
| 102 | + # TODO : Use xdis for python2 compatability |
| 103 | + instructions1 = dis.Bytecode(co1) |
| 104 | + instructions2 = dis.Bytecode(co2) |
| 105 | + for opcode1, opcode2 in zip(instructions1, instructions2): |
| 106 | + if not are_instructions_equal(opcode1, opcode2): |
| 107 | + return False |
| 108 | + return True |
| 109 | + |
| 110 | + |
| 111 | +def validate_uncompyle(text, mode='exec'): |
| 112 | + """ |
| 113 | + Validate decompilation of the given source code. |
| 114 | +
|
| 115 | + :param text: Source to validate decompilation of. |
| 116 | + """ |
| 117 | + original_code = compile(text, '<string>', mode) |
| 118 | + original_dis = _dis_to_text(original_code) |
| 119 | + original_text = text |
| 120 | + |
| 121 | + deparsed = deparse_code(PYTHON_VERSION, original_code, |
| 122 | + compile_mode=mode, out=six.StringIO()) |
| 123 | + uncompyled_text = deparsed.text |
| 124 | + uncompyled_code = compile(uncompyled_text, '<string>', 'exec') |
| 125 | + |
| 126 | + if not are_code_objects_equal(uncompyled_code, original_code): |
| 127 | + |
| 128 | + uncompyled_dis = _dis_to_text(uncompyled_text) |
| 129 | + |
| 130 | + def output(text, dis): |
| 131 | + width = 60 |
| 132 | + return '\n\n'.join([ |
| 133 | + ' SOURCE CODE '.center(width, '#'), |
| 134 | + text.strip(), |
| 135 | + ' BYTECODE '.center(width, '#'), |
| 136 | + dis |
| 137 | + ]) |
| 138 | + |
| 139 | + original = output(original_text, original_dis) |
| 140 | + uncompyled = output(uncompyled_text, uncompyled_dis) |
| 141 | + print_diff(original, uncompyled) |
| 142 | + |
| 143 | + assert 'original' == 'uncompyled' |
0 commit comments