Ejemplo n.º 1
0
 def disassemble_code(self, fname, startlineno, name, generic_format=False):
     # 'generic_format' is False for PyPy2 (returns a
     # disassembler.CodeRepresentation) or True otherwise (returns a
     # GenericCode, without attempting any disassembly)
     try:
         if py.path.local(fname).check(file=False):
             return None  # cannot find source file
     except py.error.EACCES:
         return None  # cannot open the file
     key = (fname, startlineno, name)
     try:
         return self.disassembled_codes[key]
     except KeyError:
         pass
     if generic_format:
         res = GenericCode(fname, startlineno, name)
     else:
         codeobjs = self.load_code(fname)
         if (startlineno, name) not in codeobjs:
             # cannot find the code obj at this line: this can happen for
             # various reasons, e.g. because the .py files changed since
             # the log was produced, or because the co_firstlineno
             # attribute of the code object is wrong (e.g., code objects
             # produced by gateway.applevel(), such as the ones found in
             # nanos.py)
             return None
         code = codeobjs[(startlineno, name)]
         res = dis(code)
     self.disassembled_codes[key] = res
     return res
Ejemplo n.º 2
0
 def disassemble_code(self, fname, startlineno, name, generic_format=False):
     # 'generic_format' is False for PyPy2 (returns a
     # disassembler.CodeRepresentation) or True otherwise (returns a
     # GenericCode, without attempting any disassembly)
     try:
         if py.path.local(fname).check(file=False):
             return None # cannot find source file
     except py.error.EACCES:
         return None # cannot open the file
     key = (fname, startlineno, name)
     try:
         return self.disassembled_codes[key]
     except KeyError:
         pass
     if generic_format:
         res = GenericCode(fname, startlineno, name)
     else:
         codeobjs = self.load_code(fname)
         if (startlineno, name) not in codeobjs:
             # cannot find the code obj at this line: this can happen for
             # various reasons, e.g. because the .py files changed since
             # the log was produced, or because the co_firstlineno
             # attribute of the code object is wrong (e.g., code objects
             # produced by gateway.applevel(), such as the ones found in
             # nanos.py)
             return None
         code = codeobjs[(startlineno, name)]
         res = dis(code)
     self.disassembled_codes[key] = res
     return res
Ejemplo n.º 3
0
 def test_find_ids_range(self):
     def f():
         a = 0 # ID: myline
         return a
     #
     start_lineno = f.func_code.co_firstlineno
     code = disassembler.dis(f)
     ids = find_ids_range(code)
     assert len(ids) == 1
     myline_range = ids['myline']
     assert list(myline_range) == range(start_lineno+1, start_lineno+2)
Ejemplo n.º 4
0
    def test_find_ids_range(self):
        def f():
            a = 0  # ID: myline
            return a

        #
        start_lineno = f.func_code.co_firstlineno
        code = disassembler.dis(f)
        ids = find_ids_range(code)
        assert len(ids) == 1
        myline_range = ids['myline']
        assert list(myline_range) == range(start_lineno + 1, start_lineno + 2)
Ejemplo n.º 5
0
 def test_find_ids(self):
     def f():
         i = 0
         x = 0
         z = x + 3 # ID: myline
         return z
     #
     code = disassembler.dis(f)
     ids = find_ids(code)
     assert len(ids) == 1
     myline = ids['myline']
     opcodes_names = [opcode.__class__.__name__ for opcode in myline]
     assert opcodes_names == ['LOAD_FAST', 'LOAD_CONST', 'BINARY_ADD', 'STORE_FAST']
Ejemplo n.º 6
0
    def test_find_ids(self):
        def f():
            i = 0
            x = 0
            z = x + 3  # ID: myline
            return z

        #
        code = disassembler.dis(f)
        ids = find_ids(code)
        assert len(ids) == 1
        myline = ids['myline']
        opcodes_names = [opcode.__class__.__name__ for opcode in myline]
        assert opcodes_names == [
            'LOAD_FAST', 'LOAD_CONST', 'BINARY_ADD', 'STORE_FAST'
        ]
Ejemplo n.º 7
0
 def disassemble_code(self, fname, startlineno, name):
     try:
         if py.path.local(fname).check(file=False):
             return None # cannot find source file
     except py.error.EACCES:
         return None # cannot open the file
     key = (fname, startlineno, name)
     try:
         return self.disassembled_codes[key]
     except KeyError:
         codeobjs = self.load_code(fname)
         if (startlineno, name) not in codeobjs:
             # cannot find the code obj at this line: this can happen for
             # various reasons, e.g. because the .py files changed since
             # the log was produced, or because the co_firstlineno
             # attribute of the code object is wrong (e.g., code objects
             # produced by gateway.applevel(), such as the ones found in
             # nanos.py)
             return None
         code = codeobjs[(startlineno, name)]
         res = dis(code)
         self.disassembled_codes[key] = res
         return res
Ejemplo n.º 8
0
 def disassemble_code(self, fname, startlineno, name):
     try:
         if py.path.local(fname).check(file=False):
             return None  # cannot find source file
     except py.error.EACCES:
         return None  # cannot open the file
     key = (fname, startlineno, name)
     try:
         return self.disassembled_codes[key]
     except KeyError:
         codeobjs = self.load_code(fname)
         if (startlineno, name) not in codeobjs:
             # cannot find the code obj at this line: this can happen for
             # various reasons, e.g. because the .py files changed since
             # the log was produced, or because the co_firstlineno
             # attribute of the code object is wrong (e.g., code objects
             # produced by gateway.applevel(), such as the ones found in
             # nanos.py)
             return None
         code = codeobjs[(startlineno, name)]
         res = dis(code)
         self.disassembled_codes[key] = res
         return res