def test_inline_list_decompile():
    d = Decompiler()
    x = [1, 1, 2., 3j, False, 3L, '3', u'3']
    d.ingest(x)
    ref = d.mgr.reference(x)
    result = "%s = %r" % (ref, x)
    assert d.render() == result
    _test_equality(d.render(), ref, x)
def test_dict_decompile(arg):
    d = Decompiler()
    x = dict([('b', arg), ('a', arg)])
    d.ingest(x)
    ref = d.mgr.reference(x)
    result = "%s = { 'a': %r, 'b': %r }" % (ref, arg, arg)

    assert d.render() == result
    _test_equality(d.render(), ref, x)
def test_name_hint():
    """In the absence of name conflicts, output ref should use name_hint"""
    x = [5, 5, 5, 5, 5, 5]
    d = Decompiler()
    d.ingest(x, name_hint='zzz')

    answer = "zzz = [5, 5, 5, 5, 5, 5]"
    result = d.render()

    assert answer == result
def test_unsupported_type():
    """Raise type error when trying to ingest an unsupported type"""
    class Foo(object):
        pass
    d = Decompiler()

    with pytest.raises(TypeError) as exc:
        d.ingest(Foo())
    assert exc.value.args[0] == ("Don't know how to decompile objects of "
                                 "type %s" % type(Foo()))
def test_numpy_decompile():
    import numpy as np
    x = np.array([1,2,3,4])
    s = x.dumps()
    d = Decompiler()
    d.ingest(x)

    ref = d.mgr.reference(x)
    answer = "import numpy as np\n%s = np.loads(%r)" % (ref, s)

    assert d.render() == answer
    exec(d.render())
    np.testing.assert_array_equal(locals()[ref],  x)
def test_magic_method():
    """Decompiler looks for __expfac__ for expression factories"""
    class Foo(object):
        def __init__(self, x):
            self.x = x

        def __expfac__(self, decompiler, obj):
            return [Expression("Foo({{x}})", x=obj.x, output_ref = obj)]

    f = Foo(4)
    d = Decompiler()
    d.ingest(f)

    ref = d.mgr.reference(f)
    answer = "%s = Foo(4)" % ref
    result = d.render()

    assert result == answer
def main(input_par, output_par):
    output_file = open(output_par, 'w')

    with open(input_par, 'r') as file:
        body_of_file = file.read().splitlines()
    # flag_config = False
    # flag_instructions = False
    status_of_parse = "start"
    set_of_instructions = []
    set_of_config = []
    name_of_program = ""
    for row in body_of_file:
        row = re.sub("/\*(.*?)\*/", '', row)
        row = row.strip()
        if row.find(".kernel ") != -1:
            # if flag_instructions:
            if status_of_parse == "instruction":
                # flag_instructions = False
                status_of_parse = "kernel"
                decompiler = Decompiler(output_file)
                decompiler.process_src(name_of_program, set_of_config,
                                       set_of_instructions)
                output_file.write("\n")
                set_of_instructions = []
                set_of_config = []
                name_of_program = ""
            name_of_program = row.split()[1]
        if row == ".config":
            # flag_config = True
            status_of_parse = "config"
        elif row == ".text":
            # flag_config = False
            # flag_instructions = True
            status_of_parse = "instruction"
        # elif flag_instructions:
        elif status_of_parse == "instruction":
            set_of_instructions.append(row)
        # elif flag_config:
        elif status_of_parse == "config":
            set_of_config.append(row)
        else:
            continue
    decompiler = Decompiler(output_file)
    decompiler.process_src(name_of_program, set_of_config, set_of_instructions)
    output_file.close()
Beispiel #8
0
""" Example use of the Decompiler

Try running this script like
python demo.py > out.py
python out.py

ISSUES:
-------
Axes limits are not being set correctly?
"""
import matplotlib.pyplot as plt
from decompiler import Decompiler

p, = plt.plot([1,2,3], [2,3,4], 'ro-', alpha = .3, markerfacecolor='b')
p.axes.set_xlim(-2, 20)
p.axes.set_ylim(-50, 50)
d = Decompiler()
d.ingest(p.figure)
print d.render()
print 'plt.show()'
Beispiel #9
0
 def __init__(self, parameters):
     unittest.TestCase.__init__(self, parameters)
     self.decompiler = Decompiler()
Beispiel #10
0
class DecomplierTests(unittest.TestCase):
    def __init__(self, parameters):
        unittest.TestCase.__init__(self, parameters)
        self.decompiler = Decompiler()

    def test_ExceptionThrowWhenRegisterDecodingFailed(self):
        self.decompiler.load([0x00, 0xAA, 0x02])
        self.assertRaises(Exception, self.decompiler.run)

    def test_ExceptionThrowWhenInstructionDecodingFailed(self):
        self.decompiler.load([0x04])
        self.assertRaises(Exception, self.decompiler.run)

    def test_ExceptionThrowWhenNotEnoughOperands(self):
        self.decompiler.load([0x00, 0x00])
        self.assertRaises(Exception, self.decompiler.run)

    def test_IfItsPutCommaBetweenTwoOperands(self):
        self.decompiler.load([0x00, 0x01, 0x02])
        self.decompiler.run()
        result = self.decompiler.getDecompiled()
        self.assertEqual(result, "MOV R1, R2")

    def test_IfItsNotPutCommaBetweenMnemonicInstructionAndFirstOperand(self):
        self.decompiler.load([0x18, 0x01])
        self.decompiler.run()
        result = self.decompiler.getDecompiled()
        self.assertEqual(result, "NOT R1")

    def test_IfItsPuttingConstantsHexadecimaly(self):
        self.decompiler.load([0x42, 0xFA])
        self.decompiler.run()
        result = self.decompiler.getDecompiled()
        self.assertEqual(result, "CALL 0xFA")

    def test_IfItCanHandleInstructionWithoutOperands(self):
        self.decompiler.load([0xFF])
        self.decompiler.run()
        result = self.decompiler.getDecompiled()
        self.assertEqual(result, "HALT")
def test_repeated_add_import():
    """Don't repeat import statements on render"""
    d = Decompiler()
    d.add_import('import numpy as np')
    d.add_import('import numpy as np')
    assert d.render() == 'import numpy as np'
def test_add_import():
    d = Decompiler()
    d.add_import('import numpy as np')
    assert d.render() == 'import numpy as np'